← back to Costa Rica
lib/db.js
15 lines
'use strict';
// Single shared pg pool for server.js + all route/lib modules.
const { Pool } = require('pg');
// Explicit, deliberate connection ceiling (env-tunable). Before cycle 17 server.js
// ran a SECOND pool, so the process accidentally allowed up to ~20 connections
// (2 x node-pg's default max of 10) — never a chosen capacity. Now there is one
// pool with a documented max: 20 preserves that ceiling as an intentional value on
// a single fork-mode process (Postgres max_connections is 100), tunable via PG_POOL_MAX.
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
max: Number(process.env.PG_POOL_MAX) || 20,
});
pool.on('error', (err) => console.error('[db] idle client error', err.message));
module.exports = { pool, query: (t, p) => pool.query(t, p) };