Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

SqlEventJournal unstable

SQL-backed persistence for the unstable event-log journal.

This module implements EventJournal on top of a SqlClient. It stores event entries as encoded bytes and stores per-remote sequence metadata in separate tables, giving event-log programs a durable journal that can be replayed after restart and synchronized with remote journals.

2 exports Since v4.0.0 Source

Constructors

make

unstable
Since v4.0.0

Creates an EventJournal backed by a SQL database.

Details

The constructor creates the entry and remote metadata tables when needed, persists local and remote entries, and uses the configured SqlClient.

Signature

declare function make(options?: {
readonly entryTable?: string;
readonly remotesTable?: string;
}): Effect<{
readonly changes: Effect<Subscription<Entry>, never, Scope>;
readonly destroy: Effect<void, EventJournalError>;
readonly entries: Effect<readonly Array<Entry>, EventJournalError>;
readonly nextRemoteSequence: (remoteId: RemoteId) => Effect<number, EventJournalError>;
readonly withLock: (storeId: StoreId) => <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>;
readonly withRemoteUncommited: <A, E, R>(remoteId: RemoteId, f: (entries: readonly [Entry, Entry]) => Effect<A, E, R>) => Effect<Option<A>, EventJournalError | E, R>;
readonly write: <A, E, R>(options: {
readonly effect: (entry: Entry) => Effect<A, E, R>;
readonly event: string;
readonly payload: Uint8Array;
readonly primaryKey: string;
}) => Effect<A, EventJournalError | E, R>;
readonly writeFromRemote: (options: {
readonly compact?: (uncommitted: readonly Array<RemoteEntry>) => Effect<readonly Array<Entry>, EventJournalError>;
readonly effect: (options: {
readonly conflicts: readonly Array<Entry>;
readonly entry: Entry;
}) => Effect<void, EventJournalError>;
readonly entries: readonly Array<RemoteEntry>;
readonly remoteId: RemoteId;
}) => Effect<{
readonly duplicateEntries: readonly Array<Entry>;
}, EventJournalError>;
}, SqlError, SqlClient>

Layers

layer

unstable
Since v4.0.0

Provides EventJournal using the SQL-backed implementation created by make.

When to use

Use when composing a Layer graph that should provide a persistent SQL-backed EventJournal from an existing SqlClient service.

Details

The layer delegates to make(options), so the same optional entryTable and remotesTable settings are used and construction requires SqlClient and may fail with SqlError.

Gotchas

Layer construction performs the same minimal CREATE TABLE IF NOT EXISTS setup as make; manage indexes and schema migrations outside this layer when your SQL schema needs more than the built-in tables.

See

Signature

declare function layer(options?: {
readonly entryTable?: string;
readonly remotesTable?: string;
}): Layer<EventJournal, SqlError, SqlClient>