import pg from "pg";
import { config } from "../config.js";

const { Pool } = pg;
let _pool: pg.Pool | null = null;

export function getPool(): pg.Pool {
  if (!_pool) {
    _pool = new Pool({
      connectionString: config.postgresUrl,
      max: 30,
      min: 5,
      idleTimeoutMillis: 60_000,
      connectionTimeoutMillis: 5_000,
      allowExitOnIdle: false,
    });
    _pool.on("error", (err) => console.error("[pg-pool] idle client error", err));
  }
  return _pool;
}
