/**
 * PostgreSQL driver adapter for `Db`.
 *
 * Provides `create_pg_db()` to construct a `Db` backed by a `pg.Pool`.
 * Only imports `pg` types — the actual `pg` package is dynamically imported
 * by callers (e.g., `create_db`) that construct the `Pool`.
 *
 * @module
 */
import { Db, no_nested_transaction } from './db.js';
/**
 * Create a transaction implementation for a `pg.Pool`.
 *
 * Acquires a dedicated client from the pool for each transaction,
 * ensuring `BEGIN` / `COMMIT` / `ROLLBACK` all hit the same connection.
 * Releases the client in `finally` regardless of outcome.
 */
const create_pg_transaction = (pool) => async (fn) => {
    const client = await pool.connect();
    try {
        await client.query('BEGIN');
        const tx_db = new Db({ client, transaction: no_nested_transaction });
        const result = await fn(tx_db);
        await client.query('COMMIT');
        return result;
    }
    catch (err) {
        await client.query('ROLLBACK');
        throw err;
    }
    finally {
        client.release();
    }
};
/**
 * Create a `Db` backed by a `pg.Pool`.
 *
 * Owns the transaction implementation, acquiring a dedicated pool client
 * per transaction.
 *
 * @param pool - an already-constructed `pg.Pool`
 * @returns the `Db` instance and a `close` callback bound to `pool.end()`
 */
export const create_pg_db = (pool) => ({
    db: new Db({ client: pool, transaction: create_pg_transaction(pool) }),
    close: () => pool.end(),
});
