@7digital/mysql2-timeout
v4.0.1
Published
A wrapper for mysql2 connection pool query to support acquireTimeout and queryTimeout options
Downloads
112
Readme
MySQL2 Timeout
A wrapper for mysql2
connection pool query
to support connection acquiring and query timeout options.
Installation
npm install mysql2 @7digital/mysql2-timeout
Usage
Just use this instead of mysql2
and call connect
passing acquireTimeout
and defaultQueryTimeout
millisecond values. If not passed, each will default to 10 seconds. All other options will be passed directly to mysql2
.
If connection aquisition times out, a DatabaseTimeout
error will be thrown.
If querying times out, it will
- kill the connection
- attempt to kill the outstanding query in the background
- throw a
DatabaseTimeout
error
You can override the default query timeout on a per-query basis by specifying timeout
when calling query
with an object.
This only exposes the mysql2
connection pool promise interface.
Example
const database = require('@7digital/mysql2-timeout');
async function main() {
const db = await database.connect({
host: 'localhost',
user: 'root',
database: 'test',
acquireTimeout: 2000,
defaultQueryTimeout: 5000
});
await db.query('SELECT 1');
await db.query({ sql: 'SELECT 2', timeout: 1000 });
}
Developing
Requirements:
- Node.js (see package.json for version)
- Docker
- Docker Compose
make test
Spins up database and test containers, runs the tests once, and then stops the containers.
make watch
Spins up database and test containers, and runs the tests whenever the source or tests change.