In this final multi part post, I am going to show how to do "Delete" and "Update" records through Knex.
1. Using DELETE
Delete is the command to delete record from the table. We should run delete query with where condition which is normally based on Primary key.
It will also return the count of records that were deleted.
2. Using UPDATE
Update command can be used to change the column values for an existing record or records.
Happy Coding!1 | dhandrohit@gmail.com
1. Using DELETE
Delete is the command to delete record from the table. We should run delete query with where condition which is normally based on Primary key.
In the above delete command, we are trying to delete a row which has "Deptno" = 50. In case the command is successful, it will return a Promise.resolve that has been captured in .then function.var knex = require('knex')({ client: 'mssql', connection: { user: 'sa', password: '123', server: 'localhost', database: 'lovely' } }); knex("dept").where("deptno","50").del() .then(function (count) { console.log(count); }) .finally(function () { knex.destroy(); });
It will also return the count of records that were deleted.
2. Using UPDATE
Update command can be used to change the column values for an existing record or records.
In the above example we are trying to update the name of the an existing Deptno = 40 with the new name "TEST DEPARTMENT". It also returns a Promise. In case it ran successfully, we will get the Promise.resolve that can be used with .thenvar knex = require('knex')({ client: 'mssql', connection: { user: 'sa', password: '123', server: 'localhost', database: 'lovely' } }); knex('dept').where("deptno","40") .update({ dname: 'TEST DEPARTMENT' }) .then((count)=>{ console.log("record updated"+count); });
Happy Coding!1 | dhandrohit@gmail.com
Comments