DB pool wrapper for node.js This module is used to share a mysql/mariadb pool of connections between modules, classes, files etc.
It also logs with winston if there is a database error, so you do not need to fetch the database error manually each time.
npm i larvitdb
The module must first be required and then configured. Make this in your main application file:
const winston =log = winstondb = ;db;
See list of native options here. Then you can just require the module in your other files for usage, like this:
A direct query
const db = ;db;
Or, if a connection is needed:
const db = ;dbpool;
You dont need to get a connection to escape though. You can do like this:
const db = ;db;
Advanced configuration - recoverable errors
Sometimes recoverable errors happend in the database. One such example is deadlocks in a cluster. Here we'll provide an example of how to make the database layer retry a query 5 times if a deadlock happends, before giving up.
const db = ;db;// If this query fails with a deadlock, it will be retried up to 5 times.// On each retry a warning will be logged with winston// If the 5th retry fails, an error will be logged and the callback will be called with an errordb;
Advanced configuration - long running queries
By default a warning is logged if a query runs longer than 10k ms (10 seconds). This number can be tweaked like this for 20 seconds:
or like this to disable the warnings:
db;## Custom functions### Remove all tables from current databaseThis;db.removeAllTables();
Important about time zones!
All sessions with the database will be set to UTC time!
If you do, this library will convert the time zone info for you.
However, please note that all date time you get back from the database will be in UTC.
- Always set all new sessions to UTC time zone
- Convert Date objects to UTC datetimestamps that fits MariaDB and MySQL
Major from 1.x to 2.0 since this might break functionality for some implementations.