Skip to content

buildDecisionModel

API reference for buildDecisionModel

@delta-base/toolkit


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.

State

EventType extends Readonly<{ data: EventData; metadata?: PlatformEventMetadata; tags?: string[]; type: string; }> = Readonly<{ data: EventData; metadata?: PlatformEventMetadata; tags?: string[]; type: string; }>

EventStore

An event store that supports readByQuery (DCB)

BuildDecisionModelOptions<State, EventType>

Query, initial state, and evolve function

Promise<DecisionModelResult<State, EventType>>

The decision model state and an append condition

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
);