A simple to use wrapper over Node.JS mysql library
I was working on a project on Node.JS platform. In this project I was suppose to exchange data with MySql server. For this purpose I used two packages which are available in NPM repository, namely, mysql and mysql-queues. Later is required because former driver yet doesn't support transaction feature of MySql database. If you go through them you will be confused a little bit and will find it difficult to understand in one go. To simplify this situation I created an easy to use wrapper over these two libraries.
You can find my wrapper on GitHub by following this link. I am still in progress of making it feature rich.
How to use this wrapper:
Now you can use it in a way that is specified below:
You can find my wrapper on GitHub by following this link. I am still in progress of making it feature rich.
How to use this wrapper:
- Install mysql and mysql-queues using npm install command.
- Download wrapper by going on this link.
Now you can use it in a way that is specified below:
Initialize object
var dbClass = require('DB'); var options = {}; options.host = 'localhost'; options.user = 'root'; options.password = 'root'; options.database = 'test'; var db = new dbClass.DB(options);
Select statement
db.sql = 'select * from test where id = ? and name=?'; db.bindParams([id,name]); db.select(function(err, results, fields){ .. .. });
Transaction statement
db.sql = 'insert into test (name) values (?)'; db.startTransaction(); db.insertTransaction(['bharat'],function(err, info) { if(err) { console.log('error'); console.log(err); db.rollback(); } else { id = info.insertId; db.commit(); } }); db.executeTransaction();
Comments
Post a Comment