evoq_command behaviour (evoq v1.14.1)

View Source

Command behavior for evoq.

Commands represent intentions to change state. They are: - Imperative (present tense): open_account, deposit_money - Targeted at a specific aggregate - Validated before dispatch - Domain artifacts: atom keys, Erlang terms, stay inside bounded context

Required Callbacks

- command_type() -> atom() - new(Params) -> {ok, Command} | {error, Reason} - to_map(Command) -> map()

Optional Callbacks

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

Summary

Functions

Ensure the command has a command_id. If undefined, auto-generates one.

Get the aggregate ID.

Get the aggregate type.

Get the command ID.

Get the idempotency key (may be undefined).

Get the command metadata.

Get the command payload.

Get the command type.

Set a caller-provided idempotency key for deduplication.

Validate a command using its module's validate/1 callback.

Callbacks

command_type/0

-callback command_type() -> atom().

from_map/1

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

new/1

-callback new(Params :: map()) -> {ok, Command :: term()} | {error, Reason :: term()}.

to_map/1

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

validate/1

(optional)
-callback validate(Command :: term()) -> ok | {ok, Command :: term()} | {error, Reason :: term()}.

Functions

ensure_id(Evoq_command)

-spec ensure_id(#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}) ->
                   #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}.

Ensure the command has a command_id. If undefined, auto-generates one.

get_aggregate_id(Evoq_command)

-spec get_aggregate_id(#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}) ->
                          binary().

Get the aggregate ID.

get_aggregate_type(Evoq_command)

-spec get_aggregate_type(#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}) ->
                            atom().

Get the aggregate type.

get_id(Evoq_command)

-spec get_id(#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}) ->
                binary().

Get the command ID.

get_idempotency_key(Evoq_command)

-spec get_idempotency_key(#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}) ->
                             binary() | undefined.

Get the idempotency key (may be undefined).

get_metadata(Evoq_command)

-spec get_metadata(#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}) ->
                      map().

Get the command metadata.

get_payload(Evoq_command)

-spec get_payload(#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}) ->
                     map().

Get the command payload.

get_type(Evoq_command)

-spec get_type(#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}) ->
                  atom().

Get the command type.

new(CommandType, AggregateType, AggregateId, Payload)

-spec new(atom(), atom(), binary(), map()) ->
             #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}.

Create a new command.

new(CommandType, AggregateType, AggregateId, Payload, Metadata)

-spec new(atom(), atom(), binary(), map(), map()) ->
             #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}.

Create a new command with metadata.

set_causation_id(CausationId, Evoq_command)

-spec set_causation_id(binary(),
                       #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}) ->
                          #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}.

Set the causation ID.

set_correlation_id(CorrelationId, Evoq_command)

-spec set_correlation_id(binary(),
                         #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}) ->
                            #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}.

Set the correlation ID.

set_idempotency_key(Key, Evoq_command)

-spec set_idempotency_key(binary(),
                          #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}) ->
                             #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}.

Set a caller-provided idempotency key for deduplication.

validate(Evoq_command)

-spec validate(#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 | {error, term()}.

Validate a command using its module's validate/1 callback.