quick-mysql
v1.0.2
Published
A quick and easy wrapper around mysql module
Downloads
2
Readme
This is a simple wrapper around dougwilson's mysql module which auto manages pools and provides simple to use utility methods.
Environment Variables
All the configs are sourced from environment variables.
Note : You can also inject your own mysql connection options using the
setConnectionOptions({})
function
process.env.ENABLE_GLOBAL_POOL
If enabled, will created a globally exposed and maintained pool of connections.
true
by default, can be turned off by setting value to "false"
process.env.MYSQL_CONNECTION_LIMIT
Total number of connections to keep in the pool. (Default: 10
)
process.env.MYSQL_HOST
The hostname of the database you are connecting to. (Default: localhost
)
process.env.MYSQL_PORT
The port number to connect to. (Default: 3306
)
process.env.MYSQL_USERNAME
The MySQL user to authenticate as.
process.env.MYSQL_PASSWORD
The password of that MySQL user.
process.env.MYSQL_DATABASE
Name of the database to use for this connection
Usage
Usage is pretty straight forward.
const Model = require('quick-mysql');
Model.fetch('select name from user where id = ?', [1])
.then((results)=>console.log(results))
.catch((err)=>console.log(err));
Multiple sql statements
const Model = require('quick-mysql');
let queries = [
['select name from user where id = ?',[1]],
['select name from user where id = ?',[2]]
];
Model.fetchMultiple(queries)
.then((results)=>console.log(results))
.catch((err)=>console.log(err));
Custom connection properties
if you want to override the ENV variable sourcing, you can.
const Model = require('quick-mysql');
// https://github.com/mysqljs/mysql#connection-options
Model.setConnectionOptions({
connectionLimit: process.env.MYSQL_CONNECTION_LIMIT || 10,
host: process.env.MYSQL_HOST || "localhost",
user: process.env.MYSQL_USERNAME,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
charset: 'utf8',
port: process.env.MYSQL_PORT || 3306,
dateStrings: true
});
Model.fetch('select name from user where id = ?', [1])
.then((results)=>console.log(results))
.catch((err)=>console.log(err));
Methods
fetch(sqlQuery,sqlArguments)
Executes a single sql query
fetchMultiple([[sqlQuery,sqlArguments]])
Executes multiple sql queries
update()
Alias of fetch()
updateMultiple()
Alias of fetchMultiple()
delete()
Alias of fetch()
deleteMultiple()
Alias of fetchMultiple()
setConnectionOptions(obj)
This custom obj
will be used as connection options for the mysql
module.
Available options : https://github.com/mysqljs/mysql#connection-options
releasePool()
Releases global pool connections