import pg from "pg";
import { getConfig } from "./config.js";
let pool = null;
export function getPool() {
    if (!pool) {
        pool = new pg.Pool({ connectionString: getConfig().databaseUrl, max: 5 });
    }
    return pool;
}
export async function query(text, values) {
    return getPool().query(text, values);
}
export async function withTransaction(fn) {
    const client = await getPool().connect();
    try {
        await client.query("BEGIN");
        const result = await fn(client);
        await client.query("COMMIT");
        return result;
    }
    catch (err) {
        await client.query("ROLLBACK");
        throw err;
    }
    finally {
        client.release();
    }
}
export async function closePool() {
    if (pool) {
        await pool.end();
        pool = null;
    }
}
