Cratis Chronicle projections: use composite keys
A composite key in Cratis Chronicle identifies a read model instance with several values instead of one. Use it when an order number, for example, is unique only within a customer: the customer ID and order number together select the order to update.
Defining a composite key
Section titled “Defining a composite key”Use UsingCompositeKey<T>() to map each part of the key. Configure the same parts for every event that updates the same order; both creation and shipping events must resolve to that order’s key.
using Cratis.Chronicle.Projections;
public class CompositeOrderProjection : IProjectionFor<CompositeOrder>{ public void Define(IProjectionBuilderFor<CompositeOrder> builder) => builder .From<CompositeOrderCreated>(created => created .UsingCompositeKey<CompositeOrderKey>(key => key .Set(k => k.CustomerId).To(e => e.CustomerId) .Set(k => k.OrderNumber).To(e => e.OrderNumber))) .From<CompositeOrderShipped>(shipped => shipped .UsingCompositeKey<CompositeOrderKey>(key => key .Set(k => k.CustomerId).To(e => e.CustomerId) .Set(k => k.OrderNumber).To(e => e.OrderNumber)));}import io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
class CompositeOrderProjection : IProjectionFor<CompositeOrder> { override fun define(builder: IProjectionBuilderFor<CompositeOrder>) { builder .from(CompositeOrderCreated::class) { it.usingCompositeKey { key -> key.property("customerId", "customerId") .property("orderNumber", "orderNumber") } } .from(CompositeOrderShipped::class) { it.usingCompositeKey { key -> key.property("customerId", "customerId") .property("orderNumber", "orderNumber") } } }}import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
class CompositeOrderProjection implements IProjectionFor<CompositeOrder> { @Override public void define(IProjectionBuilderFor<CompositeOrder> builder) { builder.from(CompositeOrderCreated.class, it -> { it.usingCompositeKey(key -> { key.property("customerId", "customerId").property("orderNumber", "orderNumber"); }); }); builder.from(CompositeOrderShipped.class, it -> { it.usingCompositeKey(key -> { key.property("customerId", "customerId").property("orderNumber", "orderNumber"); }); }); }}defmodule MyApp.Projections.CompositeOrderProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.CompositeOrder
alias MyApp.Events.{CompositeOrderCreated, CompositeOrderShipped}
from CompositeOrderCreated, key: {:composite, [customer_id: :customer_id, order_number: :order_number]}
from CompositeOrderShipped, key: {:composite, [customer_id: :customer_id, order_number: :order_number]}endimport { IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@projection('')class CompositeOrderProjection implements IProjectionFor<CompositeOrder> { define(builder: IProjectionBuilderFor<CompositeOrder>): void { builder .from(CompositeOrderCreated, from => from .usingCompositeKey<CompositeOrderKey>(key => key .set(target => target.customerId, event => event.customerId) .set(target => target.orderNumber, event => event.orderNumber))) .from(CompositeOrderShipped, from => from .usingCompositeKey<CompositeOrderKey>(key => key .set(target => target.customerId, event => event.customerId) .set(target => target.orderNumber, event => event.orderNumber))); }}Composite key type
Section titled “Composite key type”Define a record or class with one property for each key component:
public record CompositeOrderKey(string CustomerId, string OrderNumber);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.ReadModels.CompositeOrderKey do defstruct [:customer_id, :order_number]endclass CompositeOrderKey { customerId = ''; orderNumber = '';}Read model with composite key
Section titled “Read model with composite key”The read model’s Id property should match your composite key type:
public record CompositeOrder( CompositeOrderKey Id, string CustomerName, DateTimeOffset OrderDate, DateTimeOffset? ShippedDate);data class CompositeOrder( val customerName: String = "", val orderDate: String = "", val shippedDate: String? = null)class CompositeOrder { public String customerName = ""; public String orderDate = ""; public String shippedDate = null;}defmodule MyApp.ReadModels.CompositeOrder do defstruct [:id, :customer_name, :order_date, :shipped_date]endexport class CompositeOrder { id = ''; customerName = ''; orderDate = new Date(); shippedDate?: Date;}Composite keys with event context
Section titled “Composite keys with event context”A key can combine event content with metadata from event context. This example pairs the event’s user ID with its occurrence timestamp to identify an audit entry:
using Cratis.Chronicle.Projections;
public class AuditEntryProjectionWithCompositeKey : IProjectionFor<AuditEntryWithCompositeKey>{ public void Define(IProjectionBuilderFor<AuditEntryWithCompositeKey> builder) => builder .From<CompositeUserAction>(action => action .UsingCompositeKey<AuditEntryKey>(key => key .Set(k => k.UserId).To(e => e.UserId) .Set(k => k.Timestamp).ToEventContextProperty(c => c.Occurred)));}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Projections.AuditEntryProjectionWithCompositeKey do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.AuditEntryWithCompositeKey
alias MyApp.Events.CompositeUserAction
from CompositeUserAction, key: {:composite, [ user_id: :user_id, timestamp: {:event_context, "Occurred"} ]}endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CompositeUserAction { @field(String) userId = ''; @field(String) action = ''; @field(String) details = '';}
@projection('', AuditEntryWithCompositeKey)class AuditEntryProjectionWithCompositeKey implements IProjectionFor<AuditEntryWithCompositeKey> { define(builder: IProjectionBuilderFor<AuditEntryWithCompositeKey>): void { builder.from(CompositeUserAction, from => from .usingCompositeKey<AuditEntryKey>(key => key .set(target => target.userId).to(event => event.userId) .set(target => target.timestamp).toEventContextProperty('occurred'))); }}The corresponding key and read model:
public record AuditEntryKey(string UserId, DateTimeOffset Timestamp);
public record AuditEntryWithCompositeKey( AuditEntryKey Id, string Action, string Details);Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.ReadModels.AuditEntryKey do defstruct [:user_id, :timestamp]end
defmodule MyApp.ReadModels.AuditEntryWithCompositeKey do defstruct [:id, :action, :details]endimport { field } from '@cratis/fundamentals';
class AuditEntryKey { @field(String) userId = ''; @field(Date) timestamp = new Date();}
class AuditEntryWithCompositeKey { @field(AuditEntryKey) id = new AuditEntryKey(); @field(String) action = ''; @field(String) details = '';}ISO week keys
Section titled “ISO week keys”Pair Occurred.Week with Occurred.WeekYear, not the calendar Occurred.Year. ISO weeks can start in the previous calendar year or end in the next one. For example, December 30, 2024 is week 1 of week-year 2025, while January 1, 2021 is week 53 of week-year 2020. Using the calendar year would merge January 1 and December 30, 2024 into the same weekly key.
In .NET, use the Week() and WeekYear() extension methods from Cratis.Chronicle.Properties:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;using Cratis.Chronicle.Properties;
public record WeeklyKey(int WeekYear, int Week);public record WeeklySummary(WeeklyKey Id);
[EventType]public record WeeklyActivityRecorded;
public class WeeklySummaryProjection : IProjectionFor<WeeklySummary>{ public void Define(IProjectionBuilderFor<WeeklySummary> builder) => builder .From<WeeklyActivityRecorded>(activity => activity .UsingCompositeKey<WeeklyKey>(key => key .Set(k => k.WeekYear).ToEventContextProperty(c => c.Occurred.WeekYear()) .Set(k => k.Week).ToEventContextProperty(c => c.Occurred.Week())));}Model-bound context mappings accept the same paths: [SetFromContext<WeeklyActivityRecorded>("Occurred.WeekYear")] and [SetFromContext<WeeklyActivityRecorded>("Occurred.Week")]. The Year property still returns the calendar year.
Composite keys in child collections
Section titled “Composite keys in child collections”For a child collection, resolve the parent key and child key separately. Here, customer and order number select the parent order; product and variant select an item inside it:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record CompositeOrderCreatedForItems(string CustomerId, string OrderNumber);
[EventType]public record CompositeItemAddedToOrder( string CustomerId, string OrderNumber, string ProductId, string Variant, int Quantity);
public record CompositeOrderWithItems( CompositeOrderKey Id, IEnumerable<CompositeOrderItem> OrderItems);
public record CompositeOrderItem( CompositeItemKey Id, string ProductId, string Variant, int Quantity);
public record CompositeItemKey(string ProductId, string Variant);
public class CompositeOrderItemsProjection : IProjectionFor<CompositeOrderWithItems>{ public void Define(IProjectionBuilderFor<CompositeOrderWithItems> builder) => builder .From<CompositeOrderCreatedForItems>(created => created .UsingCompositeKey<CompositeOrderKey>(key => key .Set(k => k.CustomerId).To(e => e.CustomerId) .Set(k => k.OrderNumber).To(e => e.OrderNumber))) .Children(m => m.OrderItems, items => items .IdentifiedBy(m => m.Id) .From<CompositeItemAddedToOrder>(added => added .UsingParentCompositeKey<CompositeOrderKey>(key => key .Set(k => k.CustomerId).To(e => e.CustomerId) .Set(k => k.OrderNumber).To(e => e.OrderNumber)) .UsingCompositeKey<CompositeItemKey>(key => key .Set(k => k.ProductId).To(e => e.ProductId) .Set(k => k.Variant).To(e => e.Variant))));}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.CompositeOrderCreatedForItems do use Cratis.Chronicle.Events.EventType, id: "composite-order-created-for-items"
defstruct [:customer_id, :order_number]end
defmodule MyApp.Events.CompositeItemAddedToOrder do use Cratis.Chronicle.Events.EventType, id: "composite-item-added-to-order"
defstruct [:customer_id, :order_number, :product_id, :variant, quantity: 0]end
defmodule MyApp.ReadModels.CompositeItemKey do defstruct [:product_id, :variant]end
defmodule MyApp.ReadModels.CompositeOrderItem do defstruct [:id, :product_id, :variant, quantity: 0]end
defmodule MyApp.ReadModels.CompositeOrderWithItems do use Cratis.Chronicle.ReadModels.ReadModel, types: [order_items: {:list, MyApp.ReadModels.CompositeOrderItem}]
defstruct id: nil, order_items: []end
defmodule MyApp.Projections.CompositeOrderItemsProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.CompositeOrderWithItems
alias MyApp.Events.{CompositeItemAddedToOrder, CompositeOrderCreatedForItems}
from CompositeOrderCreatedForItems, key: {:composite, [customer_id: :customer_id, order_number: :order_number]}
children :order_items, identified_by: :id do from CompositeItemAddedToOrder, parent_key: {:composite, [customer_id: :customer_id, order_number: :order_number]}, key: {:composite, [product_id: :product_id, variant: :variant]}, set: [product_id: :product_id, variant: :variant, quantity: :quantity] endendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class CompositeItemAddedToOrder { @field(String) readonly customerId: string; @field(String) readonly orderNumber: string; @field(String) readonly productId: string; @field(String) readonly variant: string; @field(Number) readonly quantity: number;
constructor(customerId: string = '', orderNumber: string = '', productId: string = '', variant: string = '', quantity: number = 0) { this.customerId = customerId; this.orderNumber = orderNumber; this.productId = productId; this.variant = variant; this.quantity = quantity; }}
export class CompositeItemKey { productId = ''; variant = '';}
export class CompositeOrderItem { id = ''; productId = ''; variant = ''; quantity = 0;}
export class CompositeOrderWithItems { id = ''; orderItems: CompositeOrderItem[] = [];}
@projection('', CompositeOrderWithItems)export class CompositeOrderItemsProjection implements IProjectionFor<CompositeOrderWithItems> { define(builder: IProjectionBuilderFor<CompositeOrderWithItems>): void { builder .from(CompositeOrderCreated, from => from .usingCompositeKey<CompositeOrderKey>(key => key .set(target => target.customerId, event => event.customerId) .set(target => target.orderNumber, event => event.orderNumber))) .children<CompositeOrderItem>(model => model.orderItems, items => items .identifiedBy(model => model.id) .from(CompositeItemAddedToOrder, from => from .usingParentCompositeKey<CompositeOrderKey>(key => key .set(target => target.customerId, event => event.customerId) .set(target => target.orderNumber, event => event.orderNumber)) .usingCompositeKey<CompositeItemKey>(key => key .set(target => target.productId, event => event.productId) .set(target => target.variant, event => event.variant)))); }}Joins with composite keys
Section titled “Joins with composite keys”For a join, map the event’s key parts to the composite value stored in the property selected by On. Product ID and variant together identify the product whose name should update the order line:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record CompositeProductUpdated( string ProductId, string Variant, string ProductName);
public record CompositeProductKey(string ProductId, string Variant);
public record CompositeOrderLine( CompositeProductKey ProductKey, string ProductName);
public class CompositeOrderLineProjection : IProjectionFor<CompositeOrderLine>{ public void Define(IProjectionBuilderFor<CompositeOrderLine> builder) => builder .Join<CompositeProductUpdated>(product => product .On(m => m.ProductKey) .UsingCompositeKey<CompositeProductKey>(key => key .Set(k => k.ProductId).To(e => e.ProductId) .Set(k => k.Variant).To(e => e.Variant)));}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.CompositeProductUpdated do use Cratis.Chronicle.Events.EventType, id: "composite-product-updated"
defstruct [:product_id, :variant, :product_name]end
defmodule MyApp.ReadModels.CompositeProductKey do defstruct [:product_id, :variant]end
defmodule MyApp.ReadModels.CompositeOrderLine do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:product_key, :product_name]end
defmodule MyApp.Projections.CompositeOrderLineProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.CompositeOrderLine
alias MyApp.Events.CompositeProductUpdated
join CompositeProductUpdated, on: :product_key, key: {:composite, [product_id: :product_id, variant: :variant]}, set: [product_name: :product_name]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class CompositeProductUpdated { @field(String) readonly productId: string; @field(String) readonly variant: string; @field(String) readonly productName: string;
constructor(productId: string = '', variant: string = '', productName: string = '') { this.productId = productId; this.variant = variant; this.productName = productName; }}
export class CompositeProductKey { productId = ''; variant = '';}
export class CompositeOrderLine { id = ''; productName = '';}
@projection('', CompositeOrderLine)export class CompositeOrderLineProjection implements IProjectionFor<CompositeOrderLine> { define(builder: IProjectionBuilderFor<CompositeOrderLine>): void { builder .join(CompositeProductUpdated, join => join .on(model => model.id) .usingCompositeKey<CompositeProductKey>(key => key .set(target => target.productId, event => event.productId) .set(target => target.variant, event => event.variant))); }}Event definitions
Section titled “Event definitions”Events that update the same order carry both key components:
using Cratis.Chronicle.Events;
[EventType]public record CompositeOrderCreated( string CustomerId, string OrderNumber, string CustomerName, DateTimeOffset OrderDate);
[EventType]public record CompositeOrderShipped( string CustomerId, string OrderNumber, DateTimeOffset ShippedDate);
[EventType]public record CompositeUserAction( string UserId, string Action, string Details);import io.cratis.chronicle.events.EventType
@EventTypedata class CompositeOrderCreated( val customerId: String, val orderNumber: String, val customerName: String, val orderDate: String)
@EventTypedata class CompositeOrderShipped( val customerId: String, val orderNumber: String, val shippedDate: String)
@EventTypedata class CompositeUserAction( val userId: String, val action: String, val details: String)import io.cratis.chronicle.events.EventType;
@EventTyperecord CompositeOrderCreated( String customerId, String orderNumber, String customerName, String orderDate) {}
@EventTyperecord CompositeOrderShipped( String customerId, String orderNumber, String shippedDate) {}
@EventTyperecord CompositeUserAction( String userId, String action, String details) {}defmodule MyApp.Events.CompositeOrderCreated do use Cratis.Chronicle.Events.EventType, id: "composite-order-created"
defstruct [:customer_id, :order_number, :customer_name, :order_date]end
defmodule MyApp.Events.CompositeOrderShipped do use Cratis.Chronicle.Events.EventType, id: "composite-order-shipped"
defstruct [:customer_id, :order_number, :shipped_date]end
defmodule MyApp.Events.CompositeUserAction do use Cratis.Chronicle.Events.EventType, id: "composite-user-action"
defstruct [:user_id, :action, :details]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CompositeOrderCreated { @field(String) readonly customerId: string; @field(String) readonly orderNumber: string; @field(String) readonly customerName: string; @field(Date) readonly orderDate: Date;
constructor(customerId: string = '', orderNumber: string = '', customerName: string = '', orderDate: Date = new Date()) { this.customerId = customerId; this.orderNumber = orderNumber; this.customerName = customerName; this.orderDate = orderDate; }}
@eventType()class CompositeOrderShipped { @field(String) readonly customerId: string; @field(String) readonly orderNumber: string; @field(Date) readonly shippedDate: Date;
constructor(customerId: string = '', orderNumber: string = '', shippedDate: Date = new Date()) { this.customerId = customerId; this.orderNumber = orderNumber; this.shippedDate = shippedDate; }}Key composition rules
Section titled “Key composition rules”Use the same key structure for events targeting the same read model instance. Each part must retain its meaning across those events: if a key value changes, the event targets a different instance rather than renaming the existing one.
The combined values must distinguish the instances you want to keep separate. A typed key makes the mapped properties explicit, but it does not establish business uniqueness for you.
Flat composite keys in declarations
Section titled “Flat composite keys in declarations”A typeless key block identifies a root read model by its ordinary root properties, without an Id property or a separate key record. This is different from the legacy untyped fluent expression described below.
projection Orders => Order no automap from OrderPlaced key customerId = customerId orderNumber = orderNumber amount = amount remove with OrderCanceled key orderNumber = orderNumber customerId = customerIdThe Order schema declares scalar customerId, orderNumber, and amount properties. Every root from and remove with handler must supply the same complete set of at least two key parts. Part order does not affect identity. Chronicle stores and seeds the parts as root properties, even with no automap. Mappings may assign the same values again, but an event that changes or removes a key part fails before saving. A removal selects only the complete tuple; removing a missing tuple does nothing, and a later creation can recreate it.
Flat keys currently support root handlers with MongoDB and InMemory sinks only. Children, joins, nested scopes, every/all handlers, clear, and remove-via-join are not supported. Part names must be simple root scalar properties; id (in any casing), names beginning with _, dotted names, and duplicate parts are refused. Screenplay checks completeness against its declared identifier; Chronicle checks consistency between handlers and the supplied read model schema, not an identifier omitted from every handler.
The stored expression is $flatComposite(customerId=$eventContent.customerId,orderNumber=$eventContent.orderNumber). Existing $composite(...) definitions keep their legacy single-Id semantics. Registration refuses changing between flat and non-flat key modes on an existing projection or read model. Use a new read model for a different identity contract; registration does not migrate existing documents.
Flat definitions require the kernel release introducing $flatComposite or later on every executing kernel. Upgrade the entire kernel fleet before deploying them. Older kernels can silently interpret an unknown key expression as the event source ID; do not deploy flat definitions to them or roll an active flat projection back to them. Wire compatibility checks do not fence this feature.
Kernel compatibility
Section titled “Kernel compatibility”Projection declarations that specify a composite key type require Chronicle kernel v19.8.1 or later. Older kernels reject the type name when projecting events, so upgrade the kernel before deploying those declarations.
Fluent .NET projections emit untyped composite-key expressions, which remain compatible with older kernels. The updated kernel also reads those stored expressions without migration. The connection-time compatibility check covers wire versions, not this projection-expression behavior.
Generating declarations from stored projections
Section titled “Generating declarations from stored projections”When generating a declaration from an untyped fluent composite-key expression, Chronicle infers the type from the read model’s Id property, or the child item’s Id for a child collection. Nullable object types are supported. The .NET client includes the .NET key type name in the generated schema so the resulting declaration can be compiled again; the stored fluent expression remains untyped.
For a manually supplied schema without a key type name, generation uses CompositeKey as a placeholder. That declaration cannot recompile unless the schema defines a CompositeKey object with the mapped properties.
Performance considerations
Section titled “Performance considerations”Choose key parts for identity, not as a promise about query performance. Check query and index behavior against the storage provider you use; a composite projection key does not by itself establish an efficient query plan.
If every event should update one shared instance instead, use a constant key. To map metadata into values as well as keys, continue with event context.