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

I am new to ORM methods and am using light-orm for mysql and I am wondering how to insert any new row into database using light-orm or suggest me a better ORM for nodejs and mysql.

Thank you in advance.

+3


source to share


1 answer


Completely valid question. With no ORM experience, I tried to find an answer. It took me 30 minutes and to find it I had to dig into the source code as there is no information in the documentation. (You will then receive a pull request).

Extracted from the documentation :

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

      

That's okay, but how can we get the object model

you are asking?

Here is a list of the available methods in the 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 {

      

Do you have a table users

with the following fields: login, password

. A simple example (not tested) should look something like this:

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 lighting device looks great, if you feel comfortable keep using it.

+3


source







All Articles