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

EventStore.ts

139 lines4.8 KB

1import {
2 CommandId,
3 OrchestrationV2DomainEvent,
4 OrchestrationV2StoredEvent,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10import * as Schema from "effect/Schema";
11import * as Stream from "effect/Stream";
12import type * as SqlClient from "effect/unstable/sql/SqlClient";
13​
14import { OrchestrationEventStoreLive } from "../persistence/Layers/OrchestrationEventStore.ts";
15import { OrchestrationEventStore } from "../persistence/Services/OrchestrationEventStore.ts";
16​
17export class EventStoreAppendEventsError extends Schema.TaggedError<EventStoreAppendEventsError>()(
18 "EventStoreAppendEventsError",
19 {
20 eventCount: Schema.Number,
21 cause: Schema.optional(Schema.Defect()),
22 },
23) {
24 override get message(): string {
25 return `Failed to append ${this.eventCount} agent orchestration event(s).`;
26 }
28​
29export class EventStoreReadEventsError extends Schema.TaggedError<EventStoreReadEventsError>()(
30 "EventStoreReadEventsError",
31 {
32 afterSequence: Schema.optional(Schema.Number),
33 threadId: Schema.optional(ThreadId),
34 cause: Schema.optional(Schema.Defect()),
35 },
36) {
37 override get message(): string {
38 return this.threadId === undefined
39 ? "Failed to read agent orchestration events."
40 : `Failed to read agent orchestration events for thread ${this.threadId}.`;
41 }
43​
44export const EventStoreV2Error = Schema.Union([
45 EventStoreAppendEventsError,
46 EventStoreReadEventsError,
47]);
48export type EventStoreV2Error = typeof EventStoreV2Error.Type;
49​
50export interface EventStoreV2Shape {
51 readonly append: (input: {
52 readonly commandId?: CommandId;
53 readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
54 }) => Effect.Effect<ReadonlyArray<OrchestrationV2StoredEvent>, EventStoreV2Error>;
55 readonly read: (input?: {
56 readonly afterSequence?: number;
57 readonly throughSequence?: number;
58 readonly threadId?: ThreadId;
59 readonly limit?: number;
60 }) => Stream.Stream<OrchestrationV2StoredEvent, EventStoreV2Error>;
61 readonly readByCommandId: (input: {
62 readonly commandId: CommandId;
63 }) => Stream.Stream<OrchestrationV2StoredEvent, EventStoreV2Error>;
64 readonly latestSequence: (input?: {
65 readonly threadId?: ThreadId;
66 }) => Effect.Effect<number, EventStoreV2Error>;
67 readonly publishCommitted: (
68 events: ReadonlyArray<OrchestrationV2StoredEvent>,
69 ) => Effect.Effect<void>;
71​
72export class EventStoreV2 extends Context.Service<EventStoreV2, EventStoreV2Shape>()(
73 "t3/orchestration-v2/EventStore/EventStoreV2",
74) {}
75​
76const baseLayer: Layer.Layer<EventStoreV2, never, OrchestrationEventStore> = Layer.effect(
77 EventStoreV2,
78 Effect.gen(function* () {
79 const applicationEvents = yield* OrchestrationEventStore;
80​
81 const read: EventStoreV2Shape["read"] = (input) =>
82 applicationEvents
83 .readAgentEvents({
84 ...(input?.afterSequence === undefined ? {} : { afterSequence: input.afterSequence }),
85 ...(input?.throughSequence === undefined
86 ? {}
87 : { throughSequence: input.throughSequence }),
88 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
89 ...(input?.limit === undefined ? {} : { limit: input.limit }),
90 })
91 .pipe(
92 Stream.mapError(
93 (cause) =>
94 new EventStoreReadEventsError({
95 ...(input?.afterSequence === undefined
96 ? {}
97 : { afterSequence: input.afterSequence }),
98 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
99 cause,
100 }),
101 ),
102 );
103​
104 return EventStoreV2.of({
105 append: (input) =>
106 applicationEvents.appendAgentEvents(input).pipe(
107 Effect.mapError(
108 (cause) =>
109 new EventStoreAppendEventsError({
110 eventCount: input.events.length,
111 cause,
112 }),
113 ),
114 ),
115 read,
116 readByCommandId: ({ commandId }) =>
117 applicationEvents
118 .readAgentEvents({ commandId })
119 .pipe(Stream.mapError((cause) => new EventStoreReadEventsError({ cause }))),
120 latestSequence: (input) =>
121 applicationEvents.latestAgentSequence(input?.threadId).pipe(
122 Effect.mapError(
123 (cause) =>
124 new EventStoreReadEventsError({
125 ...(input?.threadId === undefined ? {} : { threadId: input.threadId }),
126 cause,
127 }),
128 ),
129 ),
130 publishCommitted: applicationEvents.publishCommitted,
131 });
132 }),
133);
134​
135export const layer: Layer.Layer<EventStoreV2, never, SqlClient.SqlClient> = baseLayer.pipe(
136 Layer.provide(OrchestrationEventStoreLive),
137);
138​
139export const layerFromOrchestrationEventStore = baseLayer;