MySqlConnector

MySqlConnector

new MySqlConnector(optionsopt, logger)

Creates mysql2-promise Connection objects, optionally from a pool. If a database connection can not be acquired due to a timeout specified via the 'connectTimeout' options setting, the methods try again using exponential backoff with jitter until the 'connectRetryTimeout' options setting is exceeded. All duration-related options values can be provided as either numbers (milliseconds, including fractions) or as string values that are supported by parse-duration.

Source:
Parameters:
Name Type Attributes Description
options Object <optional>

Constructor options. Defaults are used if the provided value is falsy.

Name Type Attributes Description
connectionLimit Integer <optional>

The maximum number of connections to keep in the connection pool when usePool is true. Defaults to 10 connections.

connectTimeout Number | String <optional>

The amount of time (converted to milliseconds), to wait for one database connection request

connectRetryTimeout Number | String <optional>

The amount of time (converted to milliseconds), to wait for a successful database connection (including retries). Defaults to 5 minutes.

database String <optional>

Which database (aka schema) to connect to. Defaults to mysql.

enableKeepAlive Boolean <optional>

Used when usePool is true. When this value is true, pooled connections are periodically discarded if they are no longer alive, which also causes them to stay alive.

host String <optional>

The database server host name. Defaults to 0.0.0.0.

keepAliveInitialDelay Number | String <optional>

Used when enableKeepAlive is true and specifies how frequently (converted to milliseconds) connections are checked. Defaults to 10 seconds.

logger function | Object <optional>

A logger object or function. Can also be provided as the second parameter.

maxConnectDelay Number | String <optional>

The maximum amount of time (converted to milliseconds) to wait between connection attempts. It starts at 10 ms and increases exponentially. Defaults to 100 seconds.

multipleStatements Boolean <optional>

true enables mysql2 connections to run multiple statements (separated by seicolons) via one call to query(), execute(), etc.

password String <optional>

The password for the database user. Ignored when useIAM is true.

port String <optional>

The database server port number. Defaults to 3306.

queueLimit Integer <optional>

The maximum command queue size for one mysql2 connection

region String <optional>

Used when useIAM is true. The AWS region name.

ssl String <optional>

Typically 'Amazon RDS' when needed

useIAM Boolean <optional>

true to use AWS RDS IAM passwordless security

user String <optional>

The database user name. Defaults to root.

usePool Boolean <optional>

true enables connection pooling

logger function | Object

A logger object or function. Can also be provided via options.logger.

Methods

connect(loggeropt) → {Promise}

Checks the database connection

Source:
Parameters:
Name Type Attributes Description
logger Object <optional>
Returns:
Type:
Promise

Resolves to true or rejects in case of connection failure

execute(task, loggeropt) → {Promise}

Acquires a database connection and invokes a function that accepts a mysql2 Connection object. Each call to connection.query() etc. is run in a separate transaction.

Source:
Parameters:
Name Type Attributes Description
task function

A function that accepts a mysql2 connection object as the first parameter and logger as the second

logger Object <optional>
Throws:

The error caught while:

  1. connecting to the database
  2. await task()
Type
Error
Returns:
Type:
Promise

Resolves to the value returned by task(), if the database connection is successful

stop() → {Promise}

Closes the connection pool. Subsequent calls to connect(), execute(), and transaction() will fail.

Source:
Returns:
Type:
Promise

transaction(task, loggeropt) → {Promise}

Acquires a database connection, begins a transaction on that connection, invokes a function that accepts a mysql2 Connection object that uses a shared transaction, and either commits or rolls back the transaction, depending on whether the function throws an exception.

Source:
Parameters:
Name Type Attributes Description
task function

A function that accepts a mysql2 connection object as the first parameter and logger as the second

logger Object <optional>
Throws:

The error caught while:

  1. connecting to the database
  2. starting a transaction
  3. await task()
  4. committing the transaction
Type
Error
Returns:
Type:
Promise

Resolves to the value returned by task(), if the database connection is successful