Behaviour for domain events stored in the event log.
use Counterpoint.Event injects:
- A default
type_string/0that returns the last segment of the module name (e.g.MyApp.Events.OrderPlaced→"OrderPlaced"). - A default
tags/1that returns[].
Both defaults can be overridden.
Example
defmodule MyApp.Events.OrderPlaced do
use Counterpoint.Event
defstruct [:order_id, :total]
# override tags to enable tag-based filtering
def tags(%__MODULE__{order_id: id}), do: ["order_id:#{id}"]
def to_map(%__MODULE__{order_id: id, total: t}),
do: %{"order_id" => id, "total" => t}
def from_map(%{"order_id" => id, "total" => t}),
do: %__MODULE__{order_id: id, total: t}
endRemember to register the module in the events: list passed to
Counterpoint.Supervisor so it can be deserialized from the store.
Summary
Callbacks
Deserialize a plain map (from JSON) back into the event struct.
Tags attached to the event for filtering. Return [] if no tags are needed.
Serialize the event struct to a plain map for JSON storage.
Unique string identifier for this event type, used as the stored type name.
Callbacks
Deserialize a plain map (from JSON) back into the event struct.
Tags attached to the event for filtering. Return [] if no tags are needed.
Serialize the event struct to a plain map for JSON storage.
@callback type_string() :: String.t()
Unique string identifier for this event type, used as the stored type name.