Chronicle projection joins across event streams
Use a join in a Cratis Chronicle projection when one read model needs facts from separate event streams, such as a user and the group they belong to. The read model stores a join key, and Chronicle uses it to find the related events and apply their property mappings.
Defining a projection with joins
Section titled “Defining a projection with joins”Use From for the events that establish the read model and Join for related events. In the .NET builder, .On(m => m.GroupId) selects the read model property that identifies the group stream:
using Cratis.Chronicle.Projections;
public class DecJoinsUserProjection : IProjectionFor<DecJoinsUser>{ public void Define(IProjectionBuilderFor<DecJoinsUser> builder) => builder .From<DecJoinsUserCreated>() .From<DecJoinsUserAssignedToGroup>(b => b .UsingKey(e => e.UserId) .Set(m => m.GroupId).ToEventSourceId()) .Join<DecJoinsGroupCreated>(j => j .On(m => m.GroupId)) .Join<DecJoinsGroupRenamed>(j => j .On(m => m.GroupId));}import io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
class DecJoinsUserProjection : IProjectionFor<DecJoinsUser> { override fun define(builder: IProjectionBuilderFor<DecJoinsUser>) { builder .from(DecJoinsUserCreated::class) .from(DecJoinsUserAssignedToGroup::class) { it.usingKey("userId") it.set(DecJoinsUser::groupId).toEventSourceId() } .join(DecJoinsGroupCreated::class) { it.on(DecJoinsUser::groupId) } .join(DecJoinsGroupRenamed::class) { it.on(DecJoinsUser::groupId) } }}import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
class DecJoinsUserProjection implements IProjectionFor<DecJoinsUser> { @Override public void define(IProjectionBuilderFor<DecJoinsUser> builder) { builder .from(DecJoinsUserCreated.class) .from(DecJoinsUserAssignedToGroup.class, fb -> { fb.usingKey("userId"); fb.set("groupId").toEventSourceId(); }) .join(DecJoinsGroupCreated.class, jb -> { jb.on("groupId"); }) .join(DecJoinsGroupRenamed.class, jb -> { jb.on("groupId"); }); }}defmodule MyApp.Projections.DecJoinsUserProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecJoinsUser
alias MyApp.Events.{ DecJoinsUserCreated, DecJoinsUserAssignedToGroup, DecJoinsGroupCreated, DecJoinsGroupRenamed }
from DecJoinsUserCreated
from DecJoinsUserAssignedToGroup, key: :user_id, set: [group_id: :event_source_id]
join DecJoinsGroupCreated, on: :group_id join DecJoinsGroupRenamed, on: :group_idendimport { IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@projection()class DecJoinsUserProjection implements IProjectionFor<DecJoinsUser> { define(builder: IProjectionBuilderFor<DecJoinsUser>): void { builder .autoMap() .from(DecJoinsUserCreated) .from(DecJoinsUserAssignedToGroup, b => b .usingKey(e => e.userId) .set(m => m.groupId).toEventSourceId()) .join(DecJoinsGroupCreated, j => j .on(m => m.groupId)) .join(DecJoinsGroupRenamed, j => j .on(m => m.groupId)); }}Read model with joined data
Section titled “Read model with joined data”The model can keep user fields alongside group fields. The following shape separates Name from GroupName, so those fields need different mappings:
public record DecJoinsUser( string Name, string Email, string? GroupId, string? GroupName, string? GroupDescription);data class DecJoinsUser( val name: String = "", val email: String = "", val groupId: String? = null, val groupName: String? = null, val groupDescription: String? = null)class DecJoinsUser { public String name = ""; public String email = ""; public String groupId = null; public String groupName = null; public String groupDescription = null;}defmodule MyApp.ReadModels.DecJoinsUser do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:name, :email, :group_id, :group_name, :group_description]endclass DecJoinsUser { name = ''; email = ''; groupId: string | null = null; groupName: string | null = null; groupDescription: string | null = null;}On selects the relationship; it does not rename properties. For this shape, explicitly map the group-created event’s Name to GroupName and Description to GroupDescription, then map the rename event’s NewName to GroupName. For example, add .Set(m => m.GroupName).To(e => e.NewName) inside the rename join. With AutoMap enabled, a group event’s Name would otherwise target the user’s same-named Name property, not GroupName. See AutoMap to control name-based mapping.
Event definitions
Section titled “Event definitions”Append group-created and group-renamed events with the group’s event source ID. In this example, the assignment event also belongs to the group stream; its UserId selects the user read model, and .ToEventSourceId() records the group stream’s ID as GroupId:
using Cratis.Chronicle.Events;
// User stream events[EventType]public record DecJoinsUserCreated(string Name, string Email);
[EventType]public record DecJoinsUserAssignedToGroup(string UserId, string GroupId);
// Group stream events[EventType]public record DecJoinsGroupCreated(string Name, string Description);
[EventType]public record DecJoinsGroupRenamed(string NewName);import io.cratis.chronicle.events.EventType
// User stream events@EventTypedata class DecJoinsUserCreated(val name: String, val email: String)
@EventTypedata class DecJoinsUserAssignedToGroup(val userId: String, val groupId: String)
// Group stream events@EventTypedata class DecJoinsGroupCreated(val name: String, val description: String)
@EventTypedata class DecJoinsGroupRenamed(val newName: String)import io.cratis.chronicle.events.EventType;
// User stream events@EventTyperecord DecJoinsUserCreated(String name, String email) {}
@EventTyperecord DecJoinsUserAssignedToGroup(String userId, String groupId) {}
// Group stream events@EventTyperecord DecJoinsGroupCreated(String name, String description) {}
@EventTyperecord DecJoinsGroupRenamed(String newName) {}defmodule MyApp.Events.DecJoinsUserCreated do use Cratis.Chronicle.Events.EventType, id: "dec-joins-user-created"
defstruct [:name, :email]end
defmodule MyApp.Events.DecJoinsUserAssignedToGroup do use Cratis.Chronicle.Events.EventType, id: "dec-joins-user-assigned-to-group"
defstruct [:user_id, :group_id]end
defmodule MyApp.Events.DecJoinsGroupCreated do use Cratis.Chronicle.Events.EventType, id: "dec-joins-group-created"
defstruct [:name, :description]end
defmodule MyApp.Events.DecJoinsGroupRenamed do use Cratis.Chronicle.Events.EventType, id: "dec-joins-group-renamed"
defstruct [:new_name]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
// User stream events@eventType()class DecJoinsUserCreated { @field(String) readonly name: string; @field(String) readonly email: string;
constructor(name: string, email: string) { this.name = name; this.email = email; }}
@eventType()class DecJoinsUserAssignedToGroup { @field(String) readonly userId: string; @field(String) readonly groupId: string;
constructor(userId: string, groupId: string) { this.userId = userId; this.groupId = groupId; }}
// Group stream events@eventType()class DecJoinsGroupCreated { @field(String) readonly name: string; @field(String) readonly description: string;
constructor(name: string, description: string) { this.name = name; this.description = description; }}
@eventType()class DecJoinsGroupRenamed { @field(String) readonly newName: string;
constructor(newName: string) { this.newName = newName; }}How joins work
Section titled “How joins work”A From event creates or updates a read model and can set the join key. When a related event arrives, Chronicle matches its key against the read model’s On property and applies the joined mappings. For a default join, that event key is its event source ID.
Chronicle also resolves earlier joined events when processing a From event. This backfill lets a newly established relationship pick up data already present in the joined stream. Joined mappings run after From mappings, so they take precedence when both write the same property.
Join scenarios
Section titled “Join scenarios”Setting join keys
Section titled “Setting join keys”Set the join key from the event that establishes the relationship. The assignment mapping above uses .UsingKey(e => e.UserId) to select the user and .Set(m => m.GroupId).ToEventSourceId() to store the group ID. If your assignment event belongs to the user stream instead, map GroupId from the event’s group-ID property rather than its event source ID.
Joining on the key
Section titled “Joining on the key”Declare a join for each related event type you want to handle. Both group-created and group-renamed joins use .On(m => m.GroupId) because they refer to the same relationship. Their property mappings determine which group fields change.
Joining by an event property
Section titled “Joining by an event property”For a root join, .UsingKey(e => e.X) matches the read model’s On property against that event property instead of the event source ID. If the property is missing or null, Chronicle falls back to the event source ID. Context, constant, and composite key expressions are not supported for root joins: Chronicle logs a warning and uses the event source ID instead.
Append the root before the keyed join event when its key differs from its event source ID. If the joined event arrives first, backfill still looks it up by event source ID, not by the explicit key property.
Join precedence
Section titled “Join precedence”Do not use arrival order to decide whether a From mapping or a join owns a property. On a From event, Chronicle resolves the latest stored event of each joined type before that event’s sequence number and applies its mappings after the From mappings. A local write cannot clear a value that this join resolution reapplies; there is no dedicated “clear this joined value” operation.
If a field must reflect which fact happened most recently, store each fact separately, such as two timestamps, and compare them. The CHR0042 analyzer warns when a From mapping and a join write the same property.
Multiple joins
Section titled “Multiple joins”Use a separate join key for each relationship when the read model needs data from several streams. Map each source into its intended fields, such as GroupName, DepartmentName, and LocationAddress; differently named fields need explicit mappings:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecJoinsMultipleEmployeeAssigned(string GroupId, string DepartmentId, string LocationId);
[EventType]public record DecJoinsMultipleGroupCreated(string Name);
[EventType]public record DecJoinsMultipleDepartmentCreated(string Name);
[EventType]public record DecJoinsMultipleLocationUpdated(string Address);
public record DecJoinsMultipleEmployeeSummary( string? GroupId, string? GroupName, string? DepartmentId, string? DepartmentName, string? LocationId, string? LocationAddress);
public class DecJoinsMultipleEmployeeSummaryProjection : IProjectionFor<DecJoinsMultipleEmployeeSummary>{ public void Define(IProjectionBuilderFor<DecJoinsMultipleEmployeeSummary> builder) => builder .From<DecJoinsMultipleEmployeeAssigned>() .Join<DecJoinsMultipleGroupCreated>(j => j.On(m => m.GroupId)) .Join<DecJoinsMultipleDepartmentCreated>(j => j.On(m => m.DepartmentId)) .Join<DecJoinsMultipleLocationUpdated>(j => j.On(m => m.LocationId));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecJoinsMultipleEmployeeAssigned(val groupId: String, val departmentId: String, val locationId: String)
@EventTypedata class DecJoinsMultipleGroupCreated(val name: String)
@EventTypedata class DecJoinsMultipleDepartmentCreated(val name: String)
@EventTypedata class DecJoinsMultipleLocationUpdated(val address: String)
data class DecJoinsMultipleEmployeeSummary( val groupId: String? = null, val groupName: String? = null, val departmentId: String? = null, val departmentName: String? = null, val locationId: String? = null, val locationAddress: String? = null)
class DecJoinsMultipleEmployeeSummaryProjection : IProjectionFor<DecJoinsMultipleEmployeeSummary> { override fun define(builder: IProjectionBuilderFor<DecJoinsMultipleEmployeeSummary>) { builder .from(DecJoinsMultipleEmployeeAssigned::class) .join(DecJoinsMultipleGroupCreated::class) { it.on(DecJoinsMultipleEmployeeSummary::groupId) } .join(DecJoinsMultipleDepartmentCreated::class) { it.on(DecJoinsMultipleEmployeeSummary::departmentId) } .join(DecJoinsMultipleLocationUpdated::class) { it.on(DecJoinsMultipleEmployeeSummary::locationId) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecJoinsMultipleEmployeeAssigned(String groupId, String departmentId, String locationId) {}
@EventTyperecord DecJoinsMultipleGroupCreated(String name) {}
@EventTyperecord DecJoinsMultipleDepartmentCreated(String name) {}
@EventTyperecord DecJoinsMultipleLocationUpdated(String address) {}
class DecJoinsMultipleEmployeeSummary { public String groupId = null; public String groupName = null; public String departmentId = null; public String departmentName = null; public String locationId = null; public String locationAddress = null;}
class DecJoinsMultipleEmployeeSummaryProjection implements IProjectionFor<DecJoinsMultipleEmployeeSummary> { @Override public void define(IProjectionBuilderFor<DecJoinsMultipleEmployeeSummary> builder) { builder .from(DecJoinsMultipleEmployeeAssigned.class) .join(DecJoinsMultipleGroupCreated.class, jb -> { jb.on("groupId"); }) .join(DecJoinsMultipleDepartmentCreated.class, jb -> { jb.on("departmentId"); }) .join(DecJoinsMultipleLocationUpdated.class, jb -> { jb.on("locationId"); }); }}defmodule MyApp.Events.DecJoinsMultipleEmployeeAssigned do use Cratis.Chronicle.Events.EventType, id: "dec-joins-multiple-employee-assigned"
defstruct [:group_id, :department_id, :location_id]end
defmodule MyApp.Events.DecJoinsMultipleGroupCreated do use Cratis.Chronicle.Events.EventType, id: "dec-joins-multiple-group-created"
defstruct [:name]end
defmodule MyApp.Events.DecJoinsMultipleDepartmentCreated do use Cratis.Chronicle.Events.EventType, id: "dec-joins-multiple-department-created"
defstruct [:name]end
defmodule MyApp.Events.DecJoinsMultipleLocationUpdated do use Cratis.Chronicle.Events.EventType, id: "dec-joins-multiple-location-updated"
defstruct [:address]end
defmodule MyApp.ReadModels.DecJoinsMultipleEmployeeSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:group_id, :group_name, :department_id, :department_name, :location_id, :location_address]end
defmodule MyApp.Projections.DecJoinsMultipleEmployeeSummaryProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecJoinsMultipleEmployeeSummary
alias MyApp.Events.{ DecJoinsMultipleEmployeeAssigned, DecJoinsMultipleGroupCreated, DecJoinsMultipleDepartmentCreated, DecJoinsMultipleLocationUpdated }
from DecJoinsMultipleEmployeeAssigned
join DecJoinsMultipleGroupCreated, on: :group_id join DecJoinsMultipleDepartmentCreated, on: :department_id join DecJoinsMultipleLocationUpdated, on: :location_idendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class DecJoinsMultipleEmployeeAssigned { groupId = ''; departmentId = ''; locationId = '';}
@eventType()class DecJoinsMultipleGroupCreated { name = '';}
@eventType()class DecJoinsMultipleDepartmentCreated { name = '';}
@eventType()class DecJoinsMultipleLocationUpdated { address = '';}
class DecJoinsMultipleEmployeeSummary { groupId: string | null = null; groupName: string | null = null; departmentId: string | null = null; departmentName: string | null = null; locationId: string | null = null; locationAddress: string | null = null;}
@projection()class DecJoinsMultipleEmployeeSummaryProjection implements IProjectionFor<DecJoinsMultipleEmployeeSummary> { define(builder: IProjectionBuilderFor<DecJoinsMultipleEmployeeSummary>): void { builder .autoMap() .from(DecJoinsMultipleEmployeeAssigned) .join(DecJoinsMultipleGroupCreated, j => j.on(m => m.groupId)) .join(DecJoinsMultipleDepartmentCreated, j => j.on(m => m.departmentId)) .join(DecJoinsMultipleLocationUpdated, j => j.on(m => m.locationId)); }}Joining children
Section titled “Joining children”Inside a .NET Children builder, the join uses the child’s IdentifiedBy property as its target, so do not specify .On(). By default, the joined event’s event source ID selects the child. Here each project-board member is identified by the member stream’s ID, allowing a member-registration event to populate that child’s name:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecJoinsChildMemberAssignedToProject(string ProjectId, string MemberId);
[EventType]public record DecJoinsChildMemberRegistered(string Name);
public record DecJoinsChildProjectMember(string MemberId, string? Name);
public record DecJoinsChildProjectBoard(IEnumerable<DecJoinsChildProjectMember> Members);
public class DecJoinsChildProjectBoardProjection : IProjectionFor<DecJoinsChildProjectBoard>{ public void Define(IProjectionBuilderFor<DecJoinsChildProjectBoard> builder) => builder .Children(m => m.Members, children => children .IdentifiedBy(e => e.MemberId) .From<DecJoinsChildMemberAssignedToProject>(b => b .UsingParentKey(e => e.ProjectId) .UsingKey(e => e.MemberId)) .Join<DecJoinsChildMemberRegistered>());}Kotlin does not support this workflow yet.`IChildrenBuilderFor` has no `join()` method — joins are only available on the top-level`IProjectionBuilderFor<TReadModel>`, not inside a child collection scope.Java does not support this workflow yet.`IChildrenBuilderFor` has no `join()` method — joins are only available on the top-level`IProjectionBuilderFor<TReadModel>`, not inside a child collection scope.defmodule MyApp.Events.DecJoinsChildMemberAssignedToProject do use Cratis.Chronicle.Events.EventType, id: "dec-joins-child-member-assigned-to-project"
defstruct [:project_id, :member_id]end
defmodule MyApp.Events.DecJoinsChildMemberRegistered do use Cratis.Chronicle.Events.EventType, id: "dec-joins-child-member-registered"
defstruct [:name]end
defmodule MyApp.ReadModels.DecJoinsChildProjectMember do use Cratis.Chronicle.ReadModels.ReadModel
defstruct member_id: nil, name: nilend
defmodule MyApp.ReadModels.DecJoinsChildProjectBoard do use Cratis.Chronicle.ReadModels.ReadModel, types: [members: {:list, MyApp.ReadModels.DecJoinsChildProjectMember}]
defstruct members: []end
defmodule MyApp.Projections.DecJoinsChildProjectBoardProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecJoinsChildProjectBoard
children :members, identified_by: :member_id do from MyApp.Events.DecJoinsChildMemberAssignedToProject, parent_key: :project_id, key: :member_id, set: [member_id: :member_id]
join MyApp.Events.DecJoinsChildMemberRegistered, on: :member_id, set: [name: :name] endendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DecJoinsChildTaskAssigned { @field(String) readonly taskId: string; @field(String) readonly projectId: string;
constructor(taskId: string, projectId: string) { this.taskId = taskId; this.projectId = projectId; }}
@eventType()class DecJoinsChildProjectCreated { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
class DecJoinsChildTask { taskId = ''; projectId = ''; projectName: string | null = null;}
class DecJoinsChildProjectBoard { tasks: DecJoinsChildTask[] = [];}
@projection()class DecJoinsChildProjectBoardProjection implements IProjectionFor<DecJoinsChildProjectBoard> { define(builder: IProjectionBuilderFor<DecJoinsChildProjectBoard>): void { builder .children<DecJoinsChildTask>(m => m.tasks, children => children .identifiedBy(e => e.taskId) .autoMap() .from(DecJoinsChildTaskAssigned, b => b .usingKey(e => e.taskId)) .join(DecJoinsChildProjectCreated, j => j .on(m => m.projectId))); }}Automatic mapping in declaration-language joins
Section titled “Automatic mapping in declaration-language joins”In the declaration language, a with <Event> block inherits the projection’s automap setting. Put no automap inside that block to use only explicit mappings, or automap to enable name-based mapping when the enclosing projection disables it. The setting also applies during backfill.
Per-join automap and no automap are declaration-language only: the .NET JoinBuilder and the C#, Kotlin, and TypeScript code generators do not express them. Generated code will not preserve a per-join override, so keep the declaration as the source of truth when using one.
Performance considerations
Section titled “Performance considerations”A joined event can update several read models that refer to the same key. Consider how many instances share each relationship and how often the joined data changes when choosing what belongs in the projection.
For related data owned by one parent, compare Children and Nested objects. They model collections and optional objects within the parent rather than joining independent streams.