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

OrchestrationEventStore.ts

70 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 `ServiceMap.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 { ServiceMap } from "effect";
14import type { Effect, Stream } from "effect";
15​
16import type { OrchestrationEventStoreError } from "../Errors.ts";
17​
18/**
19 * OrchestrationEventStoreShape - Service API for orchestration event persistence.
20 */
21export interface OrchestrationEventStoreShape {
22 /**
23 * Persist a new orchestration event.
24 *
25 * @param event - Event payload without sequence (assigned by storage).
26 * @returns Effect containing the stored event with assigned sequence.
27 *
28 * Actor kind is inferred from command/metadata before persistence.
29 */
30 readonly append: (
31 event: Omit<OrchestrationEvent, "sequence">,
32 ) => Effect.Effect<OrchestrationEvent, OrchestrationEventStoreError>;
33​
34 /**
35 * Replay events after the provided sequence.
36 *
37 * @param sequenceExclusive - Sequence cursor (exclusive).
38 * @param limit - Maximum number of events to emit.
39 * @returns Stream containing ordered events.
40 *
41 * Reads in fixed-size pages and normalizes non-integer/negative limits.
42 */
43 readonly readFromSequence: (
44 sequenceExclusive: number,
45 limit?: number,
46 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
47​
48 /**
49 * Read all events from the beginning of the stream.
50 *
51 * @returns Stream containing all stored events.
52 */
53 readonly readAll: () => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
55​
56/**
57 * OrchestrationEventStore - Service tag for orchestration event persistence.
58 *
59 * @example
60 * ```ts
61 * const program = Effect.gen(function* () {
62 * const events = yield* OrchestrationEventStore
63 * return yield* Stream.runCollect(events.readAll())
64 * })
65 * ```
66 */
67export class OrchestrationEventStore extends ServiceMap.Service<
68 OrchestrationEventStore,
69 OrchestrationEventStoreShape
70>()("t3/persistence/Services/OrchestrationEventStore") {}