evoq_aggregate behaviour (evoq v1.14.1)
View SourceAggregate behavior and GenServer implementation.
Aggregates are the consistency boundary in event sourcing. Each aggregate: - Has a unique stream ID - Declares a state module via state_module/0 - Processes commands via execute/2 callback - Applies events via apply/2 callback (typically delegates to state module) - Supports snapshots for fast recovery - Has configurable lifespan (TTL, hibernate, passivate)
Callbacks
Required: - state_module() -> module() - init(AggregateId) -> {ok, State} - execute(State, Command) -> {ok, [Event]} | {error, Reason} - apply(State, Event) -> NewState
Optional: - snapshot(State) -> SnapshotData - from_snapshot(SnapshotData) -> State
Summary
Functions
Execute a command against an aggregate.
Execute a command and return the post-event aggregate state.
Get the current state of an aggregate (for debugging).
Get the current version of an aggregate.
Start an aggregate process (uses env store_id).
Start an aggregate process with explicit store_id.
Callbacks
Functions
-spec execute_command(pid(), #evoq_command{command_id :: binary() | undefined, command_type :: atom() | undefined, aggregate_type :: atom() | undefined, aggregate_id :: binary() | undefined, payload :: map(), metadata :: map(), causation_id :: binary() | undefined, correlation_id :: binary() | undefined, idempotency_key :: binary() | undefined}) -> {ok, non_neg_integer(), [map()]} | {error, term()}.
Execute a command against an aggregate.
-spec execute_command_with_state(pid(), #evoq_command{command_id :: binary() | undefined, command_type :: atom() | undefined, aggregate_type :: atom() | undefined, aggregate_id :: binary() | undefined, payload :: map(), metadata :: map(), causation_id :: binary() | undefined, correlation_id :: binary() | undefined, idempotency_key :: binary() | undefined}) -> {ok, non_neg_integer(), [map()], term()} | {error, term()}.
Execute a command and return the post-event aggregate state.
Like execute_command/2 but includes the aggregate state after applying all new events. Enables session-level consistency where the caller receives immediate truth about the resulting state.
Get the current state of an aggregate (for debugging).
-spec get_version(pid()) -> {ok, non_neg_integer()}.
Get the current version of an aggregate.
Start an aggregate process (uses env store_id).
Start an aggregate process with explicit store_id.