Chronik v0.1.7 Chronik.Projection behaviour View Source

Chronik.Projection is a read-only model connected to Chronik.PubSub

Client code has to implement the Chronik.Projection.init/1 function and the state transition Chronik.Projection.handle_event/2.

Example

defmodule DomainEvents do
  defmodule CounterCreated do
    defstruct [:id]
  end

  defmodule CounterIncremented do
    defstruct [:id, :increment]
  end
end

defmodule CounterState do
  @behaviour Chronik.Projection

  alias DomainEvents.CounterCreated
  alias DomainEvents.CounterIncremented
  alias Chronik.Projection

  def start_link(opts), do: Projection.start_link(__MODULE__, opts)

  def init(_opts), do: {nil, []}

  def handle_event(%CounterCreated{}, nil) do
    0
  end
  def handle_event(%CounterIncremented{increment: increment}, value) do
    value + increment
  end
end

Link to this section Summary

Types

The state represents the state of an projection

Functions

Start a new projection

Return the current state for projection_id

Callbacks

The handle_event function is executed each time an event record is received on the Chronik.PubSub and is responsible of the projection state transition

The init function defines the intial state of an projection and some options

Link to this section Types

Link to this type state() View Source
state() :: term

The state represents the state of an projection.

Link to this section Functions

Link to this function start_link(projection, opts) View Source

Start a new projection

Return the current state for projection_id

Link to this section Callbacks

Link to this callback handle_event(record, state) View Source
handle_event(record :: Chronik.EventRecord, state :: state) :: state

The handle_event function is executed each time an event record is received on the Chronik.PubSub and is responsible of the projection state transition.

The return value is a new state for the received record

The init function defines the intial state of an projection and some options.

The accepted options:

  • :version start replaying events from version and up. :all indicates that the replay should be from the begining of times while :current means that no re-play should be made and start feeding domain events from current version and on.

  • :consistency indicates how the projection should subscribe to the Chronik.PubSub. Possible values are :eventual (default) and :strict.