Chronik v0.1.3 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.handle_event.
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 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 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
Link to this section Callbacks
The handle_event function is executed each time an event record is received
on the 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:
versionstart replaying events fromversionand up. A:allvalue indicates that the replay should be from the begining of times.consistencyindicates how the projection should subscribe to the PubSub. Possible values are:eventual(defualt) andstrict.