Name: node-mysql
Owner: Namshi
Description: null
Created: 2017-04-11 04:59:00.0
Updated: 2017-04-13 09:20:22.0
Pushed: 2018-02-06 12:47:09.0
Homepage: null
Size: 5675
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
This wrapper provides some enhancements for node-mysql2
This module can be installed with either yarn or npm:
rn add namshi-node-mysql
bash
m install namshi-node-mysql --save
query()
uses prepared statements but does not support bulk operations.
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db"
db = require('namshi-node-mysql')(config);
uery('UPDATE foo SET key = ?', ['value']).then(() => {
return db.query('SELECT * FROM foo');
pread(rows => {
console.log('Look at all the foo', rows);
sing multiple databases, you can add a "name" key to your config object. For example:
config = {
name: "second-db",
host: "localhost",
user: "foo",
password: "bar",
database: "db"
db2 = require('namshi-node-mysql')(config);
query('SELECT * FROM users').spread(users => {
console.log('Hello users', users);
ou can enable debugging by passing the `debug` parameter as follow:
y default it is set to false.
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db",
debug: true;
bulk()
uses execute
which supports prepared statements and we use prepared statements for bulk.
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db"
values = [
['demian', 'demian@gmail.com', 1],
['john', 'john@gmail.com', 2],
['mark', 'mark@gmail.com', 3],
['pete', 'pete@gmail.com', 4]
db = require('namshi-node-mysql')(config);
ulk('INSERT INTO foo (name, email, n) VALUES ?', values).then(() => {
return db.query('SELECT * FROM foo');
pread(rows => {
console.log('Look at all the foo', rows);
prepareBulk()
can be used if you want to format a query for bulk operation with a connection reused for a transaction.
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db"
values = [
['demian', 'demian@gmail.com', 1],
['john', 'john@gmail.com', 2],
['mark', 'mark@gmail.com', 3],
['pete', 'pete@gmail.com', 4]
db = require('namshi-node-mysql')(config);
connection;
tartTransaction().then(conn => {
connection = conn;
hen(() => {
let [query, params] = db.prepareBulk('INSERT INTO foo (name, email, n) VALUES ?', [values]);
return connection.execute(query, params);
hen(result => {
return db.commit(connection);
hen(result => {
console.log('Rows committed');
atch(err => {
db.rollback(connection).then(result => {
console.log('Rollback executed due to ', err.message);
});
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db",
namedPlaceholders: true
db = require('namshi-node-mysql')(config);
uery('SELECT * FROM users WHERE LIMIT = :limit', {limit: 10}).spread( users => {
console.log('Hello users', users);
config = {
host: "localhost",
user: "foo",
password: "bar",
database: "db"
db = require('namshi-node-mysql')(config);
connection;
tartTransaction(30).then(conn => {
connection = con;
atch(err) {
//handle error
fault timeout here is set to 20
tartTransaction().then(conn => {
connection = con;
atch(err) {
//handle error
ommit(connection).catch(err => {
//handle err
ollback(connection).catch(err => {
//handle err
This library depends on node-mysql2. It is also considered a breaking-change upgrade of node-mysql2-promise.