namshi/node-mysql

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

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

node-mysql

This wrapper provides some enhancements for node-mysql2

Build Status

Installation

This module can be installed with either yarn or npm:

rn add namshi-node-mysql
bash
m install namshi-node-mysql --save
Example Usage of query

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);

Enable DEBUG mode to log the query being executed and its parameters.
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;

Example Usage of bulk

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);

Example of prepareBulk

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);
});

Example usage of namedPlaceholders
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);

Example usage of startTransaction, commit and rollback
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

Credits

This library depends on node-mysql2. It is also considered a breaking-change upgrade of node-mysql2-promise.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.