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

OrchestrationEventStore.ts

112 lines3.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​
19export interface OrchestrationAggregateReplayRange {
20 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
21 readonly aggregateId: string;
22 readonly fromSequenceExclusive: number;
23 readonly toSequenceInclusive: number;
25​
26export interface OrchestrationAggregateReplayStats {
27 readonly eventCount: number;
28 readonly payloadBytes: number;
29 /** A creation in this range does not prove that the aggregate still exists. */
30 readonly hasCreateEvent: boolean;
32​
33/**
34 * OrchestrationEventStoreShape - Service API for orchestration event persistence.
35 */
36export interface OrchestrationEventStoreShape {
37 /**
38 * Persist a new orchestration event.
39 *
40 * @param event - Event payload without sequence (assigned by storage).
41 * @returns Effect containing the stored event with assigned sequence.
42 *
43 * Actor kind is inferred from command/metadata before persistence.
44 */
45 readonly append: (
46 event: Omit<OrchestrationEvent, "sequence">,
47 ) => Effect.Effect<OrchestrationEvent, OrchestrationEventStoreError>;
48​
49 /**
50 * Replay events after the provided sequence.
51 *
52 * @param sequenceExclusive - Sequence cursor (exclusive).
53 * @param limit - Maximum number of events to emit.
54 * @returns Stream containing ordered events.
55 *
56 * Reads in fixed-size pages and normalizes non-integer/negative limits.
57 */
58 readonly readFromSequence: (
59 sequenceExclusive: number,
60 limit?: number,
61 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
62​
63 /** Read one aggregate through a captured global head, without decoding other streams. */
64 readonly readAggregateRange: (
65 input: OrchestrationAggregateReplayRange & { readonly limit?: number },
66 ) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
67​
68 /**
69 * Measure at most maxEvents + 1 rows without decoding payloads. The extra
70 * row tells the caller to use a snapshot instead of a truncated replay.
71 */
72 readonly getAggregateReplayStats: (
73 input: OrchestrationAggregateReplayRange & { readonly maxEvents: number },
74 ) => Effect.Effect<OrchestrationAggregateReplayStats, OrchestrationEventStoreError>;
75​
76 /**
77 * Read all events from the beginning of the stream.
78 *
79 * @returns Stream containing all stored events.
80 */
81 readonly readAll: () => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
82​
83 /**
84 * Check whether an aggregate has an event after a sequence, optionally
85 * restricted to one event type.
86 *
87 * Used during replay to tell whether a later event supersedes the one being
88 * applied, without streaming the rest of the log.
89 */
90 readonly hasEventAfter: (input: {
91 readonly aggregateKind: OrchestrationEvent["aggregateKind"];
92 readonly aggregateId: string;
93 readonly type?: OrchestrationEvent["type"];
94 readonly sequenceExclusive: number;
95 }) => Effect.Effect<boolean, OrchestrationEventStoreError>;
97​
98/**
99 * OrchestrationEventStore - Service tag for orchestration event persistence.
100 *
101 * @example
102 * ```ts
103 * const program = Effect.gen(function* () {
104 * const events = yield* OrchestrationEventStore
105 * return yield* Stream.runCollect(events.readAll())
106 * })
107 * ```
108 */
109export class OrchestrationEventStore extends Context.Service<
110 OrchestrationEventStore,
111 OrchestrationEventStoreShape
112>()("t3/persistence/Services/OrchestrationEventStore") {}