diff --git a/db.ts b/db.ts new file mode 100644 index 0000000..d8a3653 --- /dev/null +++ b/db.ts @@ -0,0 +1,31 @@ +import pg from "pg"; +const { Pool } = pg; + +if (!process.env.DATABASE_URL) { + throw new Error("DATABASE_URL must be set. Provision a Postgres database and set DATABASE_URL."); +} + +export const pool = new Pool({ + connectionString: process.env.DATABASE_URL, + max: 15, + idleTimeoutMillis: 30_000, + connectionTimeoutMillis: 8_000, + keepAlive: true, + keepAliveInitialDelayMillis: 10_000, +}); + +pool.on("error", (err) => { + console.error("[db:pool] idle client error:", err.message); +}); + +export const bgPool = new Pool({ + connectionString: process.env.DATABASE_URL, + max: 10, + idleTimeoutMillis: 20_000, + connectionTimeoutMillis: 10_000, + keepAlive: true, +}); + +bgPool.on("error", (err) => { + console.error("[db:bgPool] idle client error:", err.message); +});