Simplified read-side projection without limit or reverse support.
Behaves like Counterpoint.OnDemandProjection but always reads the full
event stream in chronological order. Use OnDemandProjection when you need
Query.limit/2 or Query.reverse/1; use this one for straightforward
full-stream folds.
use Counterpoint.Projection injects @behaviour Counterpoint.Projection.
Example
defmodule MyApp.Views.TotalRevenue do
use Counterpoint.Projection
alias Counterpoint.Query
alias MyApp.Events.OrderPlaced
@impl Counterpoint.Projection
def query(_args),
do: Query.new() |> Query.add_item(types: [OrderPlaced])
@impl Counterpoint.Projection
def init, do: 0
@impl Counterpoint.Projection
def apply(total, %Counterpoint.Envelope{data: %OrderPlaced{total: t}}),
do: total + t
end
Counterpoint.Projection.run(MyApp.Views.TotalRevenue, :my_store)
Summary
Callbacks
Fold a single envelope into the current state.
Return the initial (empty) state before any events are applied.
Build the query used to fetch events for this projection.
Functions
Execute the projection for args against the given store.
Callbacks
@callback apply(state :: term(), envelope :: Counterpoint.Envelope.t()) :: term()
Fold a single envelope into the current state.
@callback init() :: term()
Return the initial (empty) state before any events are applied.
@callback query(args :: term()) :: Counterpoint.Query.t()
Build the query used to fetch events for this projection.