Sync / foldkit-durable

Services, codecs, errors, metrics

As a service. Journal.define<Operation, Snapshot, Principal>('app/TodoJournal') binds the context key and type parameters together and returns tag and layer:

ts
const TodoJournal = Journal.define<Operation, Snapshot, Principal>('app/TodoJournal')

const served = Effect.gen(function* () {
  const journal = yield* TodoJournal.tag
  return yield* journal.load(DocumentId.make('todos'))
}).pipe(Effect.provide(TodoJournal.layer(options)))

Separate Journal.define calls create distinct services (Journal.layer(options) uses the default key).

Codecs. operation and snapshot accept any Effect Schema.Codec (or { encode, decode, compared? }, see Codec.fromSchema) producing JSON-compatible payloads without Effect services.

Ids. Construct branded identifiers with DocumentId.make, OpId.make, ActorId.make, Sequence.make, and Cursor.make.

Errors. Tagged errors work with Effect.catchTag: JournalError, UnsupportedJournalVersionError, InvalidOperationError, OperationRejectedError, IdentityConflictError, InvalidCursorError, CompactedCursorError, InvalidCompactionError, and EffectFailedError.

Metrics. Journal.metrics records appends, compactions, owner effect runs, and coalesced effect runs.

Migrations. Schema tables upgrade automatically inside transactional user_version migrations; your own operation, snapshot, and effect payload schemas remain yours to migrate.