Minimal Express 5 server: paginated streaming, transactional writes, AbortSignal-aware deletes.
src/schema.ts— table definitions (users,posts)src/db.ts— singlePool+ singlesumak()instance, reused across requestssrc/server.ts— three routes exercising query, transaction, streaming, cancellation
export DATABASE_URL="postgres://postgres:pg@localhost:5432/postgres"
pnpm install
pnpm migrate # creates tables via `sumak migrate up`
pnpm devcurl http://localhost:3000/posts?limit=100
curl -X POST http://localhost:3000/posts \
-H 'content-type: application/json' \
-d '{"authorEmail":"ada@x.io","title":"first","body":"hello"}'- One sumak per process. Don't build a fresh
sumak()per request — the connection pool is what you share, not the query builder. Per-request context (tenant id, audit userId) plugs in viadb.withSchema(...)or plugin factories; the base instance stays immutable. - Streaming NDJSON.
db.selectFrom(...).stream()returns anAsyncIterablethat backs onto pg's server-side cursor. For a 10M-row export you keep a handful of rows in memory at once. - AbortSignal on DELETE. A client drop fires the
closeevent on the request; we propagate that to the driver via{ signal }, which cancels the query server-side rather than letting it complete against a dead socket. - Transactions are RAII.
db.transaction(async tx => ...)begins, passestxto the callback, commits on return, rolls back on throw. Thetxparameter is a bound sumak instance — it has the same API asdbbut with every statement issued on the same connection.