db/db_pg.ts

PostgreSQL driver adapter for Db.

Provides create_pg_db() to construct a Db backed by a pg.Pool. Statically imports only pg types; the pg runtime is dynamically imported where needed — by callers (e.g., create_db) that construct the Pool, and by register_pg_type_parsers to reach pg.types. pglite-only consumers never reach either path, so pg stays an optional peer dep.

view source

Declarations
#

2 declarations

create_pg_db
#

db/db_pg.ts view source

(pool: Pool): DbDriverResult import {create_pg_db} from '@fuzdev/fuz_app/db/db_pg.js';

Create a Db backed by a pg.Pool.

Owns the transaction implementation, acquiring a dedicated pool client per transaction.

pool

an already-constructed pg.Pool

type Pool

returns

DbDriverResult

the Db instance and a close callback bound to pool.end()

register_pg_type_parsers
#

db/db_pg.ts view source

(): Promise<void> import {register_pg_type_parsers} from '@fuzdev/fuz_app/db/db_pg.js';

Register the shared pg type-parser overrides on the module-global pg.types.

Dynamically imports the pg runtime (so pglite-only consumers, who never call this, don't need pg installed) and coerces int8 (BIGINT, OID 20) to a JS number. pg defaults to returning int8 as a string to avoid 2^53 precision loss; our int8 columns today (audit_log.seq, cell_history.id, fact.size) stay well under that bound, and reading as a number keeps the wire shape uniform with PGlite — which returns int8 as a number — so AuditLogEvent.seq and friends validate identically across both drivers.

Both create_db (production) and the test pg factory (testing/db.ts) register through this single site so test and prod read the same shape; a divergence here is exactly the test/prod write-semantics gap the parser exists to close.

CAVEAT: setTypeParser mutates pg.types globally — every pg.Pool in the process inherits the coercion, including pools the consumer constructs against unrelated databases. Any future int8 column that could legitimately exceed 2^53 (byte offsets, counters) will silently round; if one lands, localize via a per-pool types override instead of widening this global.

returns

Promise<void>

mutates

  • pg.types — registers the int8 parser on the global pg type registry

Depends on
#

Imported by
#