Chronik v0.1.6 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
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
The state
represents the state of an projection.
Link to this section Functions
Start a new projection
Return the current state for projection_id
Link to this section 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 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 fromversion
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 theChronik.PubSub
. Possible values are:eventual
(default) and:strict
.