Postgres Client (TypeScript)
item / server / data
The TypeScript twin of db-client on node-pg: query/one/execute/transaction, bounded retry on connection-level failures only, healthcheck, and a query that is already mcp-oauth's Query type. No npm dependency.
- type
- item
- files
- 2
- registry deps
- none
- npm deps
- none
- env vars
- DATABASE_URL, DATABASE_POOL_MAX, DATABASE_STATEMENT_TIMEOUT_MS, DATABASE_CONNECT_TIMEOUT_S
installs
- ~/lib/automindz/db-client.ts
- ~/tests/db-client.test.mts
- Wraps pg through a structural PoolLike — compiles with no pg installed, and db.query is exactly mcp-oauth's Query type
- Phase-aware retry: connection errors at checkout, only server-rolled-back SQLSTATEs (40001, 40P01, 57P01) after send
- Never logs parameters or the DSN; the password is redacted in every error, including the cause
import { createDb } from "@/lib/automindz/db-client"; const db = createDb(); const { rows } = await db.query<Job>("select * from jobs where id = $1", [id]) — one() returns a row or undefined and throws on more than one; execute() returns the row count; transaction(async (tx) => ...) runs on one client with BEGIN/COMMIT/ROLLBACK; healthcheck() is a boolean for health-next; end() at shutdown. db.query is exactly the Query type mcp-oauth's postgresStore(db.query) takes. The pool is created lazily and once (the promise is memoised, a failed open is not); pass { pool } for tests or a second database, { connectionString } to bypass env. Requires DATABASE_URL at call time — missing means a DbError naming the var and no connection; DATABASE_POOL_MAX, DATABASE_STATEMENT_TIMEOUT_MS, DATABASE_CONNECT_TIMEOUT_S optional. pg is yours (npm i pg), loaded with a dynamic import behind a structural PoolLike, so this file compiles with no driver installed and the error names the package. Retry is phase-aware: connection errors at checkout are retried with jittered backoff, after send only 40001, 40P01, 57P01 — a lost socket mid-statement surfaces rather than replaying a write that may have committed; never inside your transaction. Parameters never reach a log; the DSN password is redacted in every error and its cause. Multi-statement text returns the last result, as pg does. Run the shipped test with node --test tests/db-client.test.mts.
Variations
one state per section
No previews for this item yet.
