Projections
Projections are shared Chronicle read-model behavior. Use the shared docs for projection styles, model-bound projections, declarative projections, and client-tabbed examples.
- Projections
- Choosing a read model style
- Model-bound projections
- Declarative projections
- Graph projections - project events onto nodes and edges
- Elixir client setup
Child collections
Section titled “Child collections”children/2,3 declares a list on the parent that grows, changes and shrinks with events. Mirrors
the .NET [ChildrenFrom<T>]. It works in use Cratis.Chronicle.ReadModels.ReadModel and
use Cratis.Chronicle.Projections.Projection:
defmodule MyApp.ReadModels.Basket do use Cratis.Chronicle.ReadModels.ReadModel, types: [items: {:list, MyApp.ReadModels.BasketItem}]
defstruct basket_id: "", items: []
from MyApp.Events.BasketCreated, set: [basket_id: :event_source_id]
children :items, identified_by: :item_id do from MyApp.Events.ItemAdded, key: :item_id, parent_key: :event_source_id, set: [item_id: :item_id, name: :name, quantity: :quantity]
from MyApp.Events.QuantityIncreased, key: :item_id, parent_key: :event_source_id, add: [quantity: :amount]
removed_with MyApp.Events.ItemRemoved, key: :item_id, parent_key: :event_source_id endendidentified_by names the property that identifies a child. Inside the block from/2, join/2,
removed_with/2, removed_with_join/2, clear_with/2, from_every/1 and a nested children/3
take the options they take on the read model. key identifies the child an event applies to and
parent_key the parent it belongs to.
A collection that only ever grows from a single event does not need a do block:
children :entries, from: MyApp.Events.EntryAdded, identified_by: :entry_id, key: :entry_id, set: [entry_id: :entry_id, name: :name]Unlike a root join/2, a join inside a children block does not require :on — it defaults to
the collection’s identified_by, since a child is always joined by its own identifying property.
An explicit :on that disagrees with identified_by is accepted but produces a compile-time
warning rather than an error.
removed_with_join/2 removes a child when a secondary event — from a different event source —
matches it by a property instead of by the collection’s own key:
removed_with_join MyApp.Events.EntryArchivedElsewhere, key: :archived_entry_idUse clear: [:field] in a mapping, or the clear_with event, [:field] sugar, to set a scalar
back to no value. The target field must default to nil: a field whose default is a concrete
value (such as "") is not nullable to the kernel, and clearing it writes the type default back
instead of no value.
Ordering new children
Section titled “Ordering new children”New children are appended by default. prepend: true inserts each new child first, and
order_by inserts it in order of a child property — order_by: :priority (ascending),
order_by: [asc: :priority] or order_by: [desc: :priority]:
children :tasks, identified_by: :task_id, order_by: [desc: :priority] do from MyApp.Events.TaskAdded, key: :task_idendOrdering only places new children: updating a child never moves it. Use one of prepend and
order_by per collection; both, or a malformed order_by, is a compile-time error. Children
ordering needs Chronicle 19.150.0 or later.
A few mistakes are caught at compile time rather than at registration: a children collection
without :identified_by, a root join/2 without :on, the same target property assigned twice
by one event’s mapping, and a from/join target or children name that is not a field of the
read model’s struct.
Dictionary entries keyed by the event
Section titled “Dictionary entries keyed by the event”A map field can hold one entry per value an event carries. A key source names where the key comes
from: {:event_content, :sku} (an atom is sent camelCase, so it matches the event’s wire name; a
string such as "customer.id" is used as written and is case-sensitive), {:event_context, :type}
or {:caused_by, :subject | :name | :user_name}.
from MyApp.Events.PriceSet, set: [prices: {:entry, {:event_content, :sku}, :price}]
from MyApp.Events.PriceWithdrawn, remove: [prices: {:event_content, :sku}]
from MyApp.Events.ItemSold, add: [units: {:entry, {:event_content, :sku}, :quantity}], count: [sales_by_user: {:caused_by, :subject}]:set, :set_once, :add and :subtract take {:entry, key_source, expression}; :count,
:increment, :decrement and :remove take the key source itself. The kernel checks at
registration that an event content key exists on the event.
Setting a value only once
Section titled “Setting a value only once”set_once: sets a field, or a dictionary entry, only while it has no value yet, so the first
event wins and later ones leave it alone:
from MyApp.Events.DocumentRevised, set_once: [original_title: :title, created_by: {:caused_by, :subject}, created: :occurred], set: [title: :title]Projection or reducer
Section titled “Projection or reducer”Keeping the first value an event supplies, adding and removing dictionary entries keyed by an event value, and keeping children newest first or sorted are projection operations. None of them alone is a reason to write a reducer: reach for a reducer when the next state depends on logic a declaration cannot express. These operations need Chronicle 19.150.0 or later.
Pinning the id
Section titled “Pinning the id”use Cratis.Chronicle.Projections.Projection defaults a declarative projection’s id to the last
segment of its module name. Renaming or moving the module then changes that id, and Chronicle
treats it as a different projection — its observation position resets. Pin the id explicitly so
the module can be renamed or moved freely:
use Cratis.Chronicle.Projections.Projection, id: "account-summary", model: MyApp.ReadModels.AccountSummaryA projection relying on the module-name default gets a one-time warning (per module, for the life of the BEAM node) from discovery, naming the module and showing this fix. See Discovery and artifact identity drift.
Ad-hoc querying with Cratis.Chronicle.Projections
Section titled “Ad-hoc querying with Cratis.Chronicle.Projections”Cratis.Chronicle.Projections.query/2 sends a Projection Declaration Language (PDL) string straight to
the kernel and returns the projected entries, without registering a permanent projection or
defining a read model module:
{:ok, result} = Cratis.Chronicle.Projections.query(""" projection OrderSummary from OrderPlaced """)
result.instances # decoded as plain maps unless :read_model is givenPass :read_model to decode each entry as a struct instead. A declaration with syntax or type
errors returns {:error, {:invalid_declaration, [%{message:, line:, column:}]}}. See the shared
Ad-hoc Querying page for
the full picture — Elixir now joins C# in supporting this.
Cratis.Chronicle.Projections.preview/2 runs an already-registered projection’s own declaration the same
way, looked up by read model module or projection identifier.
Waiting for a projection
Section titled “Waiting for a projection”Cratis.Chronicle.Projections.wait_for/3 polls until a projection has handled a given event sequence
number, or returns {:error, :timeout}:
{:ok, %{sequence_number: sequence}} = Cratis.Chronicle.EventSequences.EventLog.append_with_result(account_id, event)
:ok = Cratis.Chronicle.Projections.wait_for(MyApp.ReadModels.Account, sequence)Event context values
Section titled “Event context values”:occurred maps to the event’s occurred time ($eventContext(Occurred)). The old $occurred
spelling was rejected by the kernel and is now translated.
{:caused_by, :subject | :name | :user_name} maps to a property of the identity that caused the
event ($causedBy(subject), $causedBy(name), $causedBy(userName)). A bare :caused_by is not
special: like any other atom it reads the event field caused_by.