mysql-magic
Promised based, high-level mysql library extension for Node.js >=7.6.
yarn add mysql-magic --save
Features
- Supports multiple named connection pools (different database settings with global scope)
- Safe async connection scopes - execute querys with build-in error handling
- High-level API
- Designed to run with the pure power of native
Promise
,await
andasync function
Usage
Application Bootstrap
const _mysqlMagic = ; // somewhere during your applications bootstrap...initialize a custom pool. It consumes any pool-options of [mysqljs](https://github.com/mysqljs/mysql)_mysqlMagic;
Run Querys
// get the poolconst _db = ; // wait for connection becomes availableconst con = await _db; // execute queryconst result fields = await con; // release connectionawait con;
Run Querys within scope
Within a connection scope, the connection will be automatically closed on finish or in case of an error. All kind of errors are proxied as exception.
const _db = ; // retrieve connection scopeconst numUsers = await _db; console;
API
- initPool
- getPool
- getConnection()
- Pool::getConnection
- Connection::release
- Connection::query
- Connection::insert
- Connection::fetchRow
- Connection::fetchAll
mysql-magic::initPool
Description: Initializes a new named connection pool. Pass mysql connection options as second argument (passthrough)
Syntax: pool:Object = initPool(name:String, options:Object)
const _db = ; // initialize a new "named" pool; // initialize default pool
mysql-magic::getPool
Description: Retrieves a named connection pool
Syntax: const pool:Object = getPool([name:String])
// get the userdb poolconst _db = ; // request a connectionconst con = await _db;...
mysql-magic::getConnection
Description: Retrieves a connection using async connection scope
Syntax: const result:Object = getConnection([name:String], scope:async-function)
Notice: A connection scope will automatically call con.release()
in the end or on an error - exceptions are forwarded
Example 1 - Named Pool
// just include the moduleconst _db = ; // retrieve connection scope using userdbconst numUsers = await _db; console;...
Example 2 - Default Pool
// just include the moduleconst _db = ; // retrieve connection scope using default poolconst numUsers = await _db; console;...
mysql-magic::Pool::getConnection
Description: Requests a connection from given pool
Syntax: const connection:Object = Pool::getConnection([scope:async-function])
Notice: A connection scope will automatically call con.release()
in the end or on an error - exceptions are forwarded
Example 1
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // do something...
Example 2 - Scopes
const _db = ; // retrieve connection scopeconst result = await _db; console;
mysql-magic::Connection::release
Description: Releases the connection
Syntax: Connection::release()
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // do something... // release the connectioncon;
mysql-magic::Connection::query
Description: Executes a mysql query
Syntax: const [result:Object, fields:Object] = Connection::query(sql:string, [values:Object])
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // do somethingconst result = await con; console; // release the connectioncon;
mysql-magic::Connection::insert
Description: Executes a insert with given values wrapped into object
Syntax: const [insertID:int, result:Object] = Connection::insert(table:string, values:Object)
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // insert a new userconst userid = await con; console; // release the connectioncon;
mysql-magic::Connection::fetchRow
Description: Executes a query an returns a single result as object
Syntax: const [result:Object] = Connection::fetchRow(sql:string, [values:Object])
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // fetch user by its nameconst user = await _db; console; // release the connectioncon;
mysql-magic::Connection::fetchAll
Description: Executes a query an returns an array of objects (similar to query but returns an empty array in case of empty resultset)
Syntax: const [results:Array] = Connection::fetchAll(sql:string, [values:Object])
// get the userdb poolconst _db = ; // request a connectionconst con = await _db; // fetch all usersconst users = await _db; // show usersfor const user in users console; // release the connectionconrelease
Any Questions ? Report a Bug ? Enhancements ?
Please open a new issue on GitHub
License
mysql-magic is OpenSource and licensed under the Terms of The MIT License