The wrapper of Node.js MySQL driver with Promise enabled.
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)))
$ npm install nodejs-mysql
Folder demo
includes a simple demo of both ES5 and ES6.
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
})