Counterpoint.Projection behaviour (counterpoint v0.1.0)

Copy Markdown View Source

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

apply(state, envelope)

@callback apply(state :: term(), envelope :: Counterpoint.Envelope.t()) :: term()

Fold a single envelope into the current state.

init()

@callback init() :: term()

Return the initial (empty) state before any events are applied.

query(args)

@callback query(args :: term()) :: Counterpoint.Query.t()

Build the query used to fetch events for this projection.

Functions

run(module, store_name, args \\ nil)

Execute the projection for args against the given store.

Fetches events using module.query(args), then folds them with module.apply/2 starting from module.init().