import pg from "pg"; const { Pool } = pg; // Module-level singleton - shared for the lifetime of the server process let _pool: pg.Pool | null = null; function getPool(): pg.Pool { if (_pool) return _pool; const connectionString = process.env.DATABASE_URL; if (!connectionString) { throw new Error("DATABASE_URL environment variable is required"); } _pool = new Pool({ connectionString, ssl: process.env.DATABASE_SSL === "true" ? { rejectUnauthorized: false } : false, max: 5, idleTimeoutMillis: 30_000, connectionTimeoutMillis: 5_000, }); _pool.on("error", (err) => { console.error("[db] Pool error:", err.message); }); return _pool; } export async function query( sql: string, params: (string | number | null | boolean | string[])[] = [] ): Promise> { return getPool().query(sql, params); }