Knex can be used to insert data into tables. The following code shows how we can do that.
In the above code, we used a node package knex to connect Nodejs to mysql. And then we are finally inserting a new record in the table named "Depts". The value of the new dname inserted is 'Technology'.
Once its inserted, it will be returning an automatic incremented ID for that dept. This Id is used in the form of Promise with the help of then function.
var knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root',
password : '',
database : 'lovely'
}
});
knex('depts').insert({
name: 'Technology'
})
.returning('id')
.then((id) => {
console.log(id);
});
Once its inserted, it will be returning an automatic incremented ID for that dept. This Id is used in the form of Promise with the help of then function.
Comments