Cratis Chronicle AutoMap: map matching event properties
AutoMap in Cratis Chronicle maps event properties to read model properties with matching names. It is enabled by default in declarative projections, so you can declare the events a read model needs without repeating each property assignment.
Use explicit mappings where names differ or a value comes from somewhere other than the event payload. Keep event and read model property types compatible; AutoMap is a naming convention, not a value transformation.
Basic usage
Section titled “Basic usage”Declare each event with From. In this example, the creation event supplies Name and Email; the rename event changes only Name.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record AutoMapUserCreated(string Name, string Email);
[EventType]public record AutoMapUserRenamed(string Name);
public record AutoMapUser(string Name, string Email);
public class AutoMapUserProjection : IProjectionFor<AutoMapUser>{ public void Define(IProjectionBuilderFor<AutoMapUser> builder) => builder .From<AutoMapUserCreated>() .From<AutoMapUserRenamed>();}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class AutoMapUserCreated(val name: String, val email: String)
@EventTypedata class AutoMapUserRenamed(val name: String)
data class AutoMapUser(val name: String = "", val email: String = "")
class AutoMapUserProjection : IProjectionFor<AutoMapUser> { override fun define(builder: IProjectionBuilderFor<AutoMapUser>) { builder .from(AutoMapUserCreated::class) .from(AutoMapUserRenamed::class) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord AutoMapUserCreated(String name, String email) {}
@EventTyperecord AutoMapUserRenamed(String name) {}
class AutoMapUser { public String name = ""; public String email = "";}
class AutoMapUserProjection implements IProjectionFor<AutoMapUser> { @Override public void define(IProjectionBuilderFor<AutoMapUser> builder) { builder.from(AutoMapUserCreated.class); builder.from(AutoMapUserRenamed.class); }}defmodule MyApp.Events.AutoMapUserCreated do use Cratis.Chronicle.Events.EventType, id: "auto-map-user-created"
defstruct [:name, :email]end
defmodule MyApp.Events.AutoMapUserRenamed do use Cratis.Chronicle.Events.EventType, id: "auto-map-user-renamed"
defstruct [:name]end
defmodule MyApp.ReadModels.AutoMapUser do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email]end
defmodule MyApp.Projections.AutoMapUserProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.AutoMapUser
alias MyApp.Events.{AutoMapUserCreated, AutoMapUserRenamed}
from AutoMapUserCreated from AutoMapUserRenamedendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class AutoMapUserCreated { @field(String) readonly name: string; @field(String) readonly email: string;
constructor(name: string, email: string) { this.name = name; this.email = email; }}
@eventType()export class AutoMapUserRenamed { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
export class AutoMapUser { name = ''; email = '';}
@projection('', AutoMapUser)export class AutoMapUserProjection implements IProjectionFor<AutoMapUser> { define(builder: IProjectionBuilderFor<AutoMapUser>): void { builder .from(AutoMapUserCreated) .from(AutoMapUserRenamed); }}How AutoMap works
Section titled “How AutoMap works”For events that supply read model values, AutoMap uses these rules:
| Rule | Behavior |
|---|---|
| Property names | Chronicle compares serialized schema property names without regard to case. The client’s naming policy determines those serialized names. |
| Property types | Choose event values compatible with the target property type. |
| Explicit mappings | An explicit mapping takes precedence over AutoMap for the same target property in that event mapping. |
| Scope | Child scopes inherit the parent’s setting unless they override it. |
The kernel resolves matching properties when it constructs the projection’s mappings from the schemas, rather than discovering them through reflection for each event.
Disable AutoMap
Section titled “Disable AutoMap”Disable AutoMap when a projection should map only the properties you name explicitly.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record AutoMapDisabledAccountRegistered(string AccountName, string ContactEmail);
public record AutoMapDisabledAccount( string Name, string Email, DateTimeOffset CreatedAt);
public class AutoMapDisabledAccountProjection : IProjectionFor<AutoMapDisabledAccount>{ public void Define(IProjectionBuilderFor<AutoMapDisabledAccount> builder) => builder .NoAutoMap() .From<AutoMapDisabledAccountRegistered>(_ => _ .Set(m => m.Name).To(e => e.AccountName) .Set(m => m.Email).To(e => e.ContactEmail) .Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred));}// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class AutoMapDisabledAccountRegistered(val accountName: String, val contactEmail: String)
data class AutoMapDisabledAccount( val name: String = "", val email: String = "", val createdAt: String = "")
class AutoMapDisabledAccountProjection : IProjectionFor<AutoMapDisabledAccount> { override fun define(builder: IProjectionBuilderFor<AutoMapDisabledAccount>) { builder.noAutoMap() .from(AutoMapDisabledAccountRegistered::class) { it.set(AutoMapDisabledAccount::name).toProperty("accountName") it.set(AutoMapDisabledAccount::email).toProperty("contactEmail") it.set(AutoMapDisabledAccount::createdAt).toEventContextProperty("occurred") } }}// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord AutoMapDisabledAccountRegistered(String accountName, String contactEmail) {}
class AutoMapDisabledAccount { public String name = ""; public String email = ""; public String createdAt = "";}
class AutoMapDisabledAccountProjection implements IProjectionFor<AutoMapDisabledAccount> { @Override public void define(IProjectionBuilderFor<AutoMapDisabledAccount> builder) { builder.noAutoMap() .from(AutoMapDisabledAccountRegistered.class, fb -> { fb.<String>set("name").toProperty("accountName"); fb.<String>set("email").toProperty("contactEmail"); fb.<String>set("createdAt").toEventContextProperty("occurred"); }); }}defmodule MyApp.Events.AutoMapDisabledAccountRegistered do use Cratis.Chronicle.Events.EventType, id: "auto-map-disabled-account-registered"
defstruct [:account_name, :contact_email]end
defmodule MyApp.ReadModels.AutoMapDisabledAccount do use Cratis.Chronicle.ReadModels.ReadModel
defstruct name: nil, email: nil, created_at: nil
# Nothing is mapped by name - every property has to be stated. no_auto_map()
from MyApp.Events.AutoMapDisabledAccountRegistered, set: [ name: :account_name, email: :contact_email, created_at: :occurred ]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class AutoMapDisabledAccountRegistered { @field(String) readonly accountName: string; @field(String) readonly contactEmail: string;
constructor(accountName: string, contactEmail: string) { this.accountName = accountName; this.contactEmail = contactEmail; }}
export class AutoMapDisabledAccount { name = ''; email = ''; createdAt = new Date(0);}
@projection('', AutoMapDisabledAccount)export class AutoMapDisabledAccountProjection implements IProjectionFor<AutoMapDisabledAccount> { define(builder: IProjectionBuilderFor<AutoMapDisabledAccount>): void { builder .noAutoMap() .from(AutoMapDisabledAccountRegistered, _ => _ .set(m => m.name).to(e => e.accountName) .set(m => m.email).to(e => e.contactEmail) .set(m => m.createdAt).toEventContextProperty('occurred')); }}Re-enable AutoMap in a child scope
Section titled “Re-enable AutoMap in a child scope”At the top level, AutoMap() is already the default. Call it in a child scope when the parent uses NoAutoMap() but the child’s properties should still map by name.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record AutoMapTeamFormed(string TeamName);
[EventType]public record AutoMapMemberJoinedTeam(string MemberId, string DisplayName);
public record AutoMapTeamMember(string MemberId, string DisplayName);
public record AutoMapTeam( string Name, DateTimeOffset CreatedAt, IEnumerable<AutoMapTeamMember> Members);
public class AutoMapTeamProjection : IProjectionFor<AutoMapTeam>{ public void Define(IProjectionBuilderFor<AutoMapTeam> builder) => builder .NoAutoMap() .From<AutoMapTeamFormed>(_ => _ .Set(m => m.Name).To(e => e.TeamName) .Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred)) .Children(m => m.Members, children => children .IdentifiedBy(m => m.MemberId) .AutoMap() .From<AutoMapMemberJoinedTeam>(_ => _ .UsingKey(e => e.MemberId)));}Kotlin does not support this workflow yet.`IProjectionBuilderFor` has `noAutoMap()` and `autoMap()`, but `IChildrenBuilderFor` has noAutoMap toggle, so there is no child scope to re-enable AutoMap on.Java does not support this workflow yet.`IProjectionBuilderFor` has `noAutoMap()` and `autoMap()`, but `IChildrenBuilderFor` has noAutoMap toggle, so there is no child scope to re-enable AutoMap on.Elixir does not support this workflow yet.import { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class AutoMapTeamFormed { @field(String) readonly teamName: string;
constructor(teamName: string) { this.teamName = teamName; }}
@eventType()class AutoMapMemberJoinedTeam { @field(String) readonly memberId: string; @field(String) readonly displayName: string;
constructor(memberId: string, displayName: string) { this.memberId = memberId; this.displayName = displayName; }}
class AutoMapTeamMember { memberId = ''; displayName = '';}
class AutoMapTeam { name = ''; createdAt = new Date(); members: AutoMapTeamMember[] = [];}
@projection()class AutoMapTeamProjection implements IProjectionFor<AutoMapTeam> { define(builder: IProjectionBuilderFor<AutoMapTeam>): void { builder .noAutoMap() .from(AutoMapTeamFormed, _ => _ .set(m => m.name).to(e => e.teamName) .set(m => m.createdAt).toEventContextProperty('occurred')) .children<AutoMapTeamMember>(m => m.members, children => children .identifiedBy(m => m.memberId) .autoMap() .from(AutoMapMemberJoinedTeam, _ => _ .usingKey(e => e.memberId))); }}Combine with explicit mappings
Section titled “Combine with explicit mappings”Leave AutoMap enabled for matching properties and add explicit mappings for the exceptions. Here, Name and Email come from event properties, while Status is a constant and CreatedAt comes from event context.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record AutoMapAccountOpened(string Name, string Email);
[EventType]public record AutoMapAccountEmailChanged(string Email);
public record AutoMapAccount( string Name, string Email, string Status, DateTimeOffset CreatedAt);
public class AutoMapAccountProjection : IProjectionFor<AutoMapAccount>{ public void Define(IProjectionBuilderFor<AutoMapAccount> builder) => builder .From<AutoMapAccountOpened>(_ => _ .Set(m => m.Status).ToValue("Active") .Set(m => m.CreatedAt).ToEventContextProperty(c => c.Occurred)) .From<AutoMapAccountEmailChanged>();}// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class AutoMapAccountOpened(val name: String, val email: String)
@EventTypedata class AutoMapAccountEmailChanged(val email: String)
data class AutoMapAccount( val name: String = "", val email: String = "", val status: String = "", val createdAt: String = "")
class AutoMapAccountProjection : IProjectionFor<AutoMapAccount> { override fun define(builder: IProjectionBuilderFor<AutoMapAccount>) { builder .from(AutoMapAccountOpened::class) { it.set(AutoMapAccount::status).toValue("Active") it.set(AutoMapAccount::createdAt).toEventContextProperty("occurred") } .from(AutoMapAccountEmailChanged::class) }}// Requires io.cratis:chronicle 6.5.0 or later.import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord AutoMapAccountOpened(String name, String email) {}
@EventTyperecord AutoMapAccountEmailChanged(String email) {}
class AutoMapAccount { public String name = ""; public String email = ""; public String status = ""; public String createdAt = "";}
class AutoMapAccountProjection implements IProjectionFor<AutoMapAccount> { @Override public void define(IProjectionBuilderFor<AutoMapAccount> builder) { builder .from(AutoMapAccountOpened.class, fb -> { fb.<String>set("status").toValue("Active"); fb.<String>set("createdAt").toEventContextProperty("occurred"); }) .from(AutoMapAccountEmailChanged.class); }}defmodule MyApp.Events.AutoMapAccountOpened do use Cratis.Chronicle.Events.EventType, id: "auto-map-account-opened"
defstruct [:name, :email]end
defmodule MyApp.Events.AutoMapAccountEmailChanged do use Cratis.Chronicle.Events.EventType, id: "auto-map-account-email-changed"
defstruct [:email]end
defmodule MyApp.ReadModels.AutoMapAccount do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email, :status, :created_at]end
defmodule MyApp.Projections.AutoMapAccountProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.AutoMapAccount
alias MyApp.Events.{AutoMapAccountEmailChanged, AutoMapAccountOpened}
# `name` and `email` are mapped automatically by AutoMap; only the # exceptions below need an explicit set:. from AutoMapAccountOpened, set: [ status: "$value(Active)", created_at: :occurred ]
# Uses AutoMap for `email` — no set: list needed. from AutoMapAccountEmailChangedendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class AutoMapAccountOpened { @field(String) readonly name: string; @field(String) readonly email: string;
constructor(name: string, email: string) { this.name = name; this.email = email; }}
@eventType()export class AutoMapAccountEmailChanged { @field(String) readonly email: string;
constructor(email: string) { this.email = email; }}
export class AutoMapAccount { name = ''; email = ''; status = ''; createdAt = new Date(0);}
@projection('', AutoMapAccount)export class AutoMapAccountProjection implements IProjectionFor<AutoMapAccount> { define(builder: IProjectionBuilderFor<AutoMapAccount>): void { builder .from(AutoMapAccountOpened, _ => _ .set(m => m.status).toValue('Active') .set(m => m.createdAt).toEventContextProperty('occurred')) .from(AutoMapAccountEmailChanged); }}AutoMap with joins
Section titled “AutoMap with joins”AutoMap also applies to joined events. The join condition finds the read model to update; AutoMap supplies matching values such as DepartmentName.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record AutoMapEmployeeHired(string EmployeeName, string DepartmentId);
[EventType]public record AutoMapDepartmentRenamed(string DepartmentName);
public record AutoMapEmployee( string EmployeeName, string DepartmentId, string DepartmentName);
public class AutoMapEmployeeProjection : IProjectionFor<AutoMapEmployee>{ public void Define(IProjectionBuilderFor<AutoMapEmployee> builder) => builder .From<AutoMapEmployeeHired>() .Join<AutoMapDepartmentRenamed>(_ => _ .On(m => m.DepartmentId));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class AutoMapEmployeeHired(val employeeName: String, val departmentId: String)
@EventTypedata class AutoMapDepartmentRenamed(val departmentName: String)
data class AutoMapEmployee( val employeeName: String = "", val departmentId: String = "", val departmentName: String = "")
class AutoMapEmployeeProjection : IProjectionFor<AutoMapEmployee> { override fun define(builder: IProjectionBuilderFor<AutoMapEmployee>) { builder .from(AutoMapEmployeeHired::class) .join(AutoMapDepartmentRenamed::class) { it.on(AutoMapEmployee::departmentId) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord AutoMapEmployeeHired(String employeeName, String departmentId) {}
@EventTyperecord AutoMapDepartmentRenamed(String departmentName) {}
class AutoMapEmployee { public String employeeName = ""; public String departmentId = ""; public String departmentName = "";}
class AutoMapEmployeeProjection implements IProjectionFor<AutoMapEmployee> { @Override public void define(IProjectionBuilderFor<AutoMapEmployee> builder) { builder .from(AutoMapEmployeeHired.class) .join(AutoMapDepartmentRenamed.class, jb -> { jb.on("departmentId"); }); }}defmodule MyApp.Events.AutoMapEmployeeHired do use Cratis.Chronicle.Events.EventType, id: "auto-map-employee-hired"
defstruct [:employee_name, :department_id]end
defmodule MyApp.Events.AutoMapDepartmentRenamed do use Cratis.Chronicle.Events.EventType, id: "auto-map-department-renamed"
defstruct [:department_name]end
defmodule MyApp.ReadModels.AutoMapEmployee do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:employee_name, :department_id, :department_name]end
defmodule MyApp.Projections.AutoMapEmployeeProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.AutoMapEmployee
alias MyApp.Events.{AutoMapEmployeeHired, AutoMapDepartmentRenamed}
from AutoMapEmployeeHired
join AutoMapDepartmentRenamed, on: :department_idendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class AutoMapEmployeeHired { @field(String) readonly employeeName: string; @field(String) readonly departmentId: string;
constructor(employeeName: string, departmentId: string) { this.employeeName = employeeName; this.departmentId = departmentId; }}
@eventType()export class AutoMapDepartmentRenamed { @field(String) readonly departmentName: string;
constructor(departmentName: string) { this.departmentName = departmentName; }}
export class AutoMapEmployee { employeeName = ''; departmentId = ''; departmentName = '';}
@projection('', AutoMapEmployee)export class AutoMapEmployeeProjection implements IProjectionFor<AutoMapEmployee> { define(builder: IProjectionBuilderFor<AutoMapEmployee>): void { builder .from(AutoMapEmployeeHired) .join(AutoMapDepartmentRenamed, _ => _ .on(m => m.departmentId)); }}Aggregate-only events
Section titled “Aggregate-only events”When an event’s mappings consist only of Count, Increment, Decrement, Add, or Subtract, AutoMap does not copy its other properties. In this example, counting submitted candidates leaves the arrangement’s Location unchanged, even though the candidate event also has a Location property. You do not need to disable AutoMap for this behavior.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DeclAggArrangementSet(string Location);
[EventType]public record DeclAggCandidateSubmitted(string Name, string Location);
public record DeclAggAssignmentSummary(string Location, int CandidateCount);
public class DeclAggAssignmentProjection : IProjectionFor<DeclAggAssignmentSummary>{ public void Define(IProjectionBuilderFor<DeclAggAssignmentSummary> builder) => builder .From<DeclAggArrangementSet>() .From<DeclAggCandidateSubmitted>(_ => _ .Count(m => m.CandidateCount));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DeclAggArrangementSet(val location: String)
@EventTypedata class DeclAggCandidateSubmitted(val name: String, val location: String)
data class DeclAggAssignmentSummary(val location: String = "", val candidateCount: Int = 0)
class DeclAggAssignmentProjection : IProjectionFor<DeclAggAssignmentSummary> { override fun define(builder: IProjectionBuilderFor<DeclAggAssignmentSummary>) { builder .from(DeclAggArrangementSet::class) .from(DeclAggCandidateSubmitted::class) { it.count(DeclAggAssignmentSummary::candidateCount) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DeclAggArrangementSet(String location) {}
@EventTyperecord DeclAggCandidateSubmitted(String name, String location) {}
class DeclAggAssignmentSummary { public String location = ""; public int candidateCount = 0;}
class DeclAggAssignmentProjection implements IProjectionFor<DeclAggAssignmentSummary> { @Override public void define(IProjectionBuilderFor<DeclAggAssignmentSummary> builder) { builder .from(DeclAggArrangementSet.class) .from(DeclAggCandidateSubmitted.class, fb -> { fb.count("candidateCount"); }); }}defmodule MyApp.Events.DeclAggArrangementSet do use Cratis.Chronicle.Events.EventType, id: "decl-agg-arrangement-set-v1"
defstruct [:location]end
defmodule MyApp.Events.DeclAggCandidateSubmitted do use Cratis.Chronicle.Events.EventType, id: "decl-agg-candidate-submitted-v1"
defstruct [:name, :location]end
defmodule MyApp.ReadModels.DeclAggAssignmentSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:location, candidate_count: 0]end
defmodule MyApp.Projections.DeclAggAssignmentProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DeclAggAssignmentSummary
alias MyApp.Events.{DeclAggArrangementSet, DeclAggCandidateSubmitted}
from DeclAggArrangementSet
# DeclAggCandidateSubmitted is subscribed only to be counted, so its own # `location` field is never mapped over the value set from # DeclAggArrangementSet. from DeclAggCandidateSubmitted, count: :candidate_countendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DeclAggArrangementSet { @field(String) readonly location: string;
constructor(location: string) { this.location = location; }}
@eventType()class DeclAggCandidateSubmitted { @field(String) readonly name: string; @field(String) readonly location: string;
constructor(name: string, location: string) { this.name = name; this.location = location; }}
class DeclAggAssignmentSummary { location = ''; candidateCount = 0;}
@projection()class DeclAggAssignmentProjection implements IProjectionFor<DeclAggAssignmentSummary> { define(builder: IProjectionBuilderFor<DeclAggAssignmentSummary>): void { builder .from(DeclAggArrangementSet) .from(DeclAggCandidateSubmitted, _ => _ .count(m => m.candidateCount)); }}When to use AutoMap
Section titled “When to use AutoMap”Use AutoMap for properties that should retain the event’s name and value. Add explicit mappings for renamed properties, constants, event context, and calculations. If you need every assignment to be visible in the projection definition, disable AutoMap and map each property yourself.
Continue with set properties for the available value sources.
Related projection shapes
Section titled “Related projection shapes”To apply these mappings inside a larger read model, choose the shape you need: