evoq_state behaviour (evoq v1.14.1)

View Source

State behaviour for evoq aggregates.

The state module is the "default read model" — it owns the aggregate's state record, knows how to create initial state, apply events to update state, and serialize/deserialize state for feedback and snapshots.

Every aggregate MUST have a corresponding state module. The aggregate declares it via the state_module/0 callback, and delegates state operations to it:

%% In the aggregate: state_module() -> venture_state. init(AggregateId) -> {ok, venture_state:new(AggregateId)}. apply(State, Event) -> venture_state:apply_event(State, Event).

This separation keeps the aggregate focused on command validation and business rules, while the state module owns the data shape, field access, event folding, and serialization.

Required Callbacks

- new(AggregateId) -> State - apply_event(State, Event) -> State - to_map(State) -> map()

Optional Callbacks

- from_map(Map) -> {ok, State} | {error, Reason}

Summary

Callbacks

apply_event/2

-callback apply_event(State :: term(), Event :: map()) -> State :: term().

from_map/1

(optional)
-callback from_map(Map :: map()) -> {ok, State :: term()} | {error, Reason :: term()}.

new/1

-callback new(AggregateId :: binary()) -> State :: term().

to_map/1

-callback to_map(State :: term()) -> map().