Cratis Chronicle model-bound projections: Map matching properties
Use convention-based mapping in Cratis Chronicle when an event and a read model share property names and compatible shapes. Declare the source event on the read model, and Chronicle copies the matching properties without a set mapping on each one. This is the model-bound form of AutoMap.
Add explicit mappings for exceptions: a different source name, a value from event context, or an operation such as adding an amount instead of replacing a value.
How It Works
Section titled “How It Works”Chronicle matches event properties to read model properties by name and compatible type. Nested objects can map when their internal names match; collections can map when their element shapes are compatible. A read model property absent from the event is left unchanged for that event.
Client syntax differs, but each client produces a projection definition containing the event type, the key that selects a read model instance, and the automatic mapping behavior.
Basic Convention Mapping
Section titled “Basic Convention Mapping”Declare the source event at the read model level, using the syntax for your client. Matching properties then map without per-property set declarations:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ConventionUserRegistered( string Name, string Email, DateTimeOffset RegisteredAt);
[FromEvent<ConventionUserRegistered>]public record ConventionUser( [Key] Guid Id, string Name, string Email, DateTimeOffset RegisteredAt);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ConventionUserRegistered( val name: String, val email: String, val registeredAt: String)
@ReadModel@FromEvent(ConventionUserRegistered::class)data class ConventionUser( val name: String = "", val email: String = "", val registeredAt: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ConventionUserRegistered(String name, String email, String registeredAt) {}
@ReadModel@FromEvent(eventType = ConventionUserRegistered.class)class ConventionUser { public String name = ""; public String email = ""; public String registeredAt = "";}defmodule MyApp.Events.ConventionUserRegistered do use Cratis.Chronicle.Events.EventType, id: "convention-user-registered-v1"
defstruct [:name, :email, :registered_at]end
defmodule MyApp.ReadModels.ConventionUser do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ConventionUserRegistered
defstruct [:name, :email, :registered_at]
# No `set:` list — matching field names are mapped automatically. from ConventionUserRegisteredendimport { eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ConventionUserRegistered { @field(String) readonly name: string; @field(String) readonly email: string; @field(Date) readonly registeredAt: Date;
constructor(name: string, email: string, registeredAt: Date) { this.name = name; this.email = email; this.registeredAt = registeredAt; }}
@fromEvent(ConventionUserRegistered)export class ConventionUser { @field(String) name = ''; @field(String) email = ''; @field(Date) registeredAt = new Date();}For these matching properties, the result is the same as declaring each set mapping explicitly:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ExplicitConventionUserRegistered( string Name, string Email, DateTimeOffset RegisteredAt);
public record ExplicitConventionUser( [Key] Guid Id,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.Name))] string Name,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.Email))] string Email,
[SetFrom<ExplicitConventionUserRegistered>(nameof(ExplicitConventionUserRegistered.RegisteredAt))] DateTimeOffset RegisteredAt);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ExplicitConventionUserRegistered( val name: String, val email: String, val registeredAt: String)
@ReadModel@FromEvent(ExplicitConventionUserRegistered::class)data class ExplicitConventionUser( @SetFrom("name") val name: String = "",
@SetFrom("email") val email: String = "",
@SetFrom("registeredAt") val registeredAt: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ExplicitConventionUserRegistered(String name, String email, String registeredAt) {}
@ReadModel@FromEvent(eventType = ExplicitConventionUserRegistered.class)class ExplicitConventionUser { @SetFrom(propertyPath = "name") public String name = "";
@SetFrom(propertyPath = "email") public String email = "";
@SetFrom(propertyPath = "registeredAt") public String registeredAt = "";}defmodule MyApp.Events.ExplicitConventionUserRegistered do use Cratis.Chronicle.Events.EventType, id: "explicit-convention-user-registered-v1"
defstruct [:name, :email, :registered_at]end
defmodule MyApp.ReadModels.ExplicitConventionUser do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ExplicitConventionUserRegistered
defstruct [:name, :email, :registered_at]
from ExplicitConventionUserRegistered, set: [name: :name, email: :email, registered_at: :registered_at]endimport { eventType, fromEvent, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ExplicitConventionUserRegistered { @field(String) readonly name: string; @field(String) readonly email: string; @field(Date) readonly registeredAt: Date;
constructor(name: string, email: string, registeredAt: Date) { this.name = name; this.email = email; this.registeredAt = registeredAt; }}
@fromEvent(ExplicitConventionUserRegistered)export class ExplicitConventionUser { @setFrom(ExplicitConventionUserRegistered, 'name') name = '';
@setFrom(ExplicitConventionUserRegistered, 'email') email = '';
@setFrom(ExplicitConventionUserRegistered, 'registeredAt') registeredAt = new Date();}Multiple Events
Section titled “Multiple Events”Declare more than one source event when several event types contribute to a read model. Each event updates the matching properties it contains and leaves the rest unchanged. If two events both contain a matching property, either can replace its value.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ConventionUserProfileCreated(string Name, string Email);
[EventType]public record ConventionUserProfileUpdated(string Name, string Email, string Phone);
[FromEvent<ConventionUserProfileCreated>][FromEvent<ConventionUserProfileUpdated>]public record ConventionUserProfile( [Key] Guid Id, string Name, string Email, string Phone);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ConventionUserProfileCreated( val name: String, val email: String)
@EventTypedata class ConventionUserProfileUpdated( val name: String, val email: String, val phone: String)
@ReadModel@FromEvent(ConventionUserProfileCreated::class)@FromEvent(ConventionUserProfileUpdated::class)data class ConventionUserProfile( val name: String = "", val email: String = "", val phone: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ConventionUserProfileCreated(String name, String email) {}
@EventTyperecord ConventionUserProfileUpdated(String name, String email, String phone) {}
@ReadModel@FromEvent(eventType = ConventionUserProfileCreated.class)@FromEvent(eventType = ConventionUserProfileUpdated.class)class ConventionUserProfile { public String name = ""; public String email = ""; public String phone = "";}defmodule MyApp.Events.ConventionUserProfileCreated do use Cratis.Chronicle.Events.EventType, id: "convention-user-profile-created-v1"
defstruct [:name, :email]end
defmodule MyApp.Events.ConventionUserProfileUpdated do use Cratis.Chronicle.Events.EventType, id: "convention-user-profile-updated-v1"
defstruct [:name, :email, :phone]end
defmodule MyApp.ReadModels.ConventionUserProfile do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{ConventionUserProfileCreated, ConventionUserProfileUpdated}
defstruct [:name, :email, :phone]
from ConventionUserProfileCreated from ConventionUserProfileUpdatedendimport { eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ConventionUserProfileCreated { @field(String) readonly name: string; @field(String) readonly email: string;
constructor(name: string, email: string) { this.name = name; this.email = email; }}
@eventType()export class ConventionUserProfileUpdated { @field(String) readonly name: string; @field(String) readonly email: string; @field(String) readonly phone: string;
constructor(name: string, email: string, phone: string) { this.name = name; this.email = email; this.phone = phone; }}
@fromEvent(ConventionUserProfileCreated)@fromEvent(ConventionUserProfileUpdated)export class ConventionUserProfile { @field(String) name = ''; @field(String) email = ''; @field(String) phone = '';}Custom Keys
Section titled “Custom Keys”By default, convention-based mappings use the event source id as the read model key. Use a custom key when the event carries the identifier of the read model instance in its content.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ConventionUserRegisteredWithKey( Guid UserId, string Name, string Email);
[FromEvent<ConventionUserRegisteredWithKey>(key: nameof(ConventionUserRegisteredWithKey.UserId))]public record ConventionUserById( [Key] Guid Id, string Name, string Email);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ConventionUserRegisteredWithKey( val userId: String, val name: String, val email: String)
@ReadModel@FromEvent(ConventionUserRegisteredWithKey::class, key = "userId")data class ConventionUserById( val name: String = "", val email: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ConventionUserRegisteredWithKey(String userId, String name, String email) {}
@ReadModel@FromEvent(eventType = ConventionUserRegisteredWithKey.class, key = "userId")class ConventionUserById { public String name = ""; public String email = "";}defmodule MyApp.Events.ConventionUserRegisteredWithKey do use Cratis.Chronicle.Events.EventType, id: "convention-user-registered-with-key-v1"
defstruct [:user_id, :name, :email]end
defmodule MyApp.ReadModels.ConventionUserById do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ConventionUserRegisteredWithKey
defstruct [:name, :email]
from ConventionUserRegisteredWithKey, key: :user_idendimport { eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ConventionUserRegisteredWithKey { @field(String) readonly userId: string; @field(String) readonly name: string; @field(String) readonly email: string;
constructor(userId: string, name: string, email: string) { this.userId = userId; this.name = name; this.email = email; }}
@fromEvent(ConventionUserRegisteredWithKey, { key: 'userId' })export class ConventionUserById { @field(String) name = ''; @field(String) email = '';}The custom key changes which instance receives the event; it does not change how property names match.
Relationship To Declarative AutoMap
Section titled “Relationship To Declarative AutoMap”Convention-based model-bound mapping and declarative AutoMap describe the same mapping behavior through different APIs.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ConventionEquivalentUserRegistered(string Name, string Email);
[FromEvent<ConventionEquivalentUserRegistered>]public record ConventionEquivalentUser( [Key] Guid Id, string Name, string Email);
public class ConventionEquivalentProjection : IProjectionFor<ConventionEquivalentUser>{ public void Define(IProjectionBuilderFor<ConventionEquivalentUser> builder) => builder.From<ConventionEquivalentUserRegistered>();}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionForimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ConventionEquivalentUserRegistered( val name: String, val email: String)
@ReadModel@FromEvent(ConventionEquivalentUserRegistered::class)data class ConventionEquivalentUser( val name: String = "", val email: String = "")
class ConventionEquivalentProjection : IProjectionFor<ConventionEquivalentUser> { override fun define(builder: IProjectionBuilderFor<ConventionEquivalentUser>) { builder.from(ConventionEquivalentUserRegistered::class) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ConventionEquivalentUserRegistered(String name, String email) {}
@ReadModel@FromEvent(eventType = ConventionEquivalentUserRegistered.class)class ConventionEquivalentUser { public String name = ""; public String email = "";}
class ConventionEquivalentProjection implements IProjectionFor<ConventionEquivalentUser> { @Override public void define(IProjectionBuilderFor<ConventionEquivalentUser> builder) { builder.from(ConventionEquivalentUserRegistered.class); }}defmodule MyApp.Events.ConventionEquivalentUserRegistered do use Cratis.Chronicle.Events.EventType, id: "convention-equivalent-user-registered-v1"
defstruct [:name, :email]end
defmodule MyApp.ReadModels.ConventionEquivalentUser do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email]end
defmodule MyApp.Projections.ConventionEquivalentProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.ConventionEquivalentUser
alias MyApp.Events.ConventionEquivalentUserRegistered
# No `set:` list — matching field names are mapped automatically, the same # way the model-bound `from` declaration on the read model itself would. from ConventionEquivalentUserRegisteredendimport { eventType, fromEvent, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ConventionEquivalentUserRegistered { @field(String) readonly name: string; @field(String) readonly email: string;
constructor(name: string, email: string) { this.name = name; this.email = email; }}
@fromEvent(ConventionEquivalentUserRegistered)export class ConventionEquivalentUser { @field(String) name = ''; @field(String) email = '';}
@projection('', ConventionEquivalentUser)export class ConventionEquivalentProjection implements IProjectionFor<ConventionEquivalentUser> { define(builder: IProjectionBuilderFor<ConventionEquivalentUser>): void { builder.from(ConventionEquivalentUserRegistered); }}Keep the model-bound form when the mapping is easiest to understand beside the read model’s properties. Use a declarative projection when its builder operations fit the mapping better or the definition should stay separate from the read model type.
Matching Structures
Section titled “Matching Structures”Matching names and shapes can also map nested values and collections. Check the members inside the nested value or collection element, not just the outer property name:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
public record ConventionAddress(string Street, string City, string PostalCode);
public record ConventionLineItem(string ProductName, decimal UnitPrice, int Quantity);
[EventType]public record ConventionCustomerRegistered( string FirstName, string LastName, ConventionAddress BillingAddress, ConventionAddress ShippingAddress);
[EventType]public record ConventionOrderCreated( string CustomerEmail, ConventionLineItem[] Items, string[] Tags);
[FromEvent<ConventionCustomerRegistered>]public record ConventionCustomer( [Key] Guid Id, string FirstName, string LastName, ConventionAddress BillingAddress, ConventionAddress ShippingAddress);
[FromEvent<ConventionOrderCreated>]public record ConventionOrder( [Key] Guid Id, string CustomerEmail, ConventionLineItem[] Items, string[] Tags);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
data class ConventionAddress( val street: String = "", val city: String = "", val postalCode: String = "")
data class ConventionLineItem( val productName: String = "", val unitPrice: Double = 0.0, val quantity: Int = 0)
@EventTypedata class ConventionCustomerRegistered( val firstName: String, val lastName: String, val billingAddress: ConventionAddress, val shippingAddress: ConventionAddress)
@EventTypedata class ConventionOrderCreated( val customerEmail: String, val items: List<ConventionLineItem>, val tags: List<String>)
@ReadModel@FromEvent(ConventionCustomerRegistered::class)data class ConventionCustomer( val firstName: String = "", val lastName: String = "", val billingAddress: ConventionAddress = ConventionAddress(), val shippingAddress: ConventionAddress = ConventionAddress())
@ReadModel@FromEvent(ConventionOrderCreated::class)data class ConventionOrder( val customerEmail: String = "", val items: List<ConventionLineItem> = emptyList(), val tags: List<String> = emptyList())import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;import java.util.List;
record ConventionAddress(String street, String city, String postalCode) {}
record ConventionLineItem(String productName, double unitPrice, int quantity) {}
@EventTyperecord ConventionCustomerRegistered( String firstName, String lastName, ConventionAddress billingAddress, ConventionAddress shippingAddress) {}
@EventTyperecord ConventionOrderCreated(String customerEmail, List<ConventionLineItem> items, List<String> tags) {}
@ReadModel@FromEvent(eventType = ConventionCustomerRegistered.class)class ConventionCustomer { public String firstName = ""; public String lastName = ""; public ConventionAddress billingAddress; public ConventionAddress shippingAddress;}
@ReadModel@FromEvent(eventType = ConventionOrderCreated.class)class ConventionOrder { public String customerEmail = ""; public List<ConventionLineItem> items = List.of(); public List<String> tags = List.of();}defmodule MyApp.Events.ConventionCustomerRegistered do use Cratis.Chronicle.Events.EventType, id: "convention-customer-registered-v1"
defstruct [:first_name, :last_name, :billing_address, :shipping_address]end
defmodule MyApp.Events.ConventionOrderCreated do use Cratis.Chronicle.Events.EventType, id: "convention-order-created-v1"
defstruct [:customer_email, :items, :tags]end
defmodule MyApp.ReadModels.ConventionCustomer do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ConventionCustomerRegistered
# Nested maps and lists are plain Elixir terms — matching field names on the # event are copied across as-is, structure and all. defstruct [:first_name, :last_name, :billing_address, :shipping_address]
from ConventionCustomerRegisteredend
defmodule MyApp.ReadModels.ConventionOrder do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.ConventionOrderCreated
defstruct [:customer_email, :items, :tags]
from ConventionOrderCreatedendimport { eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
export class ConventionAddress { street = ''; city = ''; postalCode = '';}
export class ConventionLineItem { productName = ''; unitPrice = 0; quantity = 0;}
@eventType()export class ConventionCustomerRegistered { @field(String) readonly firstName: string; @field(String) readonly lastName: string; @field(ConventionAddress) readonly billingAddress: ConventionAddress; @field(ConventionAddress) readonly shippingAddress: ConventionAddress;
constructor(firstName: string, lastName: string, billingAddress: ConventionAddress, shippingAddress: ConventionAddress) { this.firstName = firstName; this.lastName = lastName; this.billingAddress = billingAddress; this.shippingAddress = shippingAddress; }}
@eventType()export class ConventionOrderCreated { @field(String) readonly customerEmail: string; @field(Array, { genericArguments: [ConventionLineItem] }) readonly items: ConventionLineItem[]; @field(Array, { genericArguments: [String] }) readonly tags: string[];
constructor(customerEmail: string, items: ConventionLineItem[], tags: string[]) { this.customerEmail = customerEmail; this.items = items; this.tags = tags; }}
@fromEvent(ConventionCustomerRegistered)export class ConventionCustomer { firstName = ''; lastName = ''; billingAddress = new ConventionAddress(); shippingAddress = new ConventionAddress();}
@fromEvent(ConventionOrderCreated)export class ConventionOrder { customerEmail = ''; @field(Array, { genericArguments: [ConventionLineItem] }) items: ConventionLineItem[] = []; @field(Array, { genericArguments: [String] }) tags: string[] = [];}Partial Events
Section titled “Partial Events”An event can supply only part of the read model. Here, each event updates its matching properties without clearing values supplied by the other events:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record ConventionPartialUserRegistered(string Email);
[EventType]public record ConventionPartialUserCompleted( string FirstName, string LastName, string Phone);
[FromEvent<ConventionPartialUserRegistered>][FromEvent<ConventionPartialUserCompleted>]public record ConventionPartialUser( [Key] Guid Id, string Email, string FirstName, string LastName, string Phone);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class ConventionPartialUserRegistered( val email: String)
@EventTypedata class ConventionPartialUserCompleted( val firstName: String, val lastName: String, val phone: String)
@ReadModel@FromEvent(ConventionPartialUserRegistered::class)@FromEvent(ConventionPartialUserCompleted::class)data class ConventionPartialUser( val email: String = "", val firstName: String = "", val lastName: String = "", val phone: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord ConventionPartialUserRegistered(String email) {}
@EventTyperecord ConventionPartialUserCompleted(String firstName, String lastName, String phone) {}
@ReadModel@FromEvent(eventType = ConventionPartialUserRegistered.class)@FromEvent(eventType = ConventionPartialUserCompleted.class)class ConventionPartialUser { public String email = ""; public String firstName = ""; public String lastName = ""; public String phone = "";}defmodule MyApp.Events.ConventionPartialUserRegistered do use Cratis.Chronicle.Events.EventType, id: "convention-partial-user-registered-v1"
defstruct [:email]end
defmodule MyApp.Events.ConventionPartialUserCompleted do use Cratis.Chronicle.Events.EventType, id: "convention-partial-user-completed-v1"
defstruct [:first_name, :last_name, :phone]end
defmodule MyApp.ReadModels.ConventionPartialUser do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{ConventionPartialUserRegistered, ConventionPartialUserCompleted}
defstruct [:email, :first_name, :last_name, :phone]
from ConventionPartialUserRegistered from ConventionPartialUserCompletedendimport { eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class ConventionPartialUserRegistered { @field(String) readonly email: string;
constructor(email: string) { this.email = email; }}
@eventType()export class ConventionPartialUserCompleted { @field(String) readonly firstName: string; @field(String) readonly lastName: string; @field(String) readonly phone: string;
constructor(firstName: string, lastName: string, phone: string) { this.firstName = firstName; this.lastName = lastName; this.phone = phone; }}
@fromEvent(ConventionPartialUserRegistered)@fromEvent(ConventionPartialUserCompleted)export class ConventionPartialUser { @field(String) email = ''; @field(String) firstName = ''; @field(String) lastName = ''; @field(String) phone = '';}Excluding A Property From Mapping
Section titled “Excluding A Property From Mapping”An event subscribed through a property mapping or join can also carry properties that match by convention. An explicit mapping from one event does not, by itself, stop another event from overwriting the same read model property by name.
Events used only for aggregation are excluded automatically. For other collisions, exclude the affected property with [NoAutoMap].
Aggregate-Only Events
Section titled “Aggregate-Only Events”When every mapping for an event is an aggregate operation, such as count, increment, decrement, add, or subtract, Chronicle does not copy that event’s other properties by convention. Counting an event therefore cannot, on its own, overwrite a name or description with an unrelated same-named field. No exclusion attribute is needed.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record AggOnlyArrangementSet(string Location);
[EventType]public record AggOnlyCandidateSubmitted(string Name, string Location);
[FromEvent<AggOnlyArrangementSet>]public record AggOnlyAssignmentSummary( [Key] Guid Id,
// AggOnlyCandidateSubmitted is subscribed only to be counted, so its identically named // Location is not auto-mapped over the value sourced from AggOnlyArrangementSet. [SetFrom<AggOnlyArrangementSet>(nameof(AggOnlyArrangementSet.Location))] string Location,
[Count<AggOnlyCandidateSubmitted>] int CandidateCount);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.Countimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class AggOnlyArrangementSet(val location: String)
@EventTypedata class AggOnlyCandidateSubmitted(val name: String, val location: String)
@ReadModel@FromEvent(AggOnlyArrangementSet::class)data class AggOnlyAssignmentSummary( // AggOnlyCandidateSubmitted is subscribed only to be counted, so its identically named // location is not auto-mapped over the value sourced from AggOnlyArrangementSet. @SetFrom("location", AggOnlyArrangementSet::class) val location: String = "",
@Count(AggOnlyCandidateSubmitted::class) val candidateCount: Int = 0)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.Count;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord AggOnlyArrangementSet(String location) {}
@EventTyperecord AggOnlyCandidateSubmitted(String name, String location) {}
@ReadModel@FromEvent(eventType = AggOnlyArrangementSet.class)record AggOnlyAssignmentSummary( // AggOnlyCandidateSubmitted is subscribed only to be counted, so its identically named // location is not auto-mapped over the value sourced from AggOnlyArrangementSet. @SetFrom(propertyPath = "location", eventType = AggOnlyArrangementSet.class) String location,
@Count(eventType = AggOnlyCandidateSubmitted.class) int candidateCount) {}defmodule MyApp.Events.AggOnlyArrangementSet do use Cratis.Chronicle.Events.EventType, id: "agg-only-arrangement-set-v1"
defstruct [:location]end
defmodule MyApp.Events.AggOnlyCandidateSubmitted do use Cratis.Chronicle.Events.EventType, id: "agg-only-candidate-submitted-v1"
defstruct [:name, :location]end
defmodule MyApp.ReadModels.AggOnlyAssignmentSummary do use Cratis.Chronicle.ReadModels.ReadModel
alias MyApp.Events.{AggOnlyArrangementSet, AggOnlyCandidateSubmitted}
defstruct [:location, candidate_count: 0]
from AggOnlyArrangementSet
# AggOnlyCandidateSubmitted is subscribed only to be counted, so its own # `location` field is never mapped over the value set from # AggOnlyArrangementSet. from AggOnlyCandidateSubmitted, count: :candidate_countendimport { count, eventType, fromEvent, Guid, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class AggOnlyArrangementSet { @field(String) readonly location: string;
constructor(location: string) { this.location = location; }}
@eventType()export class AggOnlyCandidateSubmitted { @field(String) readonly name: string; @field(String) readonly location: string;
constructor(name: string, location: string) { this.name = name; this.location = location; }}
// AggOnlyCandidateSubmitted is subscribed only to be counted, so its identically named// location is not auto-mapped over the value sourced from AggOnlyArrangementSet.@fromEvent(AggOnlyArrangementSet)export class AggOnlyAssignmentSummary { id: Guid = Guid.empty;
@setFrom(AggOnlyArrangementSet, 'location') location = '';
@count(AggOnlyCandidateSubmitted) candidateCount = 0;}[NoAutoMap] On A Property
Section titled “[NoAutoMap] On A Property”For a collision involving a value-mapped ([SetFrom]) or joined ([Join]) event, apply [NoAutoMap] to the read model property or record parameter. Only explicit mappings can then change that property; the other properties keep mapping by convention.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record NoAutoMapWorkArrangementSet(string Location, int WorkMode);
[EventType]public record NoAutoMapCandidateSubmitted(string Name, string Location);
[FromEvent<NoAutoMapWorkArrangementSet>]public record NoAutoMapAssignmentSummary( [Key] Guid Id,
// Location is sourced only from NoAutoMapWorkArrangementSet. NoAutoMapCandidateSubmitted is // value-mapped (for CandidateName) and also carries a Location; [NoAutoMap] stops that Location // from being auto-mapped over the explicit value, while every other property keeps mapping. [SetFrom<NoAutoMapWorkArrangementSet>(nameof(NoAutoMapWorkArrangementSet.Location))] [NoAutoMap] string Location,
[SetFrom<NoAutoMapCandidateSubmitted>(nameof(NoAutoMapCandidateSubmitted.Name))] string CandidateName);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.NoAutoMapimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class NoAutoMapWorkArrangementSet(val location: String, val workMode: Int)
@EventTypedata class NoAutoMapCandidateSubmitted(val name: String, val location: String)
@ReadModel@FromEvent(NoAutoMapWorkArrangementSet::class)data class NoAutoMapAssignmentSummary( // location is sourced only from NoAutoMapWorkArrangementSet. NoAutoMapCandidateSubmitted is // value-mapped (for candidateName) and also carries a location; @NoAutoMap stops that location // from being auto-mapped over the explicit value, while every other property keeps mapping. @SetFrom("location", NoAutoMapWorkArrangementSet::class) @NoAutoMap val location: String = "",
@SetFrom("name", NoAutoMapCandidateSubmitted::class) val candidateName: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.NoAutoMap;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord NoAutoMapWorkArrangementSet(String location, int workMode) {}
@EventTyperecord NoAutoMapCandidateSubmitted(String name, String location) {}
@ReadModel@FromEvent(eventType = NoAutoMapWorkArrangementSet.class)record NoAutoMapAssignmentSummary( // location is sourced only from NoAutoMapWorkArrangementSet. NoAutoMapCandidateSubmitted is // value-mapped (for candidateName) and also carries a location; @NoAutoMap stops that location // from being auto-mapped over the explicit value, while every other property keeps mapping. @SetFrom(propertyPath = "location", eventType = NoAutoMapWorkArrangementSet.class) @NoAutoMap String location,
@SetFrom(propertyPath = "name", eventType = NoAutoMapCandidateSubmitted.class) String candidateName) {}defmodule MyApp.Events.NoAutoMapWorkArrangementSet do use Cratis.Chronicle.Events.EventType, id: "no-auto-map-work-arrangement-set"
defstruct location: "", work_mode: 0end
defmodule MyApp.Events.NoAutoMapCandidateSubmitted do use Cratis.Chronicle.Events.EventType, id: "no-auto-map-candidate-submitted"
defstruct name: "", location: ""end
defmodule MyApp.ReadModels.NoAutoMapAssignmentSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct id: nil, location: nil, candidate_name: nil
no_auto_map([:location])
from MyApp.Events.NoAutoMapWorkArrangementSet, set: [id: :event_source_id, location: :location]
from MyApp.Events.NoAutoMapCandidateSubmitted, set: [candidate_name: :name]endimport { eventType, fromEvent, noAutoMap, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class NoAutoMapWorkArrangementSet { @field(String) readonly location: string; @field(Number) readonly workMode: number;
constructor(location: string, workMode: number) { this.location = location; this.workMode = workMode; }}
@eventType()export class NoAutoMapCandidateSubmitted { @field(String) readonly name: string; @field(String) readonly location: string;
constructor(name: string, location: string) { this.name = name; this.location = location; }}
@fromEvent(NoAutoMapWorkArrangementSet)export class NoAutoMapAssignmentSummary { // location is sourced only from NoAutoMapWorkArrangementSet. NoAutoMapCandidateSubmitted is // value-mapped (for candidateName) and also carries a location; @noAutoMap stops that location // from being auto-mapped over the explicit value, while every other property keeps mapping. @setFrom(NoAutoMapWorkArrangementSet, 'location') @noAutoMap location = '';
@setFrom(NoAutoMapCandidateSubmitted, 'name') candidateName = '';}[NoAutoMap] also works at the read model (class) level to disable convention mapping for the whole read model. Use the property form when you only need to protect a single property and want convention mapping everywhere else.
When To Use It
Section titled “When To Use It”Start with convention mapping when most properties are direct copies and names are consistent. Add explicit mappings where the source or operation differs, and use a property-level exclusion when a value must only come from its declared source.
The client builds the projection definition from the model metadata; Chronicle processes events using that definition rather than reflecting over your model attributes for every event. For the explicit operations, continue with basic property mapping.