Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OrchestrationEventStore.ts

71 lines2.3 KB

1/**
2 * OrchestrationEventStore - Event store interface for orchestration events.
3 *
4 * Owns durable append/replay access to the orchestration event stream. It does
5 * not reduce events into read models or apply command validation rules.
6 *
7 * Uses Effect `Context.Service` for dependency injection and exposes typed
8 * persistence/decode errors for event append and replay operations.
9 *
10 * @module OrchestrationEventStore
11 */
12import { OrchestrationEvent } from "@t3tools/contracts";
13import * as Context from "effect/Context";
14import type * as Effect from "effect/Effect";
15import type * as Stream from "effect/Stream";
16​
17import type { OrchestrationEventStoreError } from "../Errors.ts";
18​
19/**
20 * OrchestrationEventStoreShape - Service API for orchestration event persistence.
21 */
22export interface OrchestrationEventStoreShape {
23 /**
24 * Persist a new orchestration event.
25 *
26 * @param event - Event payload without sequence (assigned by storage).
27 * @returns Effect containing the stored event with assigned sequence.
28 *
29 * Actor kind is inferred from command/metadata before persistence.
30 */
31 readonly append: (
32 event: Omit<OrchestrationEvent, "sequence">,
33 ) => Effect.Effect<OrchestrationEvent, OrchestrationEventStoreError>;
34​
35 /**
36 * Replay events after the provided sequence.
37 *
38 * @param sequenceExclusive - Sequence cursor (exclusive).
39 * @param limit - Maximum number of events to emit.
40 * @returns Stream containing ordered events.
41 *
42 * Reads in fixed-size pages and normalizes non-integer/negative limits.
43 */
44 readonly readFromSequence: (
45 sequenceExclusive: number,
46 limit?: number,
47 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
48​
49 /**
50 * Read all events from the beginning of the stream.
51 *
52 * @returns Stream containing all stored events.
53 */
54 readonly readAll: () => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
56​
57/**
58 * OrchestrationEventStore - Service tag for orchestration event persistence.
59 *
60 * @example
61 * ```ts
62 * const program = Effect.gen(function* () {
63 * const events = yield* OrchestrationEventStore
64 * return yield* Stream.runCollect(events.readAll())
65 * })
66 * ```
67 */
68export class OrchestrationEventStore extends Context.Service<
69 OrchestrationEventStore,
70 OrchestrationEventStoreShape
71>()("t3/persistence/Services/OrchestrationEventStore") {}