Chronicle projection architecture
Chronicle projections turn events into read models through a mapping definition executed by the kernel. Attributes, fluent definitions, and Projection Declaration Language (PDL) produce that shared definition; they change where you author the mapping, not where it runs.
If you are choosing a style for a screen or query, start with choosing a read-model style. This page follows a projection from its authored form to the engine and the storage sink that holds the resulting read model.
Three Projection Approaches
Section titled “Three Projection Approaches”1. Projection Declaration Language
Section titled “1. Projection Declaration Language”PDL is an indentation-based text format for definitions authored outside application code, such as in the Workbench or Screenplay. The compiler resolves the declaration into the same definition that code-based mappings produce.
projection MyProjection => MyApp.ReadModels.MyModel from MyEvent name = eventProperty count eventCountThe declaration names the destination read model and the event that supplies its fields. Choose PDL when text is the artifact your workflow owns, including ad-hoc queries. Its supported expressions and save settings determine what that definition can do.
2. Model-Bound Projections
Section titled “2. Model-Bound Projections”Model-bound projections put mapping attributes or annotations on the read model. Start here when a reader can understand how a field is populated by looking at the model itself.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ArchitectureModelBoundItemAdded(string Category);
[FromEvent<ArchitectureModelBoundItemAdded>(key: nameof(ArchitectureModelBoundItemAdded.Category))]public record ArchitectureModelBoundSummary( [Key] string Category, [Count<ArchitectureModelBoundItemAdded>] int Count);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.Countimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ArchitectureModelBoundItemAdded(val category: String)
@ReadModel@FromEvent(ArchitectureModelBoundItemAdded::class, key = "category")data class ArchitectureModelBoundSummary( @Count(ArchitectureModelBoundItemAdded::class) val count: Int = 0)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.Count;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ArchitectureModelBoundItemAdded(String category) {}
@ReadModel@FromEvent(eventType = ArchitectureModelBoundItemAdded.class, key = "category")class ArchitectureModelBoundSummary { @Count(eventType = ArchitectureModelBoundItemAdded.class) public int count = 0;}defmodule MyApp.Events.ArchitectureModelBoundItemAdded do use Cratis.Chronicle.Events.EventType, id: "architecture-model-bound-item-added"
defstruct [:category]end
defmodule MyApp.ReadModels.ArchitectureModelBoundSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct category: nil, count: 0
from MyApp.Events.ArchitectureModelBoundItemAdded, key: :category, count: :countendimport { count, eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ArchitectureModelBoundItemAdded { category = '';}
@fromEvent(ArchitectureModelBoundItemAdded, { key: 'category' })export class ArchitectureModelBoundSummary { @field(String) category = '';
@count(ArchitectureModelBoundItemAdded) count = 0;}The client discovers the metadata and builds a definition for registration. The attributes describe mappings; they are not event handlers on a model instance. See model-bound projections for the available mappings.
3. Declarative Projections
Section titled “3. Declarative Projections”Fluent projections use code to construct a mapping definition. The kernel executes that definition;
it does not call Define or its builder lambdas for each event.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record ArchitectureDeclarativeItemAdded(string Category);
public record ArchitectureDeclarativeSummary( string Category, int Count);
public class ArchitectureDeclarativeSummaryProjection : IProjectionFor<ArchitectureDeclarativeSummary>{ public void Define(IProjectionBuilderFor<ArchitectureDeclarativeSummary> builder) => builder .From<ArchitectureDeclarativeItemAdded>(_ => _ .UsingKey(e => e.Category) .Count(m => m.Count));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class ArchitectureDeclarativeItemAdded(val category: String)
data class ArchitectureDeclarativeSummary( val category: String = "", val count: Int = 0)
class ArchitectureDeclarativeSummaryProjection : IProjectionFor<ArchitectureDeclarativeSummary> { override fun define(builder: IProjectionBuilderFor<ArchitectureDeclarativeSummary>) { builder .from(ArchitectureDeclarativeItemAdded::class) { it.usingKey("category") it.count(ArchitectureDeclarativeSummary::count) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord ArchitectureDeclarativeItemAdded(String category) {}
class ArchitectureDeclarativeSummary { public String category = ""; public int count = 0;}
class ArchitectureDeclarativeSummaryProjection implements IProjectionFor<ArchitectureDeclarativeSummary> { @Override public void define(IProjectionBuilderFor<ArchitectureDeclarativeSummary> builder) { builder .from(ArchitectureDeclarativeItemAdded.class, fb -> { fb.usingKey("category"); fb.count("count"); }); }}defmodule MyApp.Events.ArchitectureDeclarativeItemAdded do use Cratis.Chronicle.Events.EventType, id: "architecture-declarative-item-added"
defstruct [:category]end
defmodule MyApp.ReadModels.ArchitectureDeclarativeSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct category: nil, count: 0end
defmodule MyApp.Projections.ArchitectureDeclarativeSummaryProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.ArchitectureDeclarativeSummary
from MyApp.Events.ArchitectureDeclarativeItemAdded, key: :category, count: :countendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class ArchitectureDeclarativeItemAdded { @field(String) readonly category: string;
constructor(category: string) { this.category = category; }}
class ArchitectureDeclarativeSummary { category = ''; count = 0;}
@projection()class ArchitectureDeclarativeSummaryProjection implements IProjectionFor<ArchitectureDeclarativeSummary> { define(builder: IProjectionBuilderFor<ArchitectureDeclarativeSummary>): void { builder.from(ArchitectureDeclarativeItemAdded, _ => _ .usingKey(e => e.category) .count(m => m.count)); }}Use the fluent builder when attributes make a mapping hard to scan or the model should remain free of mapping metadata. Keys, joins across event sources, and child collections can be easier to follow in a separate definition.
Constructor dependencies can help construct that definition, but they are not injected into a per-event calculation. If the next state needs arbitrary branching over previous state, choose a reducer instead.
Architecture Components
Section titled “Architecture Components”Frontend Layer
Section titled “Frontend Layer”The Workbench hosts the projection editor. Its Monaco language service provides syntax highlighting, completion, hover information, and error markers while you edit. These checks help you correct a declaration before saving; they do not mean its read model has caught up with the event log.
Compilation Layer
Section titled “Compilation Layer”The projection language is part of Cratis Screenplay. The
Cratis.Screenplay package parses the declaration into a syntax tree and reports syntax errors with
line and column locations. Chronicle validates the supported projection syntax against event types
and schemas, then its implementation of IProjectionSyntaxVisitor<T> produces a
ProjectionDefinition.
A declaration that parses as Screenplay can still use an expression Chronicle does not support. Fix the reported projection diagnostics before treating the definition as ready to register.
Core Layer
Section titled “Core Layer”The registry holds the ProjectionDefinition independently of its authoring form. The engine uses
that definition to construct the processing pipeline. When a client registers a changed mapping,
Chronicle compares it with the stored definition and determines whether existing history must be
replayed. Registration lifecycle explains that decision and the policy
that controls automatic rebuilds.
Runtime Layer
Section titled “Runtime Layer”The projector consumes events selected by the definition, resolves the read-model key, and applies mappings, joins, and child operations. For a materialized projection, a storage sink persists the result so queries can read it without replaying history.
That processing is asynchronous: a successful append does not wait for the sink update. A passive projection instead computes an instance when it is read, without a materialized sink. It replays the selected event source, so it cannot generally reproduce a view built from cross-source joins or custom aggregation keys.
Data Flow
Section titled “Data Flow”Choosing an approach
Section titled “Choosing an approach”Use the read-model decision guide for scenarios and a capability comparison. PDL suits definitions authored in tooling; it does not replace the model-bound-first path for application code.
Common Patterns Across All Approaches
Section titled “Common Patterns Across All Approaches”Regardless of which approach you choose, all projections share these concepts:
- Event Types: Define which events trigger the projection
- Keys: Identify read model instances
- Property Mappings: Transform event data to read model properties
- Joins: Correlate events across event sources
- Children: Model parent-child relationships
- Subscriptions: Select event types; projections do not have append-metadata subscription filters
- Operations: Set, increment, decrement, count, add, subtract
All three inputs produce the same ProjectionDefinition format. Their authoring and configuration
surfaces differ: sharing an engine does not mean that PDL has every option exposed by the code builders.
Next, choose the consistency behavior your query needs. Authoring a projection differently does not make its materialized read model update inside an append.