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

Callbacks

Authorizes a command

Handles a command

Validates the data in a command

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

authorization_result()

@type authorization_result() :: CommandedAggregateless.result(:unauthorized)

event()

@type event() :: struct()

execution_error()

@type execution_error() :: {atom(), String.t() | nil}

execution_result()

execution_success()

@type execution_success() :: [event()]

validation_result()

@type validation_result() ::
  CommandedAggregateless.result(
    {:ok, CommandedAggregateless.Command.CommandProtocol.t()},
    CommandedAggregateless.StructValidation.ValidationError.t()
  )

Callbacks

authorize(t)

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.

handle(struct, t)

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

validate(t)

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

__using__(opts \\ [])

(macro)
@spec __using__(keyword()) :: Macro.t()

Provides basic setup of a command module to reduce boilerplate

aggregate(opts \\ [], list)

(macro)
@spec aggregate(
  keyword(),
  [{:do, Macro.t()}]
) :: Macro.t()

Defines the aggregate attributes for the command

See module documentation for an example.

inputs(opts \\ [], list)

(macro)
@spec inputs(
  keyword(),
  [{:do, Macro.t()}]
) :: Macro.t()

Defines the inputs to the command

See module documentation for an example.