/** * Driver-agnostic Postgres pool/client surface used by the runtime data plane. * * The Node runtime registers its `pg` driver once at boot and the rest of the * data plane uses the abstract `RuntimePool` / `RuntimePoolClient` types. `pg` * exposes the relevant API surface (`pool.connect()` → `client.query() / * .release()`), so * the wrappers are thin. */ export interface RuntimePoolClient { query = Record>( text: string, params?: unknown[], ): Promise<{ rows: R[] }>; /** Destroy the physical connection when an active query exceeded its bound. */ release(destroy?: boolean): void; /** Interrupt an active query by destroying the physical transport. */ destroy?(error?: Error): void; } export interface RuntimePool { connect(): Promise; end(): Promise; } export interface RuntimeOneShotQueryClient { query = Record>( text: string, params?: unknown[], ): Promise<{ rows: R[] }>; } export type RuntimePoolFactory = (input: { connectionString: string; /** Soft cap on simultaneous connections; defaults to 4. */ maxConnections?: number; /** Idle timeout in ms before pool tears down a connection; defaults to 15000. */ idleTimeoutMs?: number; /** Connect timeout in ms; defaults to 10000. */ connectTimeoutMs?: number; }) => RuntimePool; export type RuntimeOneShotQueryFactory = (input: { connectionString: string; }) => RuntimeOneShotQueryClient; const runtimePgDriverRegistryKey = Symbol.for( 'deepline.play-runtime.runtime-pg-driver.registry', ); type RuntimePgDriverRegistry = { poolFactory: RuntimePoolFactory | null; oneShotQueryFactory: RuntimeOneShotQueryFactory | null; canReusePoolsAcrossRequests: boolean; }; function getRuntimePgDriverRegistry(): RuntimePgDriverRegistry { const globalWithRegistry = globalThis as typeof globalThis & { [runtimePgDriverRegistryKey]?: RuntimePgDriverRegistry; }; globalWithRegistry[runtimePgDriverRegistryKey] ??= { poolFactory: null, oneShotQueryFactory: null, canReusePoolsAcrossRequests: true, }; return globalWithRegistry[runtimePgDriverRegistryKey]; } /** * Install a Pool factory at process / Worker boot. Idempotent — calling twice * with different factories will throw to prevent silent driver swaps that * would otherwise drop in-flight pooled connections. */ export function registerRuntimePoolFactory( factory: RuntimePoolFactory, options: { canReuseAcrossRequests?: boolean } = {}, ): void { const registry = getRuntimePgDriverRegistry(); if (registry.poolFactory && registry.poolFactory !== factory) { throw new Error( 'A different runtime Postgres pool factory is already registered. ' + 'This is a build-config bug — only one driver should be installed per process.', ); } registry.poolFactory = factory; registry.canReusePoolsAcrossRequests = options.canReuseAcrossRequests ?? true; } export function isRuntimePoolFactoryRegistered(): boolean { return getRuntimePgDriverRegistry().poolFactory !== null; } export function canReuseRuntimePostgresPoolsAcrossRequests(): boolean { return getRuntimePgDriverRegistry().canReusePoolsAcrossRequests; } export function registerRuntimeOneShotQueryFactory( factory: RuntimeOneShotQueryFactory, ): void { const registry = getRuntimePgDriverRegistry(); if ( registry.oneShotQueryFactory && registry.oneShotQueryFactory !== factory ) { throw new Error( 'A different runtime one-shot Postgres query factory is already registered. ' + 'This is a build-config bug — only one driver should be installed per process.', ); } registry.oneShotQueryFactory = factory; } export function isRuntimeOneShotQueryFactoryRegistered(): boolean { return getRuntimePgDriverRegistry().oneShotQueryFactory !== null; } export function createRuntimeOneShotQueryClient(input: { connectionString: string; }): RuntimeOneShotQueryClient { const { oneShotQueryFactory } = getRuntimePgDriverRegistry(); if (!oneShotQueryFactory) { throw new Error( 'No runtime one-shot Postgres query factory registered. Call the runtime Postgres driver installer at boot before using one-shot query helpers.', ); } return oneShotQueryFactory(input); } export function createRuntimePool(input: { connectionString: string; maxConnections?: number; idleTimeoutMs?: number; connectTimeoutMs?: number; }): RuntimePool { const { poolFactory } = getRuntimePgDriverRegistry(); if (!poolFactory) { throw new Error( 'No runtime Postgres pool factory registered. Call ' + 'installPgRuntimePoolDriver() at boot before ' + 'using runtime-api Postgres helpers.', ); } return poolFactory(input); } /** Test seam — clears the registered factory so a test can swap drivers. */ export function __resetRuntimePoolFactoryForTests(): void { const registry = getRuntimePgDriverRegistry(); registry.poolFactory = null; registry.oneShotQueryFactory = null; registry.canReusePoolsAcrossRequests = true; }