@arki/event-sourcing 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +124 -0
- package/dist/builders/command-handler.d.ts +48 -0
- package/dist/builders/command-handler.d.ts.map +1 -0
- package/dist/builders/command-handler.js +40 -0
- package/dist/builders/command-handler.js.map +1 -0
- package/dist/builders/command.d.ts +39 -0
- package/dist/builders/command.d.ts.map +1 -0
- package/dist/builders/command.js +44 -0
- package/dist/builders/command.js.map +1 -0
- package/dist/builders/decider.d.ts +188 -0
- package/dist/builders/decider.d.ts.map +1 -0
- package/dist/builders/decider.js +252 -0
- package/dist/builders/decider.js.map +1 -0
- package/dist/builders/event.d.ts +39 -0
- package/dist/builders/event.d.ts.map +1 -0
- package/dist/builders/event.js +42 -0
- package/dist/builders/event.js.map +1 -0
- package/dist/builders/index.d.ts +40 -0
- package/dist/builders/index.d.ts.map +1 -0
- package/dist/builders/index.js +40 -0
- package/dist/builders/index.js.map +1 -0
- package/dist/builders/process-manager.d.ts +94 -0
- package/dist/builders/process-manager.d.ts.map +1 -0
- package/dist/builders/process-manager.js +131 -0
- package/dist/builders/process-manager.js.map +1 -0
- package/dist/builders/projection.d.ts +92 -0
- package/dist/builders/projection.d.ts.map +1 -0
- package/dist/builders/projection.js +129 -0
- package/dist/builders/projection.js.map +1 -0
- package/dist/builders/stateful-process-manager.d.ts +181 -0
- package/dist/builders/stateful-process-manager.d.ts.map +1 -0
- package/dist/builders/stateful-process-manager.js +280 -0
- package/dist/builders/stateful-process-manager.js.map +1 -0
- package/dist/bus.d.ts +3 -0
- package/dist/bus.d.ts.map +1 -0
- package/dist/bus.js +2 -0
- package/dist/bus.js.map +1 -0
- package/dist/command-flow.d.ts +87 -0
- package/dist/command-flow.d.ts.map +1 -0
- package/dist/command-flow.js +233 -0
- package/dist/command-flow.js.map +1 -0
- package/dist/command.d.ts +38 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +2 -0
- package/dist/command.js.map +1 -0
- package/dist/debug.d.ts +31 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +31 -0
- package/dist/debug.js.map +1 -0
- package/dist/decide.d.ts +11 -0
- package/dist/decide.d.ts.map +1 -0
- package/dist/decide.js +2 -0
- package/dist/decide.js.map +1 -0
- package/dist/dot.d.ts +90 -0
- package/dist/dot.d.ts.map +1 -0
- package/dist/dot.js +91 -0
- package/dist/dot.js.map +1 -0
- package/dist/error.d.ts +2 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +2 -0
- package/dist/error.js.map +1 -0
- package/dist/event-sourcing-features.d.ts +81 -0
- package/dist/event-sourcing-features.d.ts.map +1 -0
- package/dist/event-sourcing-features.js +139 -0
- package/dist/event-sourcing-features.js.map +1 -0
- package/dist/event.d.ts +25 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +2 -0
- package/dist/event.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/package.json +5 -0
- package/dist/process-manager.d.ts +113 -0
- package/dist/process-manager.d.ts.map +1 -0
- package/dist/process-manager.js +155 -0
- package/dist/process-manager.js.map +1 -0
- package/dist/simple-process-manager.d.ts +37 -0
- package/dist/simple-process-manager.d.ts.map +1 -0
- package/dist/simple-process-manager.js +55 -0
- package/dist/simple-process-manager.js.map +1 -0
- package/dist/src/builders/command-handler.d.ts +48 -0
- package/dist/src/builders/command-handler.d.ts.map +1 -0
- package/dist/src/builders/command.d.ts +39 -0
- package/dist/src/builders/command.d.ts.map +1 -0
- package/dist/src/builders/decider.d.ts +188 -0
- package/dist/src/builders/decider.d.ts.map +1 -0
- package/dist/src/builders/event.d.ts +39 -0
- package/dist/src/builders/event.d.ts.map +1 -0
- package/dist/src/builders/index.d.ts +40 -0
- package/dist/src/builders/index.d.ts.map +1 -0
- package/dist/src/builders/process-manager.d.ts +94 -0
- package/dist/src/builders/process-manager.d.ts.map +1 -0
- package/dist/src/builders/projection.d.ts +92 -0
- package/dist/src/builders/projection.d.ts.map +1 -0
- package/dist/src/builders/stateful-process-manager.d.ts +181 -0
- package/dist/src/builders/stateful-process-manager.d.ts.map +1 -0
- package/dist/src/bus.d.ts +3 -0
- package/dist/src/bus.d.ts.map +1 -0
- package/dist/src/command-flow.d.ts +87 -0
- package/dist/src/command-flow.d.ts.map +1 -0
- package/dist/src/command.d.ts +16 -0
- package/dist/src/command.d.ts.map +1 -0
- package/dist/src/debug.d.ts +31 -0
- package/dist/src/debug.d.ts.map +1 -0
- package/dist/src/decide.d.ts +11 -0
- package/dist/src/decide.d.ts.map +1 -0
- package/dist/src/error.d.ts +2 -0
- package/dist/src/error.d.ts.map +1 -0
- package/dist/src/event.d.ts +25 -0
- package/dist/src/event.d.ts.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/process-manager.d.ts +113 -0
- package/dist/src/process-manager.d.ts.map +1 -0
- package/dist/src/store.d.ts +44 -0
- package/dist/src/store.d.ts.map +1 -0
- package/dist/src/types.d.ts +10 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/store.d.ts +44 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +94 -0
- package/dist/store.js.map +1 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +100 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type { Event as EmmettEvent } from '@event-driven-io/emmett';
|
|
2
|
+
import type { z } from '@arki/contracts';
|
|
3
|
+
import type { AnyEvent } from '../event';
|
|
4
|
+
import type { CommandHandler } from './command-handler';
|
|
5
|
+
/**
|
|
6
|
+
* Applies a domain event to the current aggregate state and produces the next state.
|
|
7
|
+
*/
|
|
8
|
+
type Evolve<State, StreamEvent extends AnyEvent> = (state: State, event: StreamEvent) => State;
|
|
9
|
+
/**
|
|
10
|
+
* Computes the domain events that should be emitted for an incoming command,
|
|
11
|
+
* based on the aggregate's current state.
|
|
12
|
+
*/
|
|
13
|
+
type Decide<State, Command, StreamEvent extends AnyEvent> = (command: Command, state: State) => StreamEvent[];
|
|
14
|
+
/**
|
|
15
|
+
* Fluent builder for aggregate behaviour (aka "decider") definitions.
|
|
16
|
+
*
|
|
17
|
+
* In many event-sourcing texts this role is called an *aggregate*: an object that owns
|
|
18
|
+
* the authoritative state, knows how to rebuild itself from events (`evolve`), and
|
|
19
|
+
* decides which new events should be recorded when commands arrive (`decide`).
|
|
20
|
+
*
|
|
21
|
+
* We keep the historical Emmett name `Decider` to match upstream types, but the builder
|
|
22
|
+
* intentionally documents the aggregate semantics so domain code can align on that
|
|
23
|
+
* mental model.
|
|
24
|
+
*
|
|
25
|
+
* Capabilities:
|
|
26
|
+
* - Type safety through TypeScript generics
|
|
27
|
+
* - Optional Zod schemas to validate state evolution at runtime
|
|
28
|
+
* - Chainable configuration that terminates with `.handler()`, returning the executed aggregate contract
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* const cartAggregate = defineDecider()
|
|
33
|
+
* .stateSchema(z.object({ items: z.array(z.string()), total: z.number() }))
|
|
34
|
+
* .initialState(() => ({ items: [], total: 0 }))
|
|
35
|
+
* .evolve((state, event) => {
|
|
36
|
+
* if (event.type === 'ItemAdded') {
|
|
37
|
+
* return {
|
|
38
|
+
* items: [...state.items, event.data.item],
|
|
39
|
+
* total: state.total + event.data.price,
|
|
40
|
+
* };
|
|
41
|
+
* }
|
|
42
|
+
* return state;
|
|
43
|
+
* })
|
|
44
|
+
* .decide((command, state) => {
|
|
45
|
+
* if (command.type === 'AddItem') {
|
|
46
|
+
* return [
|
|
47
|
+
* {
|
|
48
|
+
* type: 'ItemAdded',
|
|
49
|
+
* data: { item: command.data.item, price: command.data.price },
|
|
50
|
+
* },
|
|
51
|
+
* ];
|
|
52
|
+
* }
|
|
53
|
+
* return [];
|
|
54
|
+
* })
|
|
55
|
+
* .handler();
|
|
56
|
+
*
|
|
57
|
+
* const initial = cartAggregate.initialState();
|
|
58
|
+
* const events = cartAggregate.decide(addItemCommand, initial);
|
|
59
|
+
* const next = cartAggregate.evolve(initial, events[0]);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
declare class DeciderBuilder<State = never, Command = never, StreamEvent extends AnyEvent = never> {
|
|
63
|
+
private _stateSchema?;
|
|
64
|
+
private _initialStateFn?;
|
|
65
|
+
private _evolveFn?;
|
|
66
|
+
private _decideFn?;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a deep copy of the builder to ensure immutability
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
private clone;
|
|
72
|
+
/**
|
|
73
|
+
* Sets the state schema for state validation
|
|
74
|
+
* @param schema - Zod schema for validating state
|
|
75
|
+
* @returns A new DeciderBuilder instance with the state schema set
|
|
76
|
+
*/
|
|
77
|
+
stateSchema<TNewState>(schema: z.ZodType<TNewState>): DeciderBuilder<TNewState, Command, StreamEvent>;
|
|
78
|
+
/**
|
|
79
|
+
* Sets the initial state function
|
|
80
|
+
* @param fn - Function that returns the initial state for a new aggregate
|
|
81
|
+
* @returns A new DeciderBuilder instance with the initial state function set
|
|
82
|
+
*/
|
|
83
|
+
initialState(fn: () => State): DeciderBuilder<State, Command, StreamEvent>;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the evolve function
|
|
86
|
+
*
|
|
87
|
+
* This method is generic and will infer the StreamEvent type from the function parameter,
|
|
88
|
+
* allowing TypeScript to properly infer complex discriminated union types.
|
|
89
|
+
*
|
|
90
|
+
* @template TStreamEvent - The union type of all events (inferred from fn parameter)
|
|
91
|
+
* @param fn - Function that applies an event to state to compute the next state
|
|
92
|
+
* @returns A new DeciderBuilder instance with the evolve function and inferred event types set
|
|
93
|
+
*/
|
|
94
|
+
evolve<TStreamEvent extends AnyEvent>(fn: Evolve<State, TStreamEvent>): DeciderBuilder<State, Command, TStreamEvent>;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the decide function
|
|
97
|
+
*
|
|
98
|
+
* This method is generic and will infer the Command type from the function parameter,
|
|
99
|
+
* allowing TypeScript to properly infer complex discriminated union types.
|
|
100
|
+
*
|
|
101
|
+
* @template TCommand - The union type of all commands (inferred from fn parameter)
|
|
102
|
+
* @param fn - Function that determines which events to emit for a given command and state
|
|
103
|
+
* @returns A new DeciderBuilder instance with the decide function and inferred command types set
|
|
104
|
+
*/
|
|
105
|
+
decide<TCommand>(fn: Decide<State, TCommand, StreamEvent>): DeciderBuilder<State, TCommand, StreamEvent>;
|
|
106
|
+
/**
|
|
107
|
+
* Terminal method that creates the decider
|
|
108
|
+
*
|
|
109
|
+
* Validates that all required functions are set and returns an object
|
|
110
|
+
* with three validated functions:
|
|
111
|
+
* - initialState: Returns the validated initial state
|
|
112
|
+
* - evolve: Applies an event to state with validation
|
|
113
|
+
* - decide: Determines events to emit with validation
|
|
114
|
+
*
|
|
115
|
+
* @returns An object containing the three core decider functions
|
|
116
|
+
* @throws Error if state schema, initial state function, evolve function, or decide function are not defined
|
|
117
|
+
*/
|
|
118
|
+
handler(): {
|
|
119
|
+
initialState: () => State;
|
|
120
|
+
evolve: Evolve<State, StreamEvent>;
|
|
121
|
+
decide: Decide<State, Command, StreamEvent>;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Terminal method that creates a store-connected command handler.
|
|
125
|
+
*
|
|
126
|
+
* Unlike `.handler()`, this does **not** require a state schema — it creates
|
|
127
|
+
* a handler that can be registered directly with an event store. Internally
|
|
128
|
+
* it delegates to `defineCommandHandler`, which bridges the Emmett type
|
|
129
|
+
* constraint on command metadata.
|
|
130
|
+
*
|
|
131
|
+
* Use this when you need a handler that reads/writes events from a store
|
|
132
|
+
* (the common case for aggregates), rather than the raw decider functions.
|
|
133
|
+
*
|
|
134
|
+
* @returns A store-connected command handler function
|
|
135
|
+
* @throws Error if initial state, evolve, or decide functions are not defined
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* const handler = defineDecider()
|
|
140
|
+
* .initialState(() => null as BookState)
|
|
141
|
+
* .evolve(evolve)
|
|
142
|
+
* .decide(decide)
|
|
143
|
+
* .commandHandler();
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
commandHandler(): StreamEvent extends EmmettEvent ? CommandHandler<State, Command, StreamEvent> : never;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Factory function to create a new DeciderBuilder
|
|
150
|
+
*
|
|
151
|
+
* **Recommended API**: This builder API supports complex discriminated unions
|
|
152
|
+
* through parameter-based type inference. The `.evolve()` and `.decide()` methods
|
|
153
|
+
* are generic and will automatically infer types from your function parameters.
|
|
154
|
+
*
|
|
155
|
+
* **Use cases:**
|
|
156
|
+
* - ✅ All cases (simple and complex)
|
|
157
|
+
* - ✅ Complex discriminated unions (10+ command/event types)
|
|
158
|
+
* - ✅ Fluent, chainable API preference
|
|
159
|
+
* - ✅ Automatic type inference from function parameters
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* // Works for both simple and complex union types
|
|
164
|
+
* const decider = defineDecider()
|
|
165
|
+
* .stateSchema(myStateSchema)
|
|
166
|
+
* .initialState(() => ({ ... }))
|
|
167
|
+
* .evolve((state, event) => {
|
|
168
|
+
* // TypeScript infers event type from this function
|
|
169
|
+
* switch (event.type) {
|
|
170
|
+
* case 'EventA': return { ... };
|
|
171
|
+
* case 'EventB': return { ... };
|
|
172
|
+
* // ... handles 10+ event types correctly
|
|
173
|
+
* }
|
|
174
|
+
* })
|
|
175
|
+
* .decide((command, state) => {
|
|
176
|
+
* // TypeScript infers command type from this function
|
|
177
|
+
* switch (command.type) {
|
|
178
|
+
* case 'CommandA': return [{ type: 'EventA', ... }];
|
|
179
|
+
* case 'CommandB': return [{ type: 'EventB', ... }];
|
|
180
|
+
* // ... handles 10+ command types correctly
|
|
181
|
+
* }
|
|
182
|
+
* })
|
|
183
|
+
* .handler();
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
export declare function defineDecider(): DeciderBuilder;
|
|
187
|
+
export {};
|
|
188
|
+
//# sourceMappingURL=decider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decider.d.ts","sourceRoot":"","sources":["../../../src/builders/decider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,KAAK,EAAE,QAAQ,EAAS,MAAM,UAAU,CAAC;AAGhD,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AAEtD;;GAEG;AACH,KAAK,MAAM,CAAC,KAAK,EAAE,WAAW,SAAS,QAAQ,IAAI,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,KAAK,KAAK,CAAC;AAE/F;;;GAGG;AACH,KAAK,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,SAAS,QAAQ,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,KAAK,WAAW,EAAE,CAAC;AAE9G;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,cAAM,cAAc,CAAC,KAAK,GAAG,KAAK,EAAE,OAAO,GAAG,KAAK,EAAE,WAAW,SAAS,QAAQ,GAAG,KAAK;IACvF,OAAO,CAAC,YAAY,CAAC,CAAmB;IACxC,OAAO,CAAC,eAAe,CAAC,CAAc;IACtC,OAAO,CAAC,SAAS,CAAC,CAA6B;IAC/C,OAAO,CAAC,SAAS,CAAC,CAAsC;IAExD;;;OAGG;IACH,OAAO,CAAC,KAAK;IASb;;;;OAIG;IACH,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,WAAW,CAAC;IAUrG;;;;OAIG;IACH,YAAY,CAAC,EAAE,EAAE,MAAM,KAAK,GAAG,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC;IAO1E;;;;;;;;;OASG;IACH,MAAM,CAAC,YAAY,SAAS,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,KAAK,EAAE,YAAY,CAAC,GAAG,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,YAAY,CAAC;IAUpH;;;;;;;;;OASG;IACH,MAAM,CAAC,QAAQ,EAAE,EAAE,EAAE,MAAM,CAAC,KAAK,EAAE,QAAQ,EAAE,WAAW,CAAC,GAAG,cAAc,CAAC,KAAK,EAAE,QAAQ,EAAE,WAAW,CAAC;IAUxG;;;;;;;;;;;OAWG;IACH,OAAO,IAAI;QACT,YAAY,EAAE,MAAM,KAAK,CAAC;QAC1B,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;QACnC,MAAM,EAAE,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,CAAC;KAC7C;IAqCD;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,cAAc,IAAI,WAAW,SAAS,WAAW,GAAG,cAAc,CAAC,KAAK,EAAE,OAAO,EAAE,WAAW,CAAC,GAAG,KAAK;CAexG;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,aAAa,IAAI,cAAc,CAG9C"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { z } from '@arki/contracts';
|
|
2
|
+
import type { Event } from '../event';
|
|
3
|
+
import type { DefaultRecord } from '../types';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for defining a domain event factory.
|
|
6
|
+
*/
|
|
7
|
+
export type EventConfig<TType extends string, TData extends DefaultRecord, TMetadata extends DefaultRecord | undefined = undefined> = {
|
|
8
|
+
/** The event type string (e.g., 'UserCreated') */
|
|
9
|
+
type: TType;
|
|
10
|
+
/** Zod schema for validating event data */
|
|
11
|
+
dataSchema: z.ZodType<TData>;
|
|
12
|
+
/** Optional Zod schema for validating event metadata */
|
|
13
|
+
metadataSchema?: z.ZodType<TMetadata>;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Creates a strongly-typed event factory function.
|
|
17
|
+
*
|
|
18
|
+
* This factory validates data and metadata using Zod schemas before
|
|
19
|
+
* creating events that can be appended to Emmett's event store.
|
|
20
|
+
*
|
|
21
|
+
* @param config - Configuration object containing type, data schema, and optional metadata schema
|
|
22
|
+
* @returns A factory function that validates and creates events
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const userCreatedEvent = defineEvent({
|
|
27
|
+
* type: 'UserCreated',
|
|
28
|
+
* dataSchema: z.object({ userId: z.string().uuid(), name: z.string() }),
|
|
29
|
+
* metadataSchema: z.object({ now: z.date(), correlationId: z.string().uuid() }),
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* const event = userCreatedEvent(
|
|
33
|
+
* { userId: 'usr_123', name: 'Ada' },
|
|
34
|
+
* { now: new Date(), correlationId: 'corr_456' },
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function defineEvent<TType extends string, TData extends DefaultRecord, TMetadata extends DefaultRecord | undefined = undefined>(config: EventConfig<TType, TData, TMetadata>): (data: TData, metadata?: TMetadata) => Event<TType, TData, TMetadata>;
|
|
39
|
+
//# sourceMappingURL=event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../../src/builders/event.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C;;GAEG;AACH,MAAM,MAAM,WAAW,CACrB,KAAK,SAAS,MAAM,EACpB,KAAK,SAAS,aAAa,EAC3B,SAAS,SAAS,aAAa,GAAG,SAAS,GAAG,SAAS,IACrD;IACF,kDAAkD;IAClD,IAAI,EAAE,KAAK,CAAC;IACZ,2CAA2C;IAC3C,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC7B,wDAAwD;IACxD,cAAc,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;CACvC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,WAAW,CACzB,KAAK,SAAS,MAAM,EACpB,KAAK,SAAS,aAAa,EAC3B,SAAS,SAAS,aAAa,GAAG,SAAS,GAAG,SAAS,EACvD,MAAM,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,EAAE,SAAS,KAAK,KAAK,CAAC,KAAK,EAAE,KAAK,EAAE,SAAS,CAAC,CAmBrH"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event Sourcing Builders
|
|
3
|
+
*
|
|
4
|
+
* Fluent API builders for constructing event sourcing components with type safety.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import { defineEvent, defineDecider, defineProjection } from '@arki/event-sourcing/builders';
|
|
9
|
+
*
|
|
10
|
+
* // Define an event
|
|
11
|
+
* const userCreated = defineEvent()
|
|
12
|
+
* .type('UserCreated')
|
|
13
|
+
* .data(z.object({ userId: z.string(), email: z.string() }))
|
|
14
|
+
* .metadata(z.object({ timestamp: z.string() }))
|
|
15
|
+
* .handler(({ data, metadata }) => ({ type: 'UserCreated', data, metadata }));
|
|
16
|
+
*
|
|
17
|
+
* // Define a decider
|
|
18
|
+
* const userDecider = defineDecider()
|
|
19
|
+
* .stateSchema(userStateSchema)
|
|
20
|
+
* .initialState(() => ({ users: [] }))
|
|
21
|
+
* .evolve((state, event) => { ... })
|
|
22
|
+
* .decide((command, state) => { ... })
|
|
23
|
+
* .handler();
|
|
24
|
+
*
|
|
25
|
+
* // Define a projection
|
|
26
|
+
* const userProjection = defineProjection()
|
|
27
|
+
* .name('user-projection')
|
|
28
|
+
* .eventTypes(['UserCreated', 'UserUpdated'])
|
|
29
|
+
* .contextSchema(z.object({ repo: z.any() }))
|
|
30
|
+
* .handler(async (events, context) => { ... });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export { defineCommand } from './command';
|
|
34
|
+
export { defineCommandHandler, type CommandHandler } from './command-handler';
|
|
35
|
+
export { defineEvent } from './event';
|
|
36
|
+
export { defineDecider } from './decider';
|
|
37
|
+
export { defineProjection } from './projection';
|
|
38
|
+
export { defineProcessManager } from './process-manager';
|
|
39
|
+
export { defineStatefulProcessManager } from './stateful-process-manager';
|
|
40
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/builders/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,KAAK,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,4BAA4B,EAAE,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { z } from '@arki/contracts';
|
|
2
|
+
import type { Event } from '../event';
|
|
3
|
+
/**
|
|
4
|
+
* Type definition for CanHandle predicate
|
|
5
|
+
* Used to filter events that a process manager can handle
|
|
6
|
+
*/
|
|
7
|
+
type CanHandle<EventType extends Event> = EventType['type'][];
|
|
8
|
+
/**
|
|
9
|
+
* Fluent builder for Emmett process managers (sagas).
|
|
10
|
+
*
|
|
11
|
+
* Process managers sit outside an aggregate and react to streams of events in order to
|
|
12
|
+
* coordinate long-running workflows. This builder helps you describe that behaviour with
|
|
13
|
+
* explicit event filters, a validated execution context, and a terminal `.handler(...)`
|
|
14
|
+
* that produces the configuration object Emmett expects.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const orderSaga = defineProcessManager<OrderEvent>()
|
|
19
|
+
* .name('order-fulfillment')
|
|
20
|
+
* .eventTypes(['OrderPlaced', 'PaymentProcessed', 'OrderShipped'])
|
|
21
|
+
* .contextSchema(z.object({ messageBus: z.any(), repo: z.any() }))
|
|
22
|
+
* .handler(async (events, context) => {
|
|
23
|
+
* for (const event of events) {
|
|
24
|
+
* if (event.type === 'OrderPlaced') {
|
|
25
|
+
* await context.messageBus.send({ type: 'ProcessPayment', data: event.data });
|
|
26
|
+
* } else if (event.type === 'PaymentProcessed') {
|
|
27
|
+
* await context.messageBus.send({ type: 'ShipOrder', data: event.data });
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
declare class ProcessManagerBuilder<EventType extends Event = never, Context = never> {
|
|
34
|
+
private _name?;
|
|
35
|
+
private _eventTypes?;
|
|
36
|
+
private _contextSchema?;
|
|
37
|
+
private _handlerFn?;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a deep copy of the builder to ensure immutability
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
private clone;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the process manager name
|
|
45
|
+
* @param name - Unique identifier for the process manager
|
|
46
|
+
* @returns A new ProcessManagerBuilder instance with the name set
|
|
47
|
+
*/
|
|
48
|
+
name(name: string): ProcessManagerBuilder<EventType, Context>;
|
|
49
|
+
/**
|
|
50
|
+
* Sets the event types this process manager can handle
|
|
51
|
+
* @param types - Array of event type strings
|
|
52
|
+
* @returns A new ProcessManagerBuilder instance with the event types set
|
|
53
|
+
*/
|
|
54
|
+
eventTypes(types: EventType['type'][]): ProcessManagerBuilder<EventType, Context>;
|
|
55
|
+
/**
|
|
56
|
+
* Sets the context schema for validation
|
|
57
|
+
* @param schema - Zod schema for validating the context
|
|
58
|
+
* @returns A new ProcessManagerBuilder instance with the context schema set
|
|
59
|
+
*/
|
|
60
|
+
contextSchema<TNewContext>(schema: z.ZodType<TNewContext>): ProcessManagerBuilder<EventType, TNewContext>;
|
|
61
|
+
/**
|
|
62
|
+
* Terminal method that creates the process manager
|
|
63
|
+
*
|
|
64
|
+
* Validates that all required properties are set and returns an object
|
|
65
|
+
* with three properties:
|
|
66
|
+
* - processorId: The process manager identifier
|
|
67
|
+
* - canHandle: Array of event types to filter events
|
|
68
|
+
* - eachBatch: Function that validates context and processes event batches
|
|
69
|
+
*
|
|
70
|
+
* @param fn - Function that processes event batches to coordinate workflows
|
|
71
|
+
* @returns An object containing the process manager configuration
|
|
72
|
+
* @throws Error if name, eventTypes, or contextSchema are not defined
|
|
73
|
+
*/
|
|
74
|
+
handler(fn: (events: EventType[], context: Context) => Promise<void>): {
|
|
75
|
+
processorId: string;
|
|
76
|
+
canHandle: CanHandle<EventType>;
|
|
77
|
+
eachBatch: (events: EventType[], context: unknown) => Promise<void>;
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Factory function to create a new ProcessManagerBuilder
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const builder = defineProcessManager<MyEvent>()
|
|
86
|
+
* .name('my-process-manager')
|
|
87
|
+
* .eventTypes(['EventTypeA', 'EventTypeB'])
|
|
88
|
+
* .contextSchema(myContextSchema)
|
|
89
|
+
* .handler(async (events, context) => { ... });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export declare function defineProcessManager<EventType extends Event = never>(): ProcessManagerBuilder<EventType, never>;
|
|
93
|
+
export {};
|
|
94
|
+
//# sourceMappingURL=process-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-manager.d.ts","sourceRoot":"","sources":["../../../src/builders/process-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAGtC;;;GAGG;AACH,KAAK,SAAS,CAAC,SAAS,SAAS,KAAK,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,cAAM,qBAAqB,CAAC,SAAS,SAAS,KAAK,GAAG,KAAK,EAAE,OAAO,GAAG,KAAK;IAC1E,OAAO,CAAC,KAAK,CAAC,CAAS;IACvB,OAAO,CAAC,WAAW,CAAC,CAAsB;IAC1C,OAAO,CAAC,cAAc,CAAC,CAAqB;IAC5C,OAAO,CAAC,UAAU,CAAC,CAA2D;IAE9E;;;OAGG;IACH,OAAO,CAAC,KAAK;IASb;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,qBAAqB,CAAC,SAAS,EAAE,OAAO,CAAC;IAO7D;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,GAAG,qBAAqB,CAAC,SAAS,EAAE,OAAO,CAAC;IAOjF;;;;OAIG;IACH,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,qBAAqB,CAAC,SAAS,EAAE,WAAW,CAAC;IAUzG;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,EAAE,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG;QACrE,WAAW,EAAE,MAAM,CAAC;QACpB,SAAS,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;QAChC,SAAS,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;KACrE;CAwBF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,SAAS,KAAK,GAAG,KAAK,KAAK,qBAAqB,CAAC,SAAS,EAAE,KAAK,CAAC,CAG/G"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { z } from '@arki/contracts';
|
|
2
|
+
import type { AnyEvent } from '../event';
|
|
3
|
+
/**
|
|
4
|
+
* Type definition for CanHandle predicate
|
|
5
|
+
* Used to filter events that a projection can handle
|
|
6
|
+
*/
|
|
7
|
+
type CanHandle<EventType extends AnyEvent> = EventType['type'][];
|
|
8
|
+
/**
|
|
9
|
+
* Fluent builder for read-model projections.
|
|
10
|
+
*
|
|
11
|
+
* Projections consume event streams and populate query-side views. This builder captures
|
|
12
|
+
* the essentials—projection name, handled event types, validated context—and terminates
|
|
13
|
+
* in a `.handler(...)` call that returns the shape required by Emmett’s projection
|
|
14
|
+
* registration utilities.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const userProjection = defineProjection<UserEvent>()
|
|
19
|
+
* .name('user-projection')
|
|
20
|
+
* .eventTypes(['UserCreated', 'UserUpdated'])
|
|
21
|
+
* .contextSchema(z.object({ repo: z.any() }))
|
|
22
|
+
* .handler(async (events, context) => {
|
|
23
|
+
* for (const event of events) {
|
|
24
|
+
* if (event.type === 'UserCreated') {
|
|
25
|
+
* await context.repo.users.create(event.data);
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare class ProjectionBuilder<EventType extends AnyEvent = never, Context = never> {
|
|
32
|
+
private _name?;
|
|
33
|
+
private _eventTypes?;
|
|
34
|
+
private _contextSchema?;
|
|
35
|
+
private _handlerFn?;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a deep copy of the builder to ensure immutability
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
private clone;
|
|
41
|
+
/**
|
|
42
|
+
* Sets the projection name
|
|
43
|
+
* @param name - Unique identifier for the projection
|
|
44
|
+
* @returns A new ProjectionBuilder instance with the name set
|
|
45
|
+
*/
|
|
46
|
+
name(name: string): ProjectionBuilder<EventType, Context>;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the event types this projection can handle
|
|
49
|
+
* @param types - Array of event type strings
|
|
50
|
+
* @returns A new ProjectionBuilder instance with the event types set
|
|
51
|
+
*/
|
|
52
|
+
eventTypes(types: EventType['type'][]): ProjectionBuilder<EventType, Context>;
|
|
53
|
+
/**
|
|
54
|
+
* Sets the context schema for validation
|
|
55
|
+
* @param schema - Zod schema for validating the context
|
|
56
|
+
* @returns A new ProjectionBuilder instance with the context schema set
|
|
57
|
+
*/
|
|
58
|
+
contextSchema<TNewContext>(schema: z.ZodType<TNewContext>): ProjectionBuilder<EventType, TNewContext>;
|
|
59
|
+
/**
|
|
60
|
+
* Terminal method that creates the projection
|
|
61
|
+
*
|
|
62
|
+
* Validates that all required properties are set and returns an object
|
|
63
|
+
* with three properties:
|
|
64
|
+
* - name: The projection name
|
|
65
|
+
* - canHandle: Array of event types to filter events
|
|
66
|
+
* - handle: Function that validates context and processes events
|
|
67
|
+
*
|
|
68
|
+
* @param fn - Function that processes events and updates read models
|
|
69
|
+
* @returns An object containing the projection configuration
|
|
70
|
+
* @throws Error if name, eventTypes, or contextSchema are not defined
|
|
71
|
+
*/
|
|
72
|
+
handler(fn: (events: EventType[], context: Context) => Promise<void>): {
|
|
73
|
+
name: string;
|
|
74
|
+
canHandle: CanHandle<EventType>;
|
|
75
|
+
handle: (events: EventType[], context: unknown) => Promise<void>;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Factory function to create a new ProjectionBuilder
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const builder = defineProjection<MyEvent>()
|
|
84
|
+
* .name('my-projection')
|
|
85
|
+
* .eventTypes(['EventTypeA', 'EventTypeB'])
|
|
86
|
+
* .contextSchema(myContextSchema)
|
|
87
|
+
* .handler(async (events, context) => { ... });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export declare function defineProjection<EventType extends AnyEvent = never>(): ProjectionBuilder<EventType, never>;
|
|
91
|
+
export {};
|
|
92
|
+
//# sourceMappingURL=projection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../../../src/builders/projection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGzC;;;GAGG;AACH,KAAK,SAAS,CAAC,SAAS,SAAS,QAAQ,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,cAAM,iBAAiB,CAAC,SAAS,SAAS,QAAQ,GAAG,KAAK,EAAE,OAAO,GAAG,KAAK;IACzE,OAAO,CAAC,KAAK,CAAC,CAAS;IACvB,OAAO,CAAC,WAAW,CAAC,CAAsB;IAC1C,OAAO,CAAC,cAAc,CAAC,CAAqB;IAC5C,OAAO,CAAC,UAAU,CAAC,CAA2D;IAE9E;;;OAGG;IACH,OAAO,CAAC,KAAK;IASb;;;;OAIG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,iBAAiB,CAAC,SAAS,EAAE,OAAO,CAAC;IAOzD;;;;OAIG;IACH,UAAU,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EAAE,GAAG,iBAAiB,CAAC,SAAS,EAAE,OAAO,CAAC;IAO7E;;;;OAIG;IACH,aAAa,CAAC,WAAW,EAAE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,iBAAiB,CAAC,SAAS,EAAE,WAAW,CAAC;IAUrG;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,EAAE,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG;QACrE,IAAI,EAAE,MAAM,CAAC;QACb,SAAS,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;QAChC,MAAM,EAAE,CAAC,MAAM,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;KAClE;CAwBF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,SAAS,QAAQ,GAAG,KAAK,KAAK,iBAAiB,CAAC,SAAS,EAAE,KAAK,CAAC,CAG1G"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import type { z } from '@arki/contracts';
|
|
2
|
+
import type { Event } from '../event';
|
|
3
|
+
/**
|
|
4
|
+
* Type definition for CanHandle predicate
|
|
5
|
+
* Used to filter events that a stateful process manager can handle
|
|
6
|
+
*/
|
|
7
|
+
type CanHandle<EventType extends Event> = EventType['type'][];
|
|
8
|
+
/**
|
|
9
|
+
* StatefulProcessManagerBuilder - Fluent API for building type-safe stateful process managers (sagas)
|
|
10
|
+
*
|
|
11
|
+
* Provides an immutable builder pattern for creating stateful process managers with:
|
|
12
|
+
* - Type safety through TypeScript generics
|
|
13
|
+
* - Runtime validation through Zod schemas
|
|
14
|
+
* - ORPC-style API with .handler() as terminal method
|
|
15
|
+
* - Persistent state management with load/save operations
|
|
16
|
+
*
|
|
17
|
+
* A Stateful Process Manager (Saga) maintains state across events to coordinate complex,
|
|
18
|
+
* long-running business processes by:
|
|
19
|
+
* - name: Unique identifier for the stateful process manager
|
|
20
|
+
* - eventTypes: Array of event types this process manager handles
|
|
21
|
+
* - stateSchema: Schema for validating process manager state
|
|
22
|
+
* - contextSchema: Schema for validating execution context
|
|
23
|
+
* - getStateId: Function to extract state identifier from events
|
|
24
|
+
* - loadState: Function to load existing state from storage
|
|
25
|
+
* - saveState: Function to persist state to storage
|
|
26
|
+
* - handler: Function that processes individual events and returns updated state
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* type OrderState = {
|
|
31
|
+
* orderId: string;
|
|
32
|
+
* status: 'pending' | 'paid' | 'shipped' | 'completed';
|
|
33
|
+
* paymentId?: string;
|
|
34
|
+
* trackingId?: string;
|
|
35
|
+
* };
|
|
36
|
+
*
|
|
37
|
+
* const orderProcessManager = defineStatefulProcessManager<OrderEvent, OrderState>()
|
|
38
|
+
* .name('order-fulfillment')
|
|
39
|
+
* .eventTypes(['OrderPlaced', 'PaymentProcessed', 'OrderShipped'])
|
|
40
|
+
* .stateSchema(z.object({
|
|
41
|
+
* orderId: z.string(),
|
|
42
|
+
* status: z.enum(['pending', 'paid', 'shipped', 'completed']),
|
|
43
|
+
* paymentId: z.string().optional(),
|
|
44
|
+
* trackingId: z.string().optional(),
|
|
45
|
+
* }))
|
|
46
|
+
* .contextSchema(z.object({ messageBus: z.any(), repo: z.any() }))
|
|
47
|
+
* .getStateId(event => `order-${event.data.orderId}`)
|
|
48
|
+
* .loadState(async (stateId, context) => {
|
|
49
|
+
* return await context.repo.orderSagas.findById(stateId);
|
|
50
|
+
* })
|
|
51
|
+
* .saveState(async (stateId, state, context) => {
|
|
52
|
+
* await context.repo.orderSagas.upsert({ id: stateId, ...state });
|
|
53
|
+
* })
|
|
54
|
+
* .handler(async (event, state, context) => {
|
|
55
|
+
* if (event.type === 'OrderPlaced') {
|
|
56
|
+
* // Initialize state for new order
|
|
57
|
+
* return {
|
|
58
|
+
* orderId: event.data.orderId,
|
|
59
|
+
* status: 'pending',
|
|
60
|
+
* };
|
|
61
|
+
* } else if (event.type === 'PaymentProcessed') {
|
|
62
|
+
* // Update state after payment
|
|
63
|
+
* return {
|
|
64
|
+
* ...state!,
|
|
65
|
+
* status: 'paid',
|
|
66
|
+
* paymentId: event.data.paymentId,
|
|
67
|
+
* };
|
|
68
|
+
* } else if (event.type === 'OrderShipped') {
|
|
69
|
+
* // Update state after shipping
|
|
70
|
+
* return {
|
|
71
|
+
* ...state!,
|
|
72
|
+
* status: 'shipped',
|
|
73
|
+
* trackingId: event.data.trackingId,
|
|
74
|
+
* };
|
|
75
|
+
* }
|
|
76
|
+
* return state!;
|
|
77
|
+
* });
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
declare class StatefulProcessManagerBuilder<EventType extends Event = never, State = never, Context = never> {
|
|
81
|
+
private _name?;
|
|
82
|
+
private _eventTypes?;
|
|
83
|
+
private _stateSchema?;
|
|
84
|
+
private _contextSchema?;
|
|
85
|
+
private _getStateIdFn?;
|
|
86
|
+
private _loadStateFn?;
|
|
87
|
+
private _saveStateFn?;
|
|
88
|
+
private _handlerFn?;
|
|
89
|
+
/**
|
|
90
|
+
* Creates a deep copy of the builder to ensure immutability
|
|
91
|
+
* @private
|
|
92
|
+
*/
|
|
93
|
+
private clone;
|
|
94
|
+
/**
|
|
95
|
+
* Sets the stateful process manager name
|
|
96
|
+
* @param name - Unique identifier for the stateful process manager
|
|
97
|
+
* @returns A new StatefulProcessManagerBuilder instance with the name set
|
|
98
|
+
*/
|
|
99
|
+
name(name: string): StatefulProcessManagerBuilder<EventType, State, Context>;
|
|
100
|
+
/**
|
|
101
|
+
* Sets the event types this stateful process manager can handle
|
|
102
|
+
* @param types - Array of event type strings
|
|
103
|
+
* @returns A new StatefulProcessManagerBuilder instance with the event types set
|
|
104
|
+
*/
|
|
105
|
+
eventTypes(types: EventType['type'][]): StatefulProcessManagerBuilder<EventType, State, Context>;
|
|
106
|
+
/**
|
|
107
|
+
* Sets the state schema for validation
|
|
108
|
+
* @param schema - Zod schema for validating the stateful process manager state
|
|
109
|
+
* @returns A new StatefulProcessManagerBuilder instance with the state schema set
|
|
110
|
+
*/
|
|
111
|
+
stateSchema<TNewState>(schema: z.ZodType<TNewState>): StatefulProcessManagerBuilder<EventType, TNewState, Context>;
|
|
112
|
+
/**
|
|
113
|
+
* Sets the context schema for validation
|
|
114
|
+
* @param schema - Zod schema for validating the execution context
|
|
115
|
+
* @returns A new StatefulProcessManagerBuilder instance with the context schema set
|
|
116
|
+
*/
|
|
117
|
+
contextSchema<TNewContext>(schema: z.ZodType<TNewContext>): StatefulProcessManagerBuilder<EventType, State, TNewContext>;
|
|
118
|
+
/**
|
|
119
|
+
* Sets the function to extract state identifier from events
|
|
120
|
+
* @param fn - Function that extracts a unique state identifier from an event
|
|
121
|
+
* @returns A new StatefulProcessManagerBuilder instance with the getStateId function set
|
|
122
|
+
*/
|
|
123
|
+
getStateId(fn: (event: EventType) => string): StatefulProcessManagerBuilder<EventType, State, Context>;
|
|
124
|
+
/**
|
|
125
|
+
* Sets the function to load existing state from storage
|
|
126
|
+
* @param fn - Function that loads state by identifier, returns null if state doesn't exist
|
|
127
|
+
* @returns A new StatefulProcessManagerBuilder instance with the loadState function set
|
|
128
|
+
*/
|
|
129
|
+
loadState(fn: (stateId: string, context: Context) => Promise<State | null>): StatefulProcessManagerBuilder<EventType, State, Context>;
|
|
130
|
+
/**
|
|
131
|
+
* Sets the function to persist state to storage
|
|
132
|
+
* @param fn - Function that saves state by identifier
|
|
133
|
+
* @returns A new StatefulProcessManagerBuilder instance with the saveState function set
|
|
134
|
+
*/
|
|
135
|
+
saveState(fn: (stateId: string, state: State, context: Context) => Promise<void>): StatefulProcessManagerBuilder<EventType, State, Context>;
|
|
136
|
+
/**
|
|
137
|
+
* Terminal method that creates the stateful process manager
|
|
138
|
+
*
|
|
139
|
+
* Validates that all required configuration is set and returns an object
|
|
140
|
+
* with three properties:
|
|
141
|
+
* - processorId: The stateful process manager identifier
|
|
142
|
+
* - canHandle: Array of event types to filter events
|
|
143
|
+
* - eachBatch: Function that validates context, loads state for each event,
|
|
144
|
+
* processes it through the handler, and saves the updated state
|
|
145
|
+
*
|
|
146
|
+
* @param fn - Function that processes individual events with their state and returns updated state
|
|
147
|
+
* @returns An object containing the stateful process manager configuration
|
|
148
|
+
* @throws Error if name, eventTypes, stateSchema, contextSchema, getStateId, loadState, or saveState are not defined
|
|
149
|
+
*/
|
|
150
|
+
handler(fn: (event: EventType, state: State | null, context: Context) => Promise<State>): {
|
|
151
|
+
processorId: string;
|
|
152
|
+
canHandle: CanHandle<EventType>;
|
|
153
|
+
eachBatch: (events: EventType[], context: unknown) => Promise<void>;
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Factory function to create a new StatefulProcessManagerBuilder
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const builder = defineStatefulProcessManager<MyEvent, MyState>()
|
|
162
|
+
* .name('my-stateful-process-manager')
|
|
163
|
+
* .eventTypes(['EventTypeA', 'EventTypeB'])
|
|
164
|
+
* .stateSchema(myStateSchema)
|
|
165
|
+
* .contextSchema(myContextSchema)
|
|
166
|
+
* .getStateId(event => `state-${event.data.id}`)
|
|
167
|
+
* .loadState(async (stateId, context) => {
|
|
168
|
+
* return await context.repo.findById(stateId);
|
|
169
|
+
* })
|
|
170
|
+
* .saveState(async (stateId, state, context) => {
|
|
171
|
+
* await context.repo.upsert({ id: stateId, ...state });
|
|
172
|
+
* })
|
|
173
|
+
* .handler(async (event, state, context) => {
|
|
174
|
+
* // Process event and return updated state
|
|
175
|
+
* return { ...state, updated: true };
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*/
|
|
179
|
+
export declare function defineStatefulProcessManager<EventType extends Event = never, State = never>(): StatefulProcessManagerBuilder<EventType, State, never>;
|
|
180
|
+
export {};
|
|
181
|
+
//# sourceMappingURL=stateful-process-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stateful-process-manager.d.ts","sourceRoot":"","sources":["../../../src/builders/stateful-process-manager.ts"],"names":[],"mappings":"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"}
|