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.
Why this shape
Section titled “Why this shape”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.
Install
Section titled “Install”pnpm add chord-postgres @earendil-works/chord @earendil-works/pi-durable# npm install chord-postgres @earendil-works/chord @earendil-works/pi-durablepi-durable is optional: the root entry needs only chord, and only the chord-postgres/pi-durable subpath imports
pi-durable.
pi-durable in one call
Section titled “pi-durable in one call”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 poolconst store = await openPostgresStore({ connection: process.env.DATABASE_URL }, context)
// or wire chord yourselfconst 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.
Compatibility
Section titled “Compatibility”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.
What it does not do
Section titled “What it does not do”- 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.
- Getting started — install, pi-durable, chord, closing
- API — every export, both entry points
- Options — connection, pool, schema, timeouts, owner
- How it works — the queue, the writer lock, the latch, the schema version guard
- The SQL dialect layer — every construct translated, and every one refused
- Compatibility and maintenance — what is pinned and why
- Limitations — what it does not do yet