Skip to content

chord-postgres

chord-postgres supplies chord’s store with a PostgreSQL backend. chord runs its store on any SqliteDatabase — five async SQL methods plus transaction and close, which is how its Cloudflare Durable Object backend plugs in too — and this package supplies that interface over node-postgres, in one contained SQL dialect layer.

On top of it, chord apps get their declared tables, the documents engine, the outbox and export/import on Postgres, and pi-durable gets a Storage in one call (chord-postgres/pi-durable), because pi-durable’s own StoreStorage is a Storage over a chord store — nothing in pi-durable changes.

chord’s store speaks SQLite’s dialect through a small executor interface, so the seam between chord and this package is SQL. That makes the translation explicit and testable: src/dialect.ts translates the exact statements chord 1.7.0 generates, every construct is listed in the reference, and anything it does not recognise throws instead of being sent to Postgres. A mis-translated statement writes the wrong row; a refused one fails a test.

The contract is also a queue: chord requires an adapter to serialise unrelated operations and other transactions until a transaction finishes, because the store hands out raw reads and advances its in-memory lastSeq only after COMMIT. SQLite serialises everything on one connection; this adapter does it with a queue, and each transaction additionally takes an advisory lock on the schema, since a Postgres BEGIN gives no writer lock the way BEGIN IMMEDIATE does.

Terminal window
pnpm add chord-postgres @earendil-works/chord @earendil-works/pi-durable
# npm install chord-postgres @earendil-works/chord @earendil-works/pi-durable

pi-durable is optional: the root entry needs only chord, and only the chord-postgres/pi-durable subpath imports pi-durable.

import { postgresStorage } from 'chord-postgres/pi-durable'
import { Harness } from '@earendil-works/pi-durable'
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
const storage = await postgresStorage({ connection: process.env.DATABASE_URL, owner: 'my-app' }, context)
const harness = await Harness.open(storage, { registry, models }, context)

That is the whole setup. storage.close(context) (which harness.close(context) calls) closes the store and the pool with it.

import { postgresBackend, openPostgresStore } from 'chord-postgres'
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
import { openStore, defineTable, int, str } from '@earendil-works/chord'
// the common case: a store, closed once, owning its pool
const store = await openPostgresStore({ connection: process.env.DATABASE_URL }, context)
// or wire chord yourself
const store2 = await openStore(await postgresBackend({ connection: process.env.DATABASE_URL }), context)

openStore and importStore close the backend they are given, so a store opened this way owns its pool: store.close is all there is to do. postgresDatabase(options) is the third door, for a host that wants the raw SqliteDatabase.

chord-postgres @earendil-works/chord @earendil-works/pi-durable Postgres Node
0.1.x ~1.7.0 ~1.7.0 (optional) 16 … 19 (all tested in CI) ≥ 22.19

Every pull request runs the suites against Postgres 16, 17, 18 and 19; the full pipeline, including the package smoke test, runs on 19.

  • The dialect layer is verified against chord 1.7.0’s SQL, not against arbitrary SQLite. A chord release that emits a new construct makes this package throw until the construct is translated.
  • One store per schema. Several stores over one schema do not corrupt each other’s rows, but their metadata row is last-writer-wins; open several storages on one store instead.
  • The database’s server encoding must be UTF-8, because chord’s text comparison is code-point order and COLLATE "C" is what reproduces it.
  • A lost connection is permanent for that database object. It latches and you open a new one.

The full list is on limitations.