Counterpoint.Event behaviour (counterpoint v0.1.0)

Copy Markdown View Source

Behaviour for domain events stored in the event log.

use Counterpoint.Event injects:

  • A default type_string/0 that returns the last segment of the module name (e.g. MyApp.Events.OrderPlaced"OrderPlaced").
  • A default tags/1 that 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}
end

Remember 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

from_map(map)

@callback from_map(map()) :: struct()

Deserialize a plain map (from JSON) back into the event struct.

tags(struct)

@callback tags(struct()) :: [String.t()]

Tags attached to the event for filtering. Return [] if no tags are needed.

to_map(struct)

@callback to_map(struct()) :: map()

Serialize the event struct to a plain map for JSON storage.

type_string()

@callback type_string() :: String.t()

Unique string identifier for this event type, used as the stored type name.