handleCommandWithDecider
API reference for handleCommandWithDecider
Function: handleCommandWithDecider()
Section titled “Function: handleCommandWithDecider()”handleCommandWithDecider<
State,TCommand,TEvent>(eventStore,streamId,command,decider,options?):Promise<CommandHandlerResult<State,TEvent>>
Creates a command handler with Decider pattern
Steps:
- Aggregate the stream to get current state
- Run business logic using Decider.decide function
- Append events to stream
- Return result with new state and events
Type Parameters
Section titled “Type Parameters”State
TCommand
Section titled “TCommand”TCommand extends Command
TEvent
Section titled “TEvent”TEvent extends Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }> = Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }>
Parameters
Section titled “Parameters”eventStore
Section titled “eventStore”The event store instance
streamId
Section titled “streamId”string
The stream identifier
command
Section titled “command”TCommand
The command to handle
decider
Section titled “decider”Decider<State, TCommand, TEvent>
The decider implementation with decide, evolve, and initialState
options?
Section titled “options?”Optional command handler options
Returns
Section titled “Returns”Promise<CommandHandlerResult<State, TEvent>>
Promise resolving to command handler result with new state and events
Example
Section titled “Example”const result = await handleCommandWithDecider( eventStore, 'user-123', { type: 'RegisterUser', data: { userId: '123' } }, { decide: (state, cmd) => [{ type: 'UserRegistered', data: cmd.data }], evolve: (state, event) => ({ ...state, registered: true }), initialState: () => ({ registered: false }) });