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

OrchestrationEventStore.ts

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