nodejs-mysql

0.1.3 • Public • Published

nodejs-mysql

The wrapper of Node.js MySQL driver with Promise enabled.

NPM Version NPM Downloads Linux Build Windows Build Test Coverage

NPM

import mysql from 'nodejs-mysql'
 
const db = mysql.getInstance(config)
 
db.exec('select col1, col2 from a_table')
        .then(rows => res.ok(rows))
        .catch(e => res.err(e)))

Installation

$ npm install nodejs-mysql

Demo

Folder demo includes a simple demo of both ES5 and ES6.

How to Use

Use config object to describe database connection(s).

// single database connection
const config = {
    host: '192.168.0.100',
    port: 3306,
    user: 'root',
    password: 'password',
    database: 'testdb'
}
 
// database connection pool
const config = [
    {
        host: '192.168.0.101',
        port: 3306,
        user: 'root',
        password: 'password',
        database: 'testdb',
        dateStrings: true,
        debug: false
    },{
        host: '192.168.0.102',
        port: 3306,
        user: 'root',
        password: 'password',
        database: 'testdb',
        dateStrings: true,
        debug: false
    },{
        host: '192.168.0.103',
        port: 3306,
        user: 'root',
        password: 'password',
        database: 'testdb',
        dateStrings: true,
        debug: false
    }
]

Get instance in anywhere before do database operations.

import mysql from 'nodejs-mysql'
 
const db = mysql.getInstance(config)

Attention If you are not using ES6, import package like this,

const mysql = require('nodejs-mysql').default;

Method exec for all database operation, close database connection is not need.

// select
db.exec('select col1, col2 from a_table')
        .then(rows => {
            // do sth. with result in rows
        })
        .catch(e => {
            // handle errors
        })
 
// insert
db.exec('insert into a_table set ?', {
            col1: value1,
            col2: value2
        })
        .then(rows => {
            // no error, insert ok
        })
        .catch(e => {
            // insert failed, handle errors
        })
        
// update
db.exec('update a_table set col1 = ?, col2 = ?', [value1, value2])
        .then(rows => {
            // no error, update ok
        })
        .catch(e => {
            // update failed, handle errors
        })
 
// delete
db.exec('delete from a_table where col1 = ?', [value1])
        .then(rows => {
            // no error, delete ok
        })
        .catch(e => {
            // delete failed, handle errors
        })

nodejs-mysql also support transaction. Transaction code like this,

db.connect()
  .then(conn => db.trans(conn))
  .then(conn => db.query(sql, params, conn))
  .then((rows, conn) => {
    // do sth. with the result in rows
    return db.query(sql2, params2, conn)
  })
  .then((rows, conn) => {
    // do sth. with the result2 in rows
    if (condition)
      return db.query(sql3, params3, conn)
    else
      return db.ok(rows) // <-- directly put result to next
  })
  .then((rows, conn) => {
    // do sth. with the result3 in rows
    return db.commit(conn)
  })
  .catch(e => {
    // handle errors
  })

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i nodejs-mysql

Weekly Downloads

50

Version

0.1.3

License

MIT

Last publish

Collaborators

  • daixy