Skip to content

handleCommand

API reference for handleCommand

@delta-base/toolkit


handleCommand<State, TEvent>(eventStore, streamId, handler, options): Promise<CommandHandlerResult<State, TEvent>>

Creates a command handler without Decider pattern

Steps:

  1. Aggregate the stream to get current state
  2. Run business logic using Handler function
  3. Append events to stream
  4. Return result with new state and events

State

TEvent extends Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }> = Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }>

EventStore

The event store instance

string

The stream identifier

Handler<State, TEvent>

The handler function that processes state and returns events

CommandHandlerOptions<State, TEvent>

Command handler options including evolve, initialState, etc.

Promise<CommandHandlerResult<State, TEvent>>

Promise resolving to command handler result with new state and events

const result = await handleCommand(
eventStore,
'user-123',
(state) => [{ type: 'UserRegistered', data: { userId: '123' } }],
{
evolve: (state, event) => ({ ...state, registered: true }),
initialState: () => ({ registered: false })
}
);