import { Pool } from 'pg'; import type { DatabaseProbe } from './health.js'; /** * A small connection pool to the same PostgreSQL the api uses (ADR 0002). The * collab skeleton (issue #33) only needs it for the health probe; the document * persistence hooks (issue #35) will reuse this pool for load/store. */ export function createPool(connectionString: string): Pool { return new Pool({ connectionString, max: 4, // Fail fast on health probes instead of hanging when the db is unreachable. connectionTimeoutMillis: 5000, query_timeout: 5000, }); } /** Probe database reachability for the health endpoint. Never throws. */ export async function pingDatabase(pool: Pool): Promise { try { await pool.query('SELECT 1'); return { ok: true }; } catch (error) { return { ok: false, detail: shortMessage(error) }; } } function shortMessage(error: unknown): string { const message = error instanceof Error ? error.message : String(error); // Keep the health output single-line and free of any connection string. return message.split('\n').filter(Boolean).slice(-1)[0]?.slice(0, 200) ?? 'unknown error'; }