Sync / foldkit-durable
durable
When phones or browser tabs edit shared data offline and retry requests over flaky networks, your server needs one authoritative place to decide which edits committed, in what order, and what state those commits produce—without applying a retried request twice.
foldkit-durable is a server-side operation journal on SQLite. For each document, it stores a gap-free log of committed operations, the current reduced snapshot, and a sequence cursor, validating, authorizing, reducing, and persisting each append inside one SQLite transaction so retries return the recorded outcome instead of running again.
It is the server half of replicated Foldkit state, pairing with foldkit-sync via journalContract() or running standalone for any operation schema.
Choose between Durable, Sync, and a SQL database
Use foldkit-durable when several clients or devices write one server-owned document and need an authoritative order, idempotent retries, a snapshot for newcomers, and compaction that preserves history.
- Arbitrary ad-hoc SQL queries: use a relational database with
foldkit-remote-drizzle. - Client-side outbox and optimistic UI replay: use
foldkit-sync. - Multi-master peer merging without a central order: use a CRDT instead (one journal decides order per document).
- External provider calls after a commit: run
journal.recoveron your own schedule using the effect ledger.
How documents, sequences, cursors, and snapshots fit together
A document's log is a gap-free sequence of operations: op A at sequence 1, op B at 2, op C at 3, op D at 4. A cursor names a position in that log, and the snapshot at cursor 3 is the result of reducing operations 1 through 3 from empty.
| Name | Meaning |
|---|---|
| Document | One independently ordered stream of operations, keyed by DocumentId. |
| Operation | One durable change a client submitted. The application defines its shape. |
opId | The operation's stable identity. A retry reuses it. |
| Sequence | The gap-free position the journal assigns when an operation commits. |
| Cursor | A position a reader has reached; "what came after N?" |
| Snapshot | The reduced state of the committed prefix its cursor names. |
| Epoch | The identity of a document's history; new after a reset. |
A far-behind or brand-new replica starts by loading the snapshot, while an up-to-date replica reads operations after its cursor.
Install
pnpm add foldkit-durableeffect is a peer dependency, @effect/sql-sqlite-node comes with the package, and Node 22 is required for node:sqlite. For browsers, Cloudflare D1, or custom SQLite drivers, use foldkit-durable/core.
A first journal: append and load operations
This opens an in-memory SQLite journal, appends one todo operation, and loads both the reduced snapshot and the history after cursor 0:
import { Effect, Schema } from 'effect'
import { ActorId, Cursor, DocumentId, Journal, OpId } from 'foldkit-durable'
const Operation = Schema.Struct({ opId: Schema.String, title: Schema.String })
const Snapshot = Schema.Struct({ todos: Schema.Array(Schema.String) })
type Operation = typeof Operation.Type
type Snapshot = typeof Snapshot.Type
type Principal = { readonly actorId: string }
const program = Effect.gen(function* () {
const journal = yield* Journal.make({
file: ':memory:',
operation: Operation,
snapshot: Snapshot,
empty: () => ({ todos: [] }),
reduce: (snapshot, operation) => ({ todos: [...snapshot.todos, operation.title] }),
opId: operation => OpId.make(operation.opId),
actorId: (principal: Principal) => ActorId.make(principal.actorId),
})
const todos = DocumentId.make('todos')
yield* journal.append(todos, { opId: 'tab-1:1', title: 'Milk' }, { actorId: 'alice' })
const { snapshot, cursor } = yield* journal.load(todos)
const since = yield* journal.read(todos, Cursor.make(0))
return { snapshot, cursor, since }
}).pipe(Effect.scoped)Running Effect.runPromise(program) returns { snapshot: { todos: ['Milk'] }, cursor: 1, since: [{ operation: { opId: 'tab-1:1', title: 'Milk' }, opId: 'tab-1:1', sequence: 1, actorId: 'alice' }] }.
What happened:
Journal.makecreates the SQLite schema in':memory:'(or a file path, orConfig<string>) and closes the connection when theEffect.scopedblock exits. On a long-lived server, keep that scope open for the server's lifetime.appenddecodes the payload againstOperation, checks idempotency onopId, runsreduce, and persists the operation and new snapshot in one transaction.loadreturns the current snapshot ({ todos: ['Milk'] }) andcursor(1), whilereadreturns every committed operation after cursor0.
Sending { opId: 'tab-1:1', title: 'Milk' } again returns the recorded commit without appending a duplicate; sending different data under 'tab-1:1' fails with IdentityConflictError. Invalid sender payloads fail with InvalidOperationError, while stored rows that fail the current schema raise JournalError.