Skip to Content

src/db/client.ts — Postgres pool factory

Tiny factory that turns a connection string into a pg.Pool. The boundary where the env’s DATABASE_URL becomes a live, query-able pool.

Public surface

ExportShapePurpose
createPool(databaseUrl)(string) => pg.PoolConstruct the pool
QueryablePick<Pool, 'query'> | Pick<PoolClient, 'query'>Anything you can run a query against

Why Queryable

Repositories accept Queryable rather than Pool so tests can pass a PoolClient bound to a rolled-back transaction without changing the public API. See test/helpers/db.ts for the withRollback(pool, body) pattern this enables.

Lifecycle

Created once in src/index.ts, closed in the shutdown handler after Fastify finishes draining (pool.end() is awaited last so in-flight queries finish first).

Tests

No dedicated tests — exercised through the repository suites (test/unit/users.repo.test.ts) and the in-memory fakes used by dispatch tests.