Chronik v0.1.2 Chronik.Projection behaviour View Source

The Projection is a read model connected to the PubSub.

Client code has to implement the Chronik.Projection.init function and the state transition Chronik.Projection.next_state.

Example:

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

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

defmodule CounterState do
  use Chronik.Projection

  alias DomainEvents.CounterCreated
  alias DomainEvents.CounterIncremented

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

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

Link to this section Summary

Types

The state represents the state of an projection

Callbacks

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

The next_state function is executed each time an event record is received on the PubSub and is responsible of the projeciton state transition

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 Callbacks

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

The accepted options:

  • version start replaying events from version and up. A :all value indicates that the replay should be from the begining of times.
  • consistency indicates how the projection should subscribe to the PubSub. Possible values are :eventual (defualt) and strict.
Link to this callback next_state(state, record) View Source
next_state(state :: state, record :: Chronik.EventRecord) :: state

The next_state function is executed each time an event record is received on the PubSub and is responsible of the projeciton state transition.

The return value is a new state for the received record