Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import type { PoolClient, QueryResultRow } from 'pg';
import type { LoggingFunction } from './options.ts';
/** Result of one executed statement. */
export type QueryResult = Readonly<{
rows: readonly QueryResultRow[];
rowCount: number;
command: string;
}>;
/** Database client a query runs on: a pool connection held by a transaction. */
export type Client = Pick<PoolClient, 'query' | 'release'>;
/**
* Connection that `sequelize.connectionManager.getConnection()` hands out and
* `releaseConnection()` takes back: the pooled `pg` client of the instance that owns it.
*/
export type Connection = Client;
/**
* Live connection counts of a pool (Sequelize v6 `sequelize-pool` `Pool`). Every property is read
* at access time, so a health gauge can keep one reference. Reading never opens a connection:
* all counters are `0` before the first query.
*
* @example
* ```ts
* const { size, available, using, waiting, maxSize } = sequelize.connectionManager.pool;
* console.log(`${using}/${maxSize} in use, ${waiting} waiting, ${available} idle of ${size}`);
* ```
*/
export interface PoolCounters {
/** Open connections, idle and checked out (`totalCount` of the `pg` pool). */
readonly size: number;
/** Idle connections ready to be handed out (`idleCount`). */
readonly available: number;
/** Checked out connections: `size - available`. */
readonly using: number;
/** Acquisitions waiting for a free connection (`waitingCount`). */
readonly waiting: number;
/** Configured `pool.max` (the `pg` default of 10 when unset). */
readonly maxSize: number;
}
/**
* `sequelize.connectionManager.pool`: {@link PoolCounters} of the whole instance. With
* `replication` configured the counters are the sums over the writer pool and every replica pool
* (v6 has no totals there, it has a separate `read` and `write` pool), and `read` / `write` give
* the counters of each side. `read` sums all replicas. Without replication `read` and `write`
* are `undefined`.
*
* @example
* ```ts
* const { write, read } = sequelize.connectionManager.pool;
* console.log(write?.using, read?.using);
* ```
*/
export interface ConnectionPool extends PoolCounters {
/** Counters of the writer pool; only with `replication`. */
readonly write: PoolCounters | undefined;
/** Counters of all read replica pools together; only with `replication`. */
readonly read: PoolCounters | undefined;
}
/**
* Options of `sequelize.connectionManager.getConnection()` (Sequelize v6 `GetConnectionOptions`).
*
* @example
* ```ts
* const options: GetConnectionOptions = { type: 'read', useMaster: false };
* const connection = await sequelize.connectionManager.getConnection(options);
* await sequelize.connectionManager.releaseConnection(connection);
* ```
*/
export type GetConnectionOptions = Readonly<{
/**
* `'read'` takes a read replica when one is configured. Default `'write'`. `'SELECT'` is the same as
* `'read'` (v6 passes the query type of a SELECT there).
*/
type?: 'read' | 'write' | 'SELECT';
/**
* Forces the writer even when `type` is `'read'`. postgrelize extension.
* @postgrelize
*/
useMaster?: boolean;
/** Transaction id the connection is acquired for; informational (v6 passes it for transactions). */
uuid?: string;
}>;
/**
* Acquire / release contract of `sequelize.connectionManager`. Every query, transaction, COPY and
* `sync` of the instance takes its pooled connection through `getConnection` and gives it back
* through `releaseConnection`, looked up on the manager at call time: both may be replaced on the
* instance (wrapped) to route connections.
*
* @example
* ```ts
* const manager = sequelize.connectionManager;
* const defaultGet = manager.getConnection.bind(manager);
* manager.getConnection = async (options) => {
* console.log('acquiring', options?.type ?? 'write');
* return defaultGet(options);
* };
* ```
*/
export interface ConnectionManagerContract {
/**
* Acquires a pooled connection.
*
* @param options - Replica selection and transaction id.
* @returns A connection that stays checked out until it is passed to `releaseConnection`.
*
* @example
* ```ts
* const connection = await sequelize.connectionManager.getConnection({ type: 'read' });
* await sequelize.connectionManager.releaseConnection(connection);
* ```
*/
getConnection(options?: GetConnectionOptions): Promise<Connection>;
/**
* Returns a connection obtained from `getConnection` to the pool that owns it.
*
* @param connection - Connection to release.
* @returns Nothing (may be asynchronous).
*
* @example
* ```ts
* const connection = await sequelize.connectionManager.getConnection();
* await connection.query('SELECT 1');
* await sequelize.connectionManager.releaseConnection(connection);
* ```
*/
releaseConnection(connection: Connection): Promise<void> | void;
/**
* Removes a broken connection from the pool that owns it instead of returning it: the client is
* closed and the pool opens a replacement when one is needed. The connection must not be used or
* released afterwards (a later release is ignored).
*
* @param connection - Connection obtained from `getConnection`.
* @returns Resolves when the connection is removed from its pool.
*
* @example
* ```ts
* const connection = await sequelize.connectionManager.getConnection({ type: 'read' });
* try {
* await connection.query('SELECT 1');
* await sequelize.connectionManager.releaseConnection(connection);
* } catch (error) {
* await sequelize.connectionManager.destroyConnection(connection);
* throw error;
* }
* ```
*/
destroyConnection(connection: Connection): Promise<void>;
/**
* Live connection counts of the instance's pools, for health gauges.
*
* @example
* ```ts
* const { size, using, waiting, maxSize } = sequelize.connectionManager.pool;
* ```
*/
readonly pool: ConnectionPool;
}
/** Per-call execution settings. */
export type ExecuteOptions = Readonly<{
client?: Client;
/** Run on a read replica when one is configured (ignored when `client` is set). */
route?: 'read' | 'write';
logging?: LoggingFunction | false;
benchmark?: boolean;
/** `statement_timeout` in milliseconds for this query only (`0` disables it). */
statementTimeout?: number;
transactionId?: string;
}>;
|