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:
  • 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

for IT the said…
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Node JS Online Training from India . or learn thru Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Online Training

Popular posts from this blog

Step by step Cordova calabash-ios automation

How to get local time from UTC using Moment.JS

Measure the height of keyboard on PhoneGap app using jQuery