How to insert rows in mysql Db , using light-orm in nodejs


Problem :

i am new to ORM techniques and using light-orm for mysql and i am wondering how to insert any new row to the database using light-orm or suggest me the best ORM for nodejs and mysql.

thanks in advance.



Solution :

Totally valid question. Without experience in ORM I tried to find an answer. That took me 30 minutes, and to find it I had to dig into the source-code itself, as there is no info in the documentation. (Will make a pull request afterwards).

Extracted from the documentation:

model.create(function(err, model) {});

That is good, but how do we get the model object you ask?

Here is the list of available methods in Collection object:

/**
 * Create model
 * @param data Attributes, that will be setted during construction
 */
createModel(data: {});

/**
 * Create model
 * @param {boolean} add True, if created model should be added to models list
 */
createModel(add: boolean);

/**
 * Create model
 * @param data Attributes, that will be setted during construction
 * @param {boolean} add True, if created model should be added to models list
 */
createModel(data: {}, add: boolean);

createModel(options?: any, add?: boolean) : Model {

Granted you have a table users with fields: login, password. Simple example (not tested) shall look something like the following:

var mysql = require('mysql'),
    lightOrm = require('light-orm');

lightOrm.driver = mysql.createConnection(require('./connection.json'));
lightOrm.driver.connect();
var UsersCollection = new lightOrm.Collection('users');
var model = UsersCollection.createModel({login: "mylogin", password: "mypass"}, true);
model.create(); // Write the changes to database

Overall the light-orm looks fine, if you feel comfortable - keep using it.


Mysql Tutorials

Mysql Howto..

how to get the rows from mysql that is-5 or +5 then the current row?

PHP mysql how to relationship three tables showing outputs from different tables

how do I show all enum values from database in Java

How to combine multiple rows connected with a common id into one row after retrieving from mysql

how to use comparison operator in case statement in mysql

How to resolve incorrect SQL syntax error when using “Dimension Lookup / Update” with MySql?

How do I find and replace individual strings in a MySQL database?

how to prepare a string to be inserted into MySQL e.g. add backslash

how to concatenate to mysql_query onclick td [closed]

How to flush data from mysql.slow_log table in mysql?

How to extract field name from table having values as 1?

How to do an intersect of two subqueries using mysql

How to insert values into auto identity column in MYSQL [closed]

How to prevent duplicate inserts into a table

How to insert random numbers MySql

How to upload an image in php mysql [closed]

To Connect to MYSQL - How to access database array in setting.php

A better idea how to get a lot of MySql Data out of the db

How to merge php-mysql data with jquery to fade data

how to compare a string with strings in linkedlist and insert the result as boolean into mysql table

How to use MySQL like operator in JDBC?

How to import data from CSV file into MySQL database

How to insert HTML select value as text in MySQL via PHP

How to design a MySql Table for a Tag Cloud?

MySQL - How to select data by string length

MYSQL how to ensure that one query is ran right after another

How to search with firstname and last name in - Mysql

How to change an attribute in the newly created VIEW in mysql?

mysql - How to get data by date range given only month and year

How to duplicate a MySQL database on the same server