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

OrchestrationEventStore.ts

85 lines2.9 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>;
55​
56 /**
57 * Check whether an aggregate has an event after a sequence, optionally
58 * restricted to one event type.
59 *
60 * Used during replay to tell whether a later event supersedes the one being
61 * applied, without streaming the rest of the log.
62 */
63 readonly hasEventAfter: (input: {
64 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
65 readonly aggregateId: string;
66 readonly type?: OrchestrationEvent["type"];
67 readonly sequenceExclusive: number;
68 }) => Effect.Effect<boolean, OrchestrationEventStoreError>;
70​
71/**
72 * OrchestrationEventStore - Service tag for orchestration event persistence.
73 *
74 * @example
75 * ```ts
76 * const program = Effect.gen(function* () {
77 * const events = yield* OrchestrationEventStore
78 * return yield* Stream.runCollect(events.readAll())
79 * })
80 * ```
81 */
82export class OrchestrationEventStore extends Context.Service<
83 OrchestrationEventStore,
84 OrchestrationEventStoreShape
85>()("t3/persistence/Services/OrchestrationEventStore") {}