CommandedAggregateless. Command behaviour
(commanded_aggregateless v1.0.0)
Copy Markdown
Provides a basic command module setup to reduce boilerplate
Example:
defmodule CommandedAggregateless.TestCommand do
use CommandedAggregateless.Command,
identifier: :id,
prefix: "test",
required_permission: "manage_tests"
alias CommandedAggregateless.TestAggregateCreated
inputs do
field :id, binary()
field :some_required_key, binary()
field :error_in_handle, boolean()
end
validates(:some_required_key, string: true)
aggregate do
field :id, binary()
apply_event TestAggregateCreated do
%{aggregate | id: event.id}
end
end
def handle(_aggregate, %__MODULE__{error_in_handle: true}),
do: {:error, {:command_handler_error, "There was an error handling the command"}}
def handle(%{id: nil}, command) do
{:ok, %TestAggregateCreated{id: command.id}}
end
def handle(_aggregate, command) do
{:error, {:aggregate_exists, "Aggregate #{command.id} already exists."}}
end
end
Summary
Functions
Provides basic setup of a command module to reduce boilerplate
Defines the aggregate attributes for the command
Defines the inputs to the command
Types
@type authorization_result() :: CommandedAggregateless.result(:unauthorized)
@type event() :: struct()
@type execution_result() :: CommandedAggregateless.result(execution_success(), execution_error()) | CommandedAggregateless.result(execution_error())
@type execution_success() :: [event()]
@type validation_result() :: CommandedAggregateless.result( {:ok, CommandedAggregateless.Command.CommandProtocol.t()}, CommandedAggregateless.StructValidation.ValidationError.t() )
Callbacks
@callback authorize(CommandedAggregateless.Command.CommandProtocol.t()) :: authorization_result()
Authorizes a command
Must return :ok if the command is authorized, or {:error, :unauthorized}
if it is not.
The default implementation checks if the auth_subject has the required
permission as specified when calling use CommandedAggregateless.Command.
@callback handle( struct(), CommandedAggregateless.Command.CommandProtocol.t() ) :: execution_result()
Handles a command
Passed the current state of the aggegate and the command to be handled. May return:
{:ok, events}- to indicate the command was successful and return a list of events{:error, error}- to indicate the command failed with an error:ok- to indicate the command was successful with no events
@callback validate(CommandedAggregateless.Command.CommandProtocol.t()) :: validation_result()
Validates the data in a command
Commands all use the CommandedAggregateless.StructValidation module to
validate their data by default, but you can override this behavior by
implementing the validate/1 function in your command module.
Functions
Provides basic setup of a command module to reduce boilerplate
Defines the aggregate attributes for the command
See module documentation for an example.
Defines the inputs to the command
See module documentation for an example.