node-mysql
This wrapper provides some enhancements for node-mysql2
Installation
This module can be installed with either yarn or npm:
$ yarn add namshi-node-mysql
$ npm install namshi-node-mysql --save
Example Usage of query
query()
uses prepared statements but does not support bulk operations.
let config =host: "localhost"user: "foo"password: "bar"database: "db"let db = config;db;// using multiple databases, you can add a "name" key to your config object. For example:let config =name: "second-db"host: "localhost"user: "foo"password: "bar"database: "db"let db2 = config;db2;
Enable DEBUG mode to log the query being executed and its parameters.
// You can enable debugging by passing the `debug` parameter as follow:// by default it is set to false.let 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.
let config =host: "localhost"user: "foo"password: "bar"database: "db"var values ='demian' 'demian@gmail.com' 1'john' 'john@gmail.com' 2'mark' 'mark@gmail.com' 3'pete' 'pete@gmail.com' 4;let db = config;db;
Example of prepareBulk
prepareBulk()
can be used if you want to format a query for bulk operation with a connection reused for a transaction.
let config =host: "localhost"user: "foo"password: "bar"database: "db"var values ='demian' 'demian@gmail.com' 1'john' 'john@gmail.com' 2'mark' 'mark@gmail.com' 3'pete' 'pete@gmail.com' 4;let db = config;let connection;db
namedPlaceholders
Example usage oflet config =host: "localhost"user: "foo"password: "bar"database: "db"namedPlaceholders: truelet db = config;db;
Example usage of startTransaction, commit and rollback
let config =host: "localhost"user: "foo"password: "bar"database: "db"let db = config;let connection;db//handle error;//default timeout here is set to 20db//handle error;db;db;
Credits
This library depends on node-mysql2. It is also considered a breaking-change upgrade of node-mysql2-promise.