Sync / foldkit-durable

Running in a browser or custom SQLite runtime

In a browser or any runtime without node:sqlite, import foldkit-durable/core instead. It provides the same journal as an Effect Layer requiring a SqlClient rather than a file path—for example, @effect/sql-sqlite-wasm in the browser:

ts
import * as WasmClient from '@effect/sql-sqlite-wasm/SqliteClient'
import { Layer } from 'effect'
import { DocumentId as CoreDocumentId, Journal as CoreJournal } from 'foldkit-durable/core'

const Todos = CoreJournal.define<Operation, Snapshot, Principal>('app/Todos')
// `storeOptions` as for `Journal.make`, without `file`.
const live = Todos.layer(storeOptions).pipe(Layer.provide(WasmClient.layerMemory({})))

const wasmProgram = Effect.gen(function* () {
  const journal = yield* Todos.tag
  return yield* journal.cursor(CoreDocumentId.make('todos'))
}).pipe(Effect.provide(live))

Because SqlClient is provided as a Layer, the database opens and closes cleanly with the journal, and you can share one connection with your app's other tables (Layer.mergeAll(Todos.layer(options), Tables).pipe(Layer.provide(driver))). Outside Effect, hold the layer in a ManagedRuntime. foldkit-durable/core imports no node: modules and hashes payloads in plain JavaScript with the same canonical algorithm as Node. On Cloudflare D1, pass @effect/sql-d1 with d1: true (see On Cloudflare D1).