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.
Parameters:
Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object
|
<optional> |
Constructor options. Defaults are used if the provided value is falsy.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger |
function
|
Object
|
A logger object or function. Can also be provided via options.logger. |
Methods
connect(loggeropt) → {Promise}
Checks the database connection
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.
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:
- connecting to the database
- 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.
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.
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:
- connecting to the database
- starting a transaction
- await task()
- committing the transaction
- Type
-
Error
Returns:
- Type:
-
Promise
Resolves to the value returned by task(), if the database connection is successful