/utils/dbs
Database connection and query management module that creates connection pools for multiple databases based on xyzEnvironment variables prefixed with 'DBS_'.
The node-postgres package is required to create a new connection Pool for DBS connections.
- Source
Requires
- module:pg
- module:/utils/logger
- module:/utils/processEnv
Methods
(async, inner) attemptQuery(pool, query, variablesopt, timeoutopt, onSentopt) → {Promise.<(Array|Error)>}
The attemptQuery method connects a client from the pool and executes the query on the client.
The client is scoped to the attempt and is released exactly once, only if the client was connected.
| Name | Type | Attributes | Description |
|---|---|---|---|
pool | Pool | The connection pool to use for the query. | |
query | string | SQL query to execute | |
variables | Array | <optional> | Parameters for the SQL query |
timeout | number | <optional> | Statement timeout in milliseconds |
onSent | function | <optional> | Called once the query has been sent to the database. |
- Source
Query rows or error.
- Type:
- Promise.<(Array|Error)>
(async, inner) clientQuery(query, variablesopt, timeoutopt, optionsopt) → {Promise.<(Array|boolean|Error)>}
The clientQuery method executes a query on the connection pool it is bound to.
Blocking queries are passed to the executeQuery method, which retries queries that fail with an error code in the RETRY_CODES set.
Nonblocking queries are attempted once without retry. The returned promise resolves true once the query has been sent to the database, without waiting for the query to complete. The query is sent before the caller can respond, so the database will execute the query even if a serverless process is frozen after the response. The promise resolves with the error if the client fails to connect or the statement timeout cannot be set. Errors from the query after it has been sent are logged, since the query result is not awaited.
- {Pool}
| Name | Type | Attributes | Description |
|---|---|---|---|
query | string | SQL query to execute | |
variables | Array | <optional> | Parameters for the SQL query |
timeout | number | <optional> | Statement timeout in milliseconds. Defaults to xyzEnv.STATEMENT_TIMEOUT. |
options | Object | <optional> |
| Name | Type | Attributes | Description |
|---|---|---|---|
options.nonblocking | boolean | <optional> | Resolve once the query is sent without waiting for the query result. |
- Source
Query rows, true for a sent nonblocking query, or an error.
- Type:
- Promise.<(Array|boolean|Error)>
(async, inner) executeQuery(pool, query, variablesopt, timeoutopt) → {Promise.<(Array|Error)>}
The executeQuery method attempts a query up to RETRY_LIMIT times.
Queries which fail with an error code in the RETRY_CODES set are retried with an exponential backoff delay. The backoff delay starts after the client from the failed attempt has been released.
The reason for a retry and the backoff delay are logged with the dbs_retry key, as well as the last error once the retries are exhausted.
| Name | Type | Attributes | Description |
|---|---|---|---|
pool | Pool | The connection pool to use for the query. | |
query | string | SQL query to execute | |
variables | Array | <optional> | Parameters for the SQL query |
timeout | number | <optional> | Statement timeout in milliseconds |
- Source
Query rows or the last error.
- Type:
- Promise.<(Array|Error)>