Skip to content

Cratis Chronicle projection declarations with PDL

The Projection Declaration Language (PDL) describes how Cratis Chronicle builds read models from events using text declarations instead of projection classes. PDL is part of Cratis Screenplay, the indentation-based language for describing Cratis applications. Chronicle uses the Screenplay compiler to turn those declarations into projection definitions.

Start with Screenplay — Projections for declaration syntax, property mapping, keys, counters, joins, children, and the full grammar. This page explains the Chronicle-specific choices that affect whether a declaration compiles and which event generations it uses. To inspect results without saving a projection, use ad-hoc querying.

A projection can build a property graph instead of a read model: write => graph <Name> after the projection name and describe the node and edge blocks. Chronicle lowers the declaration into a graph projection definition and registers the graph as a read model whose schema carries the x-graph-* annotations, the same shape the .NET client registers for a model-bound or fluent graph projection.

graph Organization
node Person
personId Uuid key
name String
node Group
groupId Uuid key
name String
edge MEMBER_OF source Person target Group
since DateTime
projection OrganizationGraph => graph Organization
node Person
from PersonRegistered key personId
name = name
remove with PersonRemoved key personId detach
node Group
from GroupCreated key groupId
name = name
edge MEMBER_OF
from MemberAdded source personId target groupId
since = $eventContext.occurred
remove with MemberRemoved source personId target groupId

Save the graph and its projection together as one declaration. The declaration holds exactly one graph projection and the graph it builds; top-level concept lines are allowed, and a graph property otherwise uses a primitive type (Uuid, String, Int, Decimal, Bool, Date, DateTime). Chronicle stores graph read models in the graph sink (Neo4j).

  • Properties an event shares by name with a node or edge are mapped automatically, as for a model-bound graph.
  • An every block is applied to each from of its block, so a rendered declaration writes the mappings out under every from instead of in an every.
  • The Preview request of the projections service does not preview a graph projection. The Workbench previews one in memory, see Graph projections.
  • Rendering a registered graph projection, whichever client registered it, produces a declaration that saves unchanged.

See Screenplay — Graph projections and Screenplay — Graphs for the language.

Set the save request’s GenerationDelivery to Pinned to resolve each event reference to the latest registered generation when you save. For example, from PersonRegistered records the latest registered generation of PersonRegistered in the projection definition. Registering a newer generation later does not change that pin; saving the declaration again resolves the name against the generations registered at that time.

The same resolution applies to join and removal references, including clauses inside children and nested objects. A pinned projection can name only one generation per event type across all its clauses. Registration rejects conflicting pins, but different event types can pin different generations.

PDL has no generation-qualified event reference syntax. To name a generation explicitly in code, use the .NET projection builders or model-bound attributes with the CLR event type representing that generation. The one-generation-per-event-type rule still applies to pinned projections.

Compatibility is the default: bare event names become generation 1 in the definition, while event dispatch matches the event type identifier without its generation. The save request’s setting does not apply to ad-hoc queries or immediate projections. Wildcard (all) projections use Compatibility; Pinned wildcard registration is not supported. Read Event type migrations before changing delivery policy or migrating events used by a pinned projection.

Use the registered read-model identifier after =>, not its display name or schema title. In the .NET client, that identifier is the full type name (typeof(TReadModel).FullName), such as MyApp.ReadModels.Order. Chronicle renders registered projections with this identifier, so you can save the declaration unchanged.

Match registered spelling and casing exactly in identifiers and property paths. The .NET client’s default naming policy preserves CLR property names, which are normally PascalCase. If a name does not match exactly, compilation fails. When a display title, short type name, or differently cased name matches a known identifier or property, the diagnostic suggests the registered spelling. If several names match, it lists the candidates so you can choose the intended identifier.

An every block inside children applies its mappings to descendant events too, unless it declares exclude children. This lets a child-level mapping respond to events from deeper in its hierarchy.

For stored declarations created before this behavior was corrected, re-save the original PDL that uses every inside children, then replay if recommended. A kernel upgrade or replay alone does not recompile stored definitions.

Set-once, dictionary removal, ordering, and arithmetic

Section titled “Set-once, dictionary removal, ordering, and arithmetic”

Chronicle supports the following Screenplay projection statements:

projection OrderTotals => MyApp.ReadModels.OrderTotals
from ItemAdded
set once openedAt = $eventContext.occurred
add total by quantity * unitPrice
from ItemRemoved
remove items.$eventContent.itemId
children lines identified by id
sort by position ascending
from LineAdded key lineId
parent orderId
position = position

set once writes when the target is missing or null. Later events do not replace a non-null value: zero, false, and an empty string count as already set. A dynamic dictionary target applies this rule independently to each entry.

remove deletes one dictionary entry, rather than assigning null or removing the read model. The container must be a dictionary in the registered read-model schema. Address its entry with .$eventContent.path, .$eventContext.path, or .$causedBy.subject, .name, or .userName. An absent entry or container is a no-op, and other entries are retained. Static targets and unknown key paths are rejected.

Children append by default (insert last). Use insert first to prepend, or sort by with ascending or descending and a scalar child property, including dotted paths. New children with equal values retain insertion order. Missing and null values come first ascending and last descending. Updating an existing child never moves it, even when its sort value changes.

add and subtract accept one optional multiplication after by. Chronicle multiplies the two numeric operands as decimals before converting the product to the target type. Missing, null, nonnumeric operands and overflow fail processing; they do not silently become zero. This is not a general arithmetic-expression language, and division is not supported.

Generated PDL preserves these operations and child ordering; the default append order may omit its ordering line. Upgrade every kernel that will process the projection before saving or replaying a declaration that uses arithmetic factors: older kernels cannot execute the new stored product expressions. This delivery adds no SQL-specific sink functionality. Untyped composite keys and flat composite identities are not yet supported; typed composite keys retain their existing behavior.

Continue with Ad-hoc Querying to send a declaration through IProjections.Query() and inspect the resulting JSON read models. That API processes a bounded event set without registering the projection; it is useful for exploration, not a substitute for a maintained read model.