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 | 797x 797x 797x 816x 798x 798x 798x 796x 2x 2x 2x 795x 10x | import type { Client as PgClient, PoolConfig } from 'pg';
import type { HookRegistry } from '../hooks/hooks.ts';
import { AfterConnectHook } from './after-connect-hook.ts';
import type { ConnectionConfig as ConnectionSettings } from './options.ts';
/**
* Runs the connection hooks of a `Sequelize` instance: `beforeConnect` (may replace the password)
* and `afterConnect` (session setup on each new pooled connection).
*
* @internal
*/
export class ConnectHooks {
readonly #hooks: () => HookRegistry;
readonly #owner: Readonly<{ config: ConnectionSettings }>;
readonly #after = new AfterConnectHook((client) => this.#afterConnect(client));
/**
* @param hooks - Current hook registry of the instance.
* @param owner - The `Sequelize` instance (hook context and connection settings).
*/
constructor(hooks: () => HookRegistry, owner: Readonly<{ config: ConnectionSettings }>) {
this.#hooks = hooks;
this.#owner = owner;
}
/**
* Adds the hook handling to a pool configuration.
*
* @param config - Pool configuration.
* @returns Configuration whose password is resolved through `beforeConnect` and whose clients run `afterConnect`.
*/
apply(config: PoolConfig): PoolConfig {
return {
...config,
password: () => this.#password(config.password),
Client: this.#after.clientClass(),
};
}
async #password(configured: PoolConfig['password']): Promise<string> {
const password = typeof configured === 'function' ? await configured() : (configured ?? '');
if (!this.#hooks().has('beforeConnect')) {
return password;
}
const config = { ...this.#owner.config, password };
await this.#hooks().run('beforeConnect', this.#owner, config);
return config.password ?? '';
}
async #afterConnect(client: PgClient): Promise<void> {
if (this.#hooks().has('afterConnect')) {
await this.#hooks().run('afterConnect', this.#owner, client, { ...this.#owner.config });
}
}
}
|