import { Logger } from '@pgpmjs/logger'; import { Pool } from 'pg'; import { getPgEnvOptions, PgConfig } from 'pg-env'; import { DbAdmin } from './admin'; import { PgTestClient, PgTestClientOpts } from './test-client'; const log = new Logger('test-connector'); const SYS_EVENTS = ['SIGTERM']; const end = async (pool: Pool): Promise => { try { if ((pool as any).ended || (pool as any).ending) { log.warn('โš ๏ธ pg pool already ended or ending'); return; } await pool.end(); } catch (err) { log.error('โŒ pg pool termination error:', err); } }; export class PgTestConnector { private static instance: PgTestConnector; private readonly clients = new Set(); private readonly pgPools = new Map(); private readonly seenDbConfigs = new Map(); private readonly pendingConnects = new Set>(); private config: PgConfig; private verbose = false; private shuttingDown = false; private constructor(config: PgConfig, verbose = false) { this.verbose = verbose; this.config = config; SYS_EVENTS.forEach((event) => { process.on(event, () => { log.info(`โน Received ${event}, closing all connections...`); this.closeAll(); }); }); } static getInstance(config: PgConfig, verbose = false): PgTestConnector { if (!PgTestConnector.instance) { PgTestConnector.instance = new PgTestConnector(config, verbose); } return PgTestConnector.instance; } private poolKey(config: PgConfig): string { return `${config.user}@${config.host}:${config.port}/${config.database}`; } private dbKey(config: PgConfig): string { return `${config.host}:${config.port}/${config.database}`; } beginTeardown(): void { this.shuttingDown = true; } private registerConnect(p: Promise): void { this.pendingConnects.add(p); p.finally(() => this.pendingConnects.delete(p)); } private async awaitPendingConnects(): Promise { const arr = Array.from(this.pendingConnects); if (arr.length) { await Promise.allSettled(arr); } } getPool(config: PgConfig): Pool { const key = this.poolKey(config); if (!this.pgPools.has(key)) { const pool = new Pool(config); this.pgPools.set(key, pool); log.info(`๐Ÿ“˜ Created new pg pool: ${key}`); } return this.pgPools.get(key)!; } getClient(config: PgConfig, opts: Partial = {}): PgTestClient { if (this.shuttingDown) { throw new Error('PgTestConnector is shutting down; no new clients allowed'); } const client = new PgTestClient(config, { trackConnect: (p) => this.registerConnect(p), ...opts }); this.clients.add(client); const key = this.dbKey(config); this.seenDbConfigs.set(key, config); log.info(`๐Ÿ”Œ New PgTestClient connected to ${config.database}`); return client; } async closeAll(opts: { keepDb?: boolean } = {}): Promise { const { keepDb = false } = opts; this.beginTeardown(); await this.awaitPendingConnects(); log.info('๐Ÿงน Closing all PgTestClients...'); await Promise.all( Array.from(this.clients).map(async (client) => { try { await client.close(); log.success(`โœ… Closed client for ${client.config.database}`); } catch (err) { log.error(`โŒ Error closing PgTestClient for ${client.config.database}:`, err); } }) ); this.clients.clear(); log.info('๐Ÿงฏ Disposing pg pools...'); const poolTasks: Promise[] = []; for (const [key, pool] of this.pgPools.entries()) { log.debug(`๐Ÿงฏ Disposing pg pool [${key}]`); poolTasks.push(end(pool)); } this.pgPools.clear(); await Promise.allSettled(poolTasks); if (keepDb) { log.info('๐Ÿ“ฆ Keeping databases (keepDb=true)...'); const dbNames = Array.from(this.seenDbConfigs.values()).map(c => c.database); log.info(`๐Ÿ“ฆ Preserved databases: ${dbNames.join(', ')}`); } else { log.info('๐Ÿ—‘๏ธ Dropping seen databases...'); await Promise.all( Array.from(this.seenDbConfigs.values()).map(async (config) => { try { const rootPg = getPgEnvOptions(this.config); const admin = new DbAdmin( { ...config, user: rootPg.user, password: rootPg.password }, this.verbose ); admin.drop(); log.warn(`๐Ÿงจ Dropped database: ${config.database}`); } catch (err) { log.error(`โŒ Failed to drop database ${config.database}:`, err); } }) ); } this.seenDbConfigs.clear(); const action = keepDb ? 'preserved' : 'dropped'; log.success(`โœ… All PgTestClients closed, pools disposed, databases ${action}.`); this.pendingConnects.clear(); this.shuttingDown = false; } close(): void { this.closeAll(); } drop(config: PgConfig): void { const key = this.dbKey(config); const admin = new DbAdmin(config, this.verbose); admin.drop(); log.warn(`๐Ÿงจ Dropped database: ${config.database}`); this.seenDbConfigs.delete(key); } async kill(client: PgTestClient): Promise { await client.close(); this.drop(client.config); } }