import { Pool, type QueryResultRow } from "pg"; let pool: Pool | null = null; /** * Lazily opens the shared connection pool. It is lazy so that unit tests which * never touch the database do not need one running. */ export function getPool(): Pool { if (!pool) { pool = new Pool({ connectionString: process.env.DATABASE_URL, max: Number(process.env.PG_POOL_MAX ?? 10), }); } return pool; } export async function query(text: string, params: unknown[] = []): Promise { const result = await getPool().query(text, params); return result.rows; } export async function queryOne(text: string, params: unknown[] = []): Promise { const rows = await query(text, params); return rows.length > 0 ? rows[0] : null; } export async function closePool(): Promise { if (pool) { await pool.end(); pool = null; } }