Getting started
chord-postgres has two doors. chord-postgres/pi-durable opens a pi-durable Storage; chord-postgres opens a chord
store (postgresDatabase, postgresBackend, openPostgresStore). Both take a postgres:// connection string, or a
node-postgres Pool you already have.
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-durable@earendil-works/chord is a required peer. @earendil-works/pi-durable is an optional peer: the root entry does not
import it, and only the chord-postgres/pi-durable subpath does. pg is a normal dependency of this package.
The adapter creates its schema (chord by default) when it is missing, so the database user needs the right to create
it. Nothing outside that schema is touched.
pi-durable
Section titled “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 pi-durable setup. What postgresStorage does:
- opens the chord store on Postgres (
openPostgresStorewith the same options, minusowner); - claims the owner’s tables in that store with pi-durable’s own
StoreStorage.open(store, owner, context); - returns a
Storagethat also exposesstore, and whoseclosecloses the store as well.
The owner is the name pi-durable’s tables live under, as "<owner>:<table>". pi-durable’s rule for it is
[a-z0-9@/._~-]+, and Postgres’ 63-byte identifier limit caps it at 38 characters, because the longest name
pi-durable declares is <owner>:entries:conversation.
postgresDatabase returns a chord SqliteDatabase; postgresBackend wraps it for openStore/importStore; and
openPostgresStore does both and calls openStore:
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 is the raw form, for a host that wants the SqliteDatabase itself — for a custom backend, or for a
store it opens with its own call:
import { postgresDatabase } from 'chord-postgres'
const database = await postgresDatabase({ connection: process.env.DATABASE_URL, schema: 'app_store' })// … chord work …await database.close()Closing
Section titled “Closing”Closing is the caller’s job and it is how the pool is released.
| You opened | Close |
|---|---|
postgresDatabase |
database.close() — ends the pool this adapter created, leaves a borrowed pool open |
postgresBackend / openPostgresStore |
the store’s close — openStore closes the backend, which closes the pool |
postgresStorage |
storage.close(context) — releases the owner’s claims, then closes the store and the pool |
harness.close(context) calls the storage’s close, so a pi-durable host does not close anything twice. When a call
that opens throws, the adapter closes what it already opened before rethrowing.
- API — every export and what it is for
- Options — what the entry points take
- How it works — the queue, the writer lock, the latch