buildDecisionModel
API reference for buildDecisionModel
Function: buildDecisionModel()
Section titled “Function: buildDecisionModel()”buildDecisionModel<
State,EventType>(eventStore,options):Promise<DecisionModelResult<State,EventType>>
Build a decision model by reading events via a DCB query and folding them into state. Returns the state and a pre-built append condition for use with EventStore.append().
This is the primary pattern for command handling with Dynamic Consistency Boundaries (DCB). The query filters events by type and tags, and the append condition ensures no conflicting events were appended between the read and the write.
Type Parameters
Section titled “Type Parameters”State
EventType
Section titled “EventType”EventType extends Readonly<{ data: EventData; metadata?: PlatformEventMetadata; tags?: string[]; type: string; }> = Readonly<{ data: EventData; metadata?: PlatformEventMetadata; tags?: string[]; type: string; }>
Parameters
Section titled “Parameters”eventStore
Section titled “eventStore”An event store that supports readByQuery (DCB)
options
Section titled “options”BuildDecisionModelOptions<State, EventType>
Query, initial state, and evolve function
Returns
Section titled “Returns”Promise<DecisionModelResult<State, EventType>>
The decision model state and an append condition
Example
Section titled “Example”const { state, appendCondition } = await buildDecisionModel( eventStore, { query: { items: [{ types: ['CourseDefined', 'CourseArchived'], tags: [`course:${courseId}`], }], }, initialState: () => ({ exists: false }), evolve: (state, event) => { switch (event.type) { case 'CourseDefined': return { exists: true }; case 'CourseArchived': return { exists: false }; default: return state; } }, });
if (!state.exists) throw new Error('Course not found');
await eventStore.append( [{ type: 'CourseCapacityChanged', data: { newCapacity: 30 }, tags: [`course:${courseId}`] }], appendCondition);