← back to Restaurant Directory

db/pool.ts

31 lines

/**
 * PG connection pool — single instance, used everywhere in lacountyeats.
 * Peer auth via Unix socket; no hardcoded passwords.
 *
 * DATABASE_URL is honoured if set (for CI / non-Mac envs); otherwise default
 * to the local Mac socket path with the project's PG database name.
 */
import 'dotenv/config';
import pg from 'pg';

const { Pool } = pg;

export const pool = new Pool({
  connectionString:
    process.env.DATABASE_URL ||
    'postgresql:///restaurant_professional_directory?host=/tmp',
  max: 8,
  idleTimeoutMillis: 30_000,
});

pool.on('error', (err) => {
  console.error('[pg pool error]', err);
});

export async function query<T extends pg.QueryResultRow = pg.QueryResultRow>(
  text: string,
  params?: unknown[],
): Promise<pg.QueryResult<T>> {
  return pool.query<T>(text, params);
}