Skip to content

A wrapper for mysql2 connection pool query to support acquireTimeout and queryTimeout options

License

Notifications You must be signed in to change notification settings

7digital/mysql2-timeout

Repository files navigation

MySQL2 Timeout

7digital


Test npm Node.js Version

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.

About

A wrapper for mysql2 connection pool query to support acquireTimeout and queryTimeout options

Resources

License

Stars

Watchers

Forks

Packages

No packages published