commanded_messaging v0.2.1 Commanded.Event View Source

Creates a domain event structure.

Options

  • from - A struct to adapt the keys from.
  • with - A list of keys to add to the event.
  • drop - A list of keys to drop from the keys adapted from a struct.
  • version - An optional version of the event. Defaults to 1.

Example

  # This is for demonstration purposes only. You don't need to create a new event to version one.
  defmodule AccountCreatedVersioned do
    use Commanded.Event,
      version: 2,
      from: CreateAccount,
      with: [:date, :sex],
      drop: [:email],

    defimpl Commanded.Event.Upcaster, for: AccountCreatedWithDroppedKeys do
      def upcast(%{version: 1} = event, _metadata) do
        AccountCreatedVersioned.new(event, sex: "maybe", version: 2)
      end

      def upcast(event, _metadata), do: event
    end
  end

  iex> changeset = CreateAccount.new(username: "chris", email: "chris@example.com", age: 5)
  iex> cmd = Ecto.Changeset.apply_changes(changeset)
  iex> event = AccountCreatedWithDroppedKeys.new(cmd)
  iex> Commanded.Event.Upcaster.upcast(event, %{})
  %AccountCreatedVersioned{age: 5, date: nil, sex: "maybe", username: "chris", version: 2}