handleCommand
API reference for handleCommand
Function: handleCommand()
Section titled “Function: handleCommand()”handleCommand<
State
,TEvent
>(eventStore
,streamId
,handler
,options
):Promise
<CommandHandlerResult
<State
,TEvent
>>
Creates a command handler without Decider pattern
Steps:
- Aggregate the stream to get current state
- Run business logic using Handler function
- Append events to stream
- Return result with new state and events
Type Parameters
Section titled “Type Parameters”State
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
handler
Section titled “handler”Handler
<State
, TEvent
>
The handler function that processes state and returns events
options
Section titled “options”CommandHandlerOptions
<State
, TEvent
>
Command handler options including evolve, initialState, etc.
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 handleCommand( eventStore, 'user-123', (state) => [{ type: 'UserRegistered', data: { userId: '123' } }], { evolve: (state, event) => ({ ...state, registered: true }), initialState: () => ({ registered: false }) });