Nested model-bound projections: populate and clear one object
A nested model-bound projection lets events populate, update, or clear one nullable object inside a parent read model. Use it for an optional part of the view, such as a command definition, whose fields change together. Use a child collection when the parent can contain several independently identified items.
The examples below use the .NET [Nested] attribute. TypeScript exposes @nested, and Kotlin and Java expose @Nested.
Basic Lifecycle
Section titled “Basic Lifecycle”Mark the nullable parent property as nested, then declare the populate events on the nested type. Add clear events if the relationship can end:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedBasic(string Name, string Schema);
[EventType]public record CommandClearedForNestedBasic;
[FromEvent<CommandSetForNestedBasic>]public record SliceWithNestedCommandBasic( [Key] Guid Id, string Name, [Nested] CommandItemNestedBasic? Command);
[FromEvent<CommandSetForNestedBasic>][ClearWith<CommandClearedForNestedBasic>]public record CommandItemNestedBasic( string Name, string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.Nestedimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class CommandSetForNestedBasic(val name: String, val schema: String)
@EventTypedata class CommandClearedForNestedBasic(val placeholder: Boolean = true)
@ReadModel@FromEvent(CommandSetForNestedBasic::class)data class SliceWithNestedCommandBasic( val name: String = "",
@Nested val command: CommandItemNestedBasic? = null)
@FromEvent(CommandSetForNestedBasic::class)@ClearWith(CommandClearedForNestedBasic::class)data class CommandItemNestedBasic( val name: String = "", val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.Nested;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord CommandSetForNestedBasic(String name, String schema) {}
@EventTyperecord CommandClearedForNestedBasic() {}
@ReadModel@FromEvent(eventType = CommandSetForNestedBasic.class)record SliceWithNestedCommandBasic( String name,
@Nested CommandItemNestedBasic command) {}
@FromEvent(eventType = CommandSetForNestedBasic.class)@ClearWith(eventType = CommandClearedForNestedBasic.class)record CommandItemNestedBasic(String name, String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, Guid, nested } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class CommandSetForNestedBasic { @field(String) readonly name: string; @field(String) readonly schema: string;
constructor(name: string, schema: string) { this.name = name; this.schema = schema; }}
@eventType()export class CommandClearedForNestedBasic {}
@fromEvent(CommandSetForNestedBasic)@clearWith(CommandClearedForNestedBasic)export class CommandItemNestedBasic { name = ''; schema = '';}
@fromEvent(CommandSetForNestedBasic)export class SliceWithNestedCommandBasic { id: Guid = Guid.empty; name = '';
@field(CommandItemNestedBasic) @nested command: CommandItemNestedBasic | null = null;}The object starts empty. As events arrive:
- The first populate event creates the nested object.
- Later populate events update the existing nested object.
- A clear event sets the parent property back to
null.
Parent Property
Section titled “Parent Property”Put [Nested] on the parent property that holds the object. Keeping it nullable represents both the initial empty state and the state after a clear event:
using Cratis.Chronicle.Projections.ModelBound;
public record ParentWithNestedProperty( [Nested] NestedPropertyChild? Child);
public record NestedPropertyChild( string Name, string Description);import io.cratis.chronicle.projections.Nested
data class ParentWithNestedProperty( @Nested val child: NestedPropertyChild? = null)
data class NestedPropertyChild( val name: String = "", val description: String = "")import io.cratis.chronicle.projections.Nested;
record ParentWithNestedProperty( @Nested NestedPropertyChild child) {}
record NestedPropertyChild(String name, String description) {}Elixir does not support this workflow yet.import { nested } from '@cratis/chronicle';
class NestedPropertyChild { name = ''; description = '';}
class ParentWithNestedProperty { @nested child: NestedPropertyChild | null = null;}The nested type is scanned for its own model-bound projection annotations, including FromEvent, ClearWith, and property mappings.
Clearing Nested Objects
Section titled “Clearing Nested Objects”Apply a clear annotation to the nested type when one event should remove the nested object from the parent.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedClear(string Name, string Schema);
[EventType]public record CommandClearedForNestedClear;
[FromEvent<CommandSetForNestedClear>][ClearWith<CommandClearedForNestedClear>]public record CommandItemNestedClear( string Name, string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEvent
@EventTypedata class CommandSetForNestedClear(val name: String, val schema: String)
@EventTypedata class CommandClearedForNestedClear(val placeholder: Boolean = true)
@FromEvent(CommandSetForNestedClear::class)@ClearWith(CommandClearedForNestedClear::class)data class CommandItemNestedClear( val name: String = "", val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;
@EventTyperecord CommandSetForNestedClear(String name, String schema) {}
@EventTyperecord CommandClearedForNestedClear() {}
@FromEvent(eventType = CommandSetForNestedClear.class)@ClearWith(eventType = CommandClearedForNestedClear.class)record CommandItemNestedClear(String name, String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CommandSetForNestedClear { @field(String) readonly name: string; @field(String) readonly schema: string;
constructor(name: string, schema: string) { this.name = name; this.schema = schema; }}
@eventType()class CommandClearedForNestedClear {}
@fromEvent(CommandSetForNestedClear)@clearWith(CommandClearedForNestedClear)class CommandItemNestedClear { name = ''; schema = '';}Use multiple clear annotations when more than one event should clear the same nested object.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedMultipleClear(string Name, string Schema);
[EventType]public record CommandClearedForNestedMultipleClear;
[EventType]public record SliceArchivedForNestedMultipleClear;
[FromEvent<CommandSetForNestedMultipleClear>][ClearWith<CommandClearedForNestedMultipleClear>][ClearWith<SliceArchivedForNestedMultipleClear>]public record CommandItemNestedMultipleClear( string Name, string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEvent
@EventTypedata class CommandSetForNestedMultipleClear(val name: String, val schema: String)
@EventTypedata class CommandClearedForNestedMultipleClear(val placeholder: Boolean = true)
@EventTypedata class SliceArchivedForNestedMultipleClear(val placeholder: Boolean = true)
@FromEvent(CommandSetForNestedMultipleClear::class)@ClearWith(CommandClearedForNestedMultipleClear::class)@ClearWith(SliceArchivedForNestedMultipleClear::class)data class CommandItemNestedMultipleClear( val name: String = "", val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;
@EventTyperecord CommandSetForNestedMultipleClear(String name, String schema) {}
@EventTyperecord CommandClearedForNestedMultipleClear() {}
@EventTyperecord SliceArchivedForNestedMultipleClear() {}
@FromEvent(eventType = CommandSetForNestedMultipleClear.class)@ClearWith(eventType = CommandClearedForNestedMultipleClear.class)@ClearWith(eventType = SliceArchivedForNestedMultipleClear.class)record CommandItemNestedMultipleClear(String name, String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CommandSetForNestedMultipleClear { @field(String) readonly name: string; @field(String) readonly schema: string;
constructor(name: string, schema: string) { this.name = name; this.schema = schema; }}
@eventType()class CommandClearedForNestedMultipleClear {}
@eventType()class SliceArchivedForNestedMultipleClear {}
@fromEvent(CommandSetForNestedMultipleClear)@clearWith(CommandClearedForNestedMultipleClear)@clearWith(SliceArchivedForNestedMultipleClear)class CommandItemNestedMultipleClear { name = ''; schema = '';}You can also put the clear annotation on the parent property. This is useful when several parents share the nested type but different events end their relationships. To clear one member while keeping the nested object, put the annotation on that member. See Clearing Values for that distinction.
Updating From Multiple Events
Section titled “Updating From Multiple Events”Declare each event that can populate or update the nested type. Matching property names are auto-mapped by default, so a rename event can update Name while a schema-change event updates Schema:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedMultipleFrom(string Name, string Schema);
[EventType]public record CommandRenamedForNestedMultipleFrom(string Name);
[EventType]public record CommandSchemaUpdatedForNestedMultipleFrom(string Schema);
[EventType]public record CommandClearedForNestedMultipleFrom;
[FromEvent<CommandSetForNestedMultipleFrom>][FromEvent<CommandRenamedForNestedMultipleFrom>][FromEvent<CommandSchemaUpdatedForNestedMultipleFrom>][ClearWith<CommandClearedForNestedMultipleFrom>]public record CommandItemNestedMultipleFrom( string Name, string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEvent
@EventTypedata class CommandSetForNestedMultipleFrom(val name: String, val schema: String)
@EventTypedata class CommandRenamedForNestedMultipleFrom(val name: String)
@EventTypedata class CommandSchemaUpdatedForNestedMultipleFrom(val schema: String)
@EventTypedata class CommandClearedForNestedMultipleFrom(val placeholder: Boolean = true)
@FromEvent(CommandSetForNestedMultipleFrom::class)@FromEvent(CommandRenamedForNestedMultipleFrom::class)@FromEvent(CommandSchemaUpdatedForNestedMultipleFrom::class)@ClearWith(CommandClearedForNestedMultipleFrom::class)data class CommandItemNestedMultipleFrom( val name: String = "", val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;
@EventTyperecord CommandSetForNestedMultipleFrom(String name, String schema) {}
@EventTyperecord CommandRenamedForNestedMultipleFrom(String name) {}
@EventTyperecord CommandSchemaUpdatedForNestedMultipleFrom(String schema) {}
@EventTyperecord CommandClearedForNestedMultipleFrom() {}
@FromEvent(eventType = CommandSetForNestedMultipleFrom.class)@FromEvent(eventType = CommandRenamedForNestedMultipleFrom.class)@FromEvent(eventType = CommandSchemaUpdatedForNestedMultipleFrom.class)@ClearWith(eventType = CommandClearedForNestedMultipleFrom.class)record CommandItemNestedMultipleFrom(String name, String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CommandSetForNestedMultipleFrom { @field(String) readonly name: string; @field(String) readonly schema: string;
constructor(name: string, schema: string) { this.name = name; this.schema = schema; }}
@eventType()class CommandRenamedForNestedMultipleFrom { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
@eventType()class CommandSchemaUpdatedForNestedMultipleFrom { @field(String) readonly schema: string;
constructor(schema: string) { this.schema = schema; }}
@eventType()class CommandClearedForNestedMultipleFrom {}
@fromEvent(CommandSetForNestedMultipleFrom)@fromEvent(CommandRenamedForNestedMultipleFrom)@fromEvent(CommandSchemaUpdatedForNestedMultipleFrom)@clearWith(CommandClearedForNestedMultipleFrom)class CommandItemNestedMultipleFrom { name = ''; schema = '';}When event property names differ from nested object property names, add explicit property mappings to the nested type.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedExplicit(string CommandName, string JsonSchema);
[EventType]public record CommandSchemaUpdatedForNestedExplicit(string UpdatedSchema);
[EventType]public record CommandClearedForNestedExplicit;
[FromEvent<CommandSetForNestedExplicit>][FromEvent<CommandSchemaUpdatedForNestedExplicit>][ClearWith<CommandClearedForNestedExplicit>]public record CommandItemNestedExplicit( [SetFrom<CommandSetForNestedExplicit>(nameof(CommandSetForNestedExplicit.CommandName))] string Name, [SetFrom<CommandSetForNestedExplicit>(nameof(CommandSetForNestedExplicit.JsonSchema))] [SetFrom<CommandSchemaUpdatedForNestedExplicit>(nameof(CommandSchemaUpdatedForNestedExplicit.UpdatedSchema))] string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFrom
@EventTypedata class CommandSetForNestedExplicit(val commandName: String, val jsonSchema: String)
@EventTypedata class CommandSchemaUpdatedForNestedExplicit(val updatedSchema: String)
@EventTypedata class CommandClearedForNestedExplicit(val placeholder: Boolean = true)
@FromEvent(CommandSetForNestedExplicit::class)@FromEvent(CommandSchemaUpdatedForNestedExplicit::class)@ClearWith(CommandClearedForNestedExplicit::class)data class CommandItemNestedExplicit( @SetFrom("commandName", CommandSetForNestedExplicit::class) val name: String = "",
@SetFrom("jsonSchema", CommandSetForNestedExplicit::class) @SetFrom("updatedSchema", CommandSchemaUpdatedForNestedExplicit::class) val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;
@EventTyperecord CommandSetForNestedExplicit(String commandName, String jsonSchema) {}
@EventTyperecord CommandSchemaUpdatedForNestedExplicit(String updatedSchema) {}
@EventTyperecord CommandClearedForNestedExplicit() {}
@FromEvent(eventType = CommandSetForNestedExplicit.class)@FromEvent(eventType = CommandSchemaUpdatedForNestedExplicit.class)@ClearWith(eventType = CommandClearedForNestedExplicit.class)record CommandItemNestedExplicit( @SetFrom(propertyPath = "commandName", eventType = CommandSetForNestedExplicit.class) String name,
@SetFrom(propertyPath = "jsonSchema", eventType = CommandSetForNestedExplicit.class) @SetFrom(propertyPath = "updatedSchema", eventType = CommandSchemaUpdatedForNestedExplicit.class) String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CommandSetForNestedExplicit { @field(String) readonly commandName: string; @field(String) readonly jsonSchema: string;
constructor(commandName: string, jsonSchema: string) { this.commandName = commandName; this.jsonSchema = jsonSchema; }}
@eventType()class CommandSchemaUpdatedForNestedExplicit { @field(String) readonly updatedSchema: string;
constructor(updatedSchema: string) { this.updatedSchema = updatedSchema; }}
@eventType()class CommandClearedForNestedExplicit {}
@fromEvent(CommandSetForNestedExplicit)@fromEvent(CommandSchemaUpdatedForNestedExplicit)@clearWith(CommandClearedForNestedExplicit)class CommandItemNestedExplicit { @setFrom(CommandSetForNestedExplicit, 'commandName') name = '';
@setFrom(CommandSetForNestedExplicit, 'jsonSchema') @setFrom(CommandSchemaUpdatedForNestedExplicit, 'updatedSchema') schema = '';}Auto-Mapping
Section titled “Auto-Mapping”AutoMap is enabled by default for nested types. Disable it on the nested type when every property should be mapped explicitly.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedNoAutoMap(string CommandName, string Schema);
[EventType]public record CommandClearedForNestedNoAutoMap;
[FromEvent<CommandSetForNestedNoAutoMap>][ClearWith<CommandClearedForNestedNoAutoMap>][NoAutoMap]public record CommandItemNestedNoAutoMap( [SetFrom<CommandSetForNestedNoAutoMap>(nameof(CommandSetForNestedNoAutoMap.CommandName))] string Name, [SetFrom<CommandSetForNestedNoAutoMap>(nameof(CommandSetForNestedNoAutoMap.Schema))] string Schema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.NoAutoMapimport io.cratis.chronicle.projections.SetFrom
@EventTypedata class CommandSetForNestedNoAutoMap(val commandName: String, val schema: String)
@EventTypedata class CommandClearedForNestedNoAutoMap(val placeholder: Boolean = true)
@FromEvent(CommandSetForNestedNoAutoMap::class)@ClearWith(CommandClearedForNestedNoAutoMap::class)@NoAutoMapdata class CommandItemNestedNoAutoMap( @SetFrom("commandName", CommandSetForNestedNoAutoMap::class) val name: String = "",
@SetFrom("schema", CommandSetForNestedNoAutoMap::class) val schema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.NoAutoMap;import io.cratis.chronicle.projections.SetFrom;
@EventTyperecord CommandSetForNestedNoAutoMap(String commandName, String schema) {}
@EventTyperecord CommandClearedForNestedNoAutoMap() {}
@FromEvent(eventType = CommandSetForNestedNoAutoMap.class)@ClearWith(eventType = CommandClearedForNestedNoAutoMap.class)@NoAutoMaprecord CommandItemNestedNoAutoMap( @SetFrom(propertyPath = "commandName", eventType = CommandSetForNestedNoAutoMap.class) String name,
@SetFrom(propertyPath = "schema", eventType = CommandSetForNestedNoAutoMap.class) String schema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, Guid, nested, noAutoMap, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class CommandSetForNestedNoAutoMap { commandName = ''; schema = '';}
@eventType()export class CommandClearedForNestedNoAutoMap {}
@fromEvent(CommandSetForNestedNoAutoMap)@clearWith(CommandClearedForNestedNoAutoMap)@noAutoMapexport class CommandItemNestedNoAutoMap { @setFrom(CommandSetForNestedNoAutoMap, 'commandName') name = '';
@setFrom(CommandSetForNestedNoAutoMap, 'schema') schema = '';}
@fromEvent(CommandSetForNestedNoAutoMap)export class SliceWithNestedCommandNoAutoMap { id: Guid = Guid.empty;
@field(CommandItemNestedNoAutoMap) @nested command: CommandItemNestedNoAutoMap | null = null;}Multiple Nested Objects
Section titled “Multiple Nested Objects”A parent can hold more than one nested object. Each nested property points to a type with its own populate and clear events:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record CommandSetForNestedMultiple(string Name, string Schema);
[EventType]public record CommandClearedForNestedMultiple;
[EventType]public record ValidationConfiguredForNestedMultiple(string Rules, bool IsStrict);
[EventType]public record ValidationRemovedForNestedMultiple;
public record SliceWithMultipleNestedObjects( string Name, [Nested] CommandItemNestedMultiple? Command, [Nested] ValidationConfigNestedMultiple? Validation);
[FromEvent<CommandSetForNestedMultiple>][ClearWith<CommandClearedForNestedMultiple>]public record CommandItemNestedMultiple(string Name, string Schema);
[FromEvent<ValidationConfiguredForNestedMultiple>][ClearWith<ValidationRemovedForNestedMultiple>]public record ValidationConfigNestedMultiple(string Rules, bool IsStrict);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.Nestedimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class SliceCreatedForNestedMultiple(val name: String)
@EventTypedata class CommandSetForNestedMultiple(val name: String, val schema: String)
@EventTypedata class CommandClearedForNestedMultiple(val placeholder: Boolean = true)
@EventTypedata class ValidationConfiguredForNestedMultiple(val rules: String, val isStrict: Boolean)
@EventTypedata class ValidationRemovedForNestedMultiple(val placeholder: Boolean = true)
@ReadModel@FromEvent(SliceCreatedForNestedMultiple::class)data class SliceWithMultipleNestedObjects( val name: String = "",
@Nested val command: CommandItemNestedMultiple? = null,
@Nested val validation: ValidationConfigNestedMultiple? = null)
@FromEvent(CommandSetForNestedMultiple::class)@ClearWith(CommandClearedForNestedMultiple::class)data class CommandItemNestedMultiple(val name: String = "", val schema: String = "")
@FromEvent(ValidationConfiguredForNestedMultiple::class)@ClearWith(ValidationRemovedForNestedMultiple::class)data class ValidationConfigNestedMultiple(val rules: String = "", val isStrict: Boolean = false)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.Nested;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord SliceCreatedForNestedMultiple(String name) {}
@EventTyperecord CommandSetForNestedMultiple(String name, String schema) {}
@EventTyperecord CommandClearedForNestedMultiple() {}
@EventTyperecord ValidationConfiguredForNestedMultiple(String rules, boolean isStrict) {}
@EventTyperecord ValidationRemovedForNestedMultiple() {}
@ReadModel@FromEvent(eventType = SliceCreatedForNestedMultiple.class)record SliceWithMultipleNestedObjects( String name,
@Nested CommandItemNestedMultiple command,
@Nested ValidationConfigNestedMultiple validation) {}
@FromEvent(eventType = CommandSetForNestedMultiple.class)@ClearWith(eventType = CommandClearedForNestedMultiple.class)record CommandItemNestedMultiple(String name, String schema) {}
@FromEvent(eventType = ValidationConfiguredForNestedMultiple.class)@ClearWith(eventType = ValidationRemovedForNestedMultiple.class)record ValidationConfigNestedMultiple(String rules, boolean isStrict) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, nested } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class CommandSetForNestedMultiple { @field(String) readonly name: string; @field(String) readonly schema: string;
constructor(name: string, schema: string) { this.name = name; this.schema = schema; }}
@eventType()class CommandClearedForNestedMultiple {}
@eventType()class ValidationConfiguredForNestedMultiple { @field(String) readonly rules: string; @field(Boolean) readonly isStrict: boolean;
constructor(rules: string, isStrict: boolean) { this.rules = rules; this.isStrict = isStrict; }}
@eventType()class ValidationRemovedForNestedMultiple {}
@fromEvent(CommandSetForNestedMultiple)@clearWith(CommandClearedForNestedMultiple)class CommandItemNestedMultiple { name = ''; schema = '';}
@fromEvent(ValidationConfiguredForNestedMultiple)@clearWith(ValidationRemovedForNestedMultiple)class ValidationConfigNestedMultiple { rules = ''; isStrict = false;}
class SliceWithMultipleNestedObjects { name = '';
@nested command: CommandItemNestedMultiple | null = null;
@nested validation: ValidationConfigNestedMultiple | null = null;}Nested Objects Inside Children
Section titled “Nested Objects Inside Children”A child collection item can hold a nested object with its own lifecycle:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record TaskAddedForNestedChildren(Guid TaskId, string Title);
[EventType]public record TaskAssignedForNestedChildren(Guid TaskId, string Name, string Email);
[EventType]public record TaskUnassignedForNestedChildren(Guid TaskId);
public record ProjectWithNestedChildren( [Key] Guid Id, string Name, [ChildrenFrom<TaskAddedForNestedChildren>(key: nameof(TaskAddedForNestedChildren.TaskId))] IEnumerable<ProjectTaskWithNestedAssignee> Tasks);
public record ProjectTaskWithNestedAssignee( [Key] Guid TaskId, string Title, [Nested] TaskAssigneeNestedChild? Assignee);
[FromEvent<TaskAssignedForNestedChildren>][ClearWith<TaskUnassignedForNestedChildren>]public record TaskAssigneeNestedChild( string Name, string Email);Kotlin does not support this workflow yet.`ProjectionsService.collectChildrenMap` only reads `@FromEvent`/`@SetFrom`/aggregate annotations offthe child element type — it never recurses into a `@Nested` property declared on that child type, so anested object inside a child collection item is never wired up.Java does not support this workflow yet.`ProjectionsService.collectChildrenMap` only reads `@FromEvent`/`@SetFrom`/aggregate annotations offthe child element type — it never recurses into a `@Nested` property declared on that child type, so anested object inside a child collection item is never wired up.Elixir does not support this workflow yet.import { childrenFrom, clearWith, eventType, fromEvent, Guid, nested } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class TaskAddedForNestedChildren { @field(Guid) readonly taskId: Guid; @field(String) readonly title: string;
constructor(taskId: Guid, title: string) { this.taskId = taskId; this.title = title; }}
@eventType()class TaskAssignedForNestedChildren { @field(Guid) readonly taskId: Guid; @field(String) readonly name: string; @field(String) readonly email: string;
constructor(taskId: Guid, name: string, email: string) { this.taskId = taskId; this.name = name; this.email = email; }}
@eventType()class TaskUnassignedForNestedChildren { @field(Guid) readonly taskId: Guid;
constructor(taskId: Guid) { this.taskId = taskId; }}
@fromEvent(TaskAssignedForNestedChildren)@clearWith(TaskUnassignedForNestedChildren)class TaskAssigneeNestedChild { name = ''; email = '';}
class ProjectTaskWithNestedAssignee { taskId: Guid = Guid.empty; title = '';
@nested @field(TaskAssigneeNestedChild) assignee: TaskAssigneeNestedChild | null = null;}
class ProjectWithNestedChildren { id: Guid = Guid.empty; name = '';
@childrenFrom(TaskAddedForNestedChildren, 'taskId') @field(Array, { genericArguments: [ProjectTaskWithNestedAssignee] }) tasks: ProjectTaskWithNestedAssignee[] = [];}Joins inside such a nested object are not supported by the kernel. Keep those join mappings on the collection item itself; see Joins.
Complete Example
Section titled “Complete Example”The parent in this example keeps an optional command definition. Setting the command populates it, later events change selected fields, and clearing it leaves the parent read model in place:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record SliceCreatedForNestedComplete(string Name);
[EventType]public record CommandSetForNestedComplete( Guid CommandId, string Name, string Schema, string Rules, string StateSchema);
[EventType]public record CommandRenamedForNestedComplete(Guid CommandId, string Name);
[EventType]public record CommandDefinitionUpdatedForNestedComplete( Guid CommandId, string Schema, string Rules, string StateSchema);
[EventType]public record CommandClearedForNestedComplete;
[FromEvent<SliceCreatedForNestedComplete>]public record SliceNestedComplete( [Key] Guid Id, string Name, [Nested] CommandItemNestedComplete? Command);
[FromEvent<CommandSetForNestedComplete>][FromEvent<CommandRenamedForNestedComplete>][FromEvent<CommandDefinitionUpdatedForNestedComplete>][ClearWith<CommandClearedForNestedComplete>]public record CommandItemNestedComplete( [SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.CommandId))] Guid Id, [SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Name))] [SetFrom<CommandRenamedForNestedComplete>(nameof(CommandRenamedForNestedComplete.Name))] string Name, [SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Schema))] [SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.Schema))] string Schema, [SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.Rules))] [SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.Rules))] string Rules, [SetFrom<CommandSetForNestedComplete>(nameof(CommandSetForNestedComplete.StateSchema))] [SetFrom<CommandDefinitionUpdatedForNestedComplete>(nameof(CommandDefinitionUpdatedForNestedComplete.StateSchema))] string StateSchema);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ClearWithimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.Nestedimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class SliceCreatedForNestedComplete(val name: String)
@EventTypedata class CommandSetForNestedComplete( val commandId: String, val name: String, val schema: String, val rules: String, val stateSchema: String)
@EventTypedata class CommandRenamedForNestedComplete(val commandId: String, val name: String)
@EventTypedata class CommandDefinitionUpdatedForNestedComplete( val commandId: String, val schema: String, val rules: String, val stateSchema: String)
@EventTypedata class CommandClearedForNestedComplete(val placeholder: Boolean = true)
@ReadModel@FromEvent(SliceCreatedForNestedComplete::class)data class SliceNestedComplete( val name: String = "",
@Nested val command: CommandItemNestedComplete? = null)
@FromEvent(CommandSetForNestedComplete::class)@FromEvent(CommandRenamedForNestedComplete::class)@FromEvent(CommandDefinitionUpdatedForNestedComplete::class)@ClearWith(CommandClearedForNestedComplete::class)data class CommandItemNestedComplete( @SetFrom("commandId", CommandSetForNestedComplete::class) val id: String = "",
@SetFrom("name", CommandSetForNestedComplete::class) @SetFrom("name", CommandRenamedForNestedComplete::class) val name: String = "",
@SetFrom("schema", CommandSetForNestedComplete::class) @SetFrom("schema", CommandDefinitionUpdatedForNestedComplete::class) val schema: String = "",
@SetFrom("rules", CommandSetForNestedComplete::class) @SetFrom("rules", CommandDefinitionUpdatedForNestedComplete::class) val rules: String = "",
@SetFrom("stateSchema", CommandSetForNestedComplete::class) @SetFrom("stateSchema", CommandDefinitionUpdatedForNestedComplete::class) val stateSchema: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ClearWith;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.Nested;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord SliceCreatedForNestedComplete(String name) {}
@EventTyperecord CommandSetForNestedComplete( String commandId, String name, String schema, String rules, String stateSchema) {}
@EventTyperecord CommandRenamedForNestedComplete(String commandId, String name) {}
@EventTyperecord CommandDefinitionUpdatedForNestedComplete( String commandId, String schema, String rules, String stateSchema) {}
@EventTyperecord CommandClearedForNestedComplete() {}
@ReadModel@FromEvent(eventType = SliceCreatedForNestedComplete.class)record SliceNestedComplete( String name,
@Nested CommandItemNestedComplete command) {}
@FromEvent(eventType = CommandSetForNestedComplete.class)@FromEvent(eventType = CommandRenamedForNestedComplete.class)@FromEvent(eventType = CommandDefinitionUpdatedForNestedComplete.class)@ClearWith(eventType = CommandClearedForNestedComplete.class)record CommandItemNestedComplete( @SetFrom(propertyPath = "commandId", eventType = CommandSetForNestedComplete.class) String id,
@SetFrom(propertyPath = "name", eventType = CommandSetForNestedComplete.class) @SetFrom(propertyPath = "name", eventType = CommandRenamedForNestedComplete.class) String name,
@SetFrom(propertyPath = "schema", eventType = CommandSetForNestedComplete.class) @SetFrom(propertyPath = "schema", eventType = CommandDefinitionUpdatedForNestedComplete.class) String schema,
@SetFrom(propertyPath = "rules", eventType = CommandSetForNestedComplete.class) @SetFrom(propertyPath = "rules", eventType = CommandDefinitionUpdatedForNestedComplete.class) String rules,
@SetFrom(propertyPath = "stateSchema", eventType = CommandSetForNestedComplete.class) @SetFrom(propertyPath = "stateSchema", eventType = CommandDefinitionUpdatedForNestedComplete.class) String stateSchema) {}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, Guid, nested, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class SliceCreatedForNestedComplete { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
@eventType()export class CommandSetForNestedComplete { @field(Guid) readonly commandId: Guid; @field(String) readonly name: string; @field(String) readonly schema: string; @field(String) readonly rules: string; @field(String) readonly stateSchema: string;
constructor(commandId: Guid, name: string, schema: string, rules: string, stateSchema: string) { this.commandId = commandId; this.name = name; this.schema = schema; this.rules = rules; this.stateSchema = stateSchema; }}
@eventType()export class CommandRenamedForNestedComplete { @field(Guid) readonly commandId: Guid; @field(String) readonly name: string;
constructor(commandId: Guid, name: string) { this.commandId = commandId; this.name = name; }}
@eventType()export class CommandDefinitionUpdatedForNestedComplete { @field(Guid) readonly commandId: Guid; @field(String) readonly schema: string; @field(String) readonly rules: string; @field(String) readonly stateSchema: string;
constructor(commandId: Guid, schema: string, rules: string, stateSchema: string) { this.commandId = commandId; this.schema = schema; this.rules = rules; this.stateSchema = stateSchema; }}
@eventType()export class CommandClearedForNestedComplete {}
@fromEvent(CommandSetForNestedComplete)@fromEvent(CommandRenamedForNestedComplete)@fromEvent(CommandDefinitionUpdatedForNestedComplete)@clearWith(CommandClearedForNestedComplete)export class CommandItemNestedComplete { @setFrom(CommandSetForNestedComplete, 'commandId') id: Guid = Guid.empty;
@setFrom(CommandSetForNestedComplete, 'name') @setFrom(CommandRenamedForNestedComplete, 'name') name = '';
@setFrom(CommandSetForNestedComplete, 'schema') @setFrom(CommandDefinitionUpdatedForNestedComplete, 'schema') schema = '';
@setFrom(CommandSetForNestedComplete, 'rules') @setFrom(CommandDefinitionUpdatedForNestedComplete, 'rules') rules = '';
@setFrom(CommandSetForNestedComplete, 'stateSchema') @setFrom(CommandDefinitionUpdatedForNestedComplete, 'stateSchema') stateSchema = '';}
@fromEvent(SliceCreatedForNestedComplete)export class SliceNestedComplete { id: Guid = Guid.empty; name = '';
@field(CommandItemNestedComplete) @nested command: CommandItemNestedComplete | null = null;}| Event | Effect on the nested object |
|---|---|
SliceCreatedForNestedComplete |
Creates the parent read model; the nested command remains null. |
CommandSetForNestedComplete |
Populates the command object. |
CommandRenamedForNestedComplete |
Updates the command name in place. |
CommandDefinitionUpdatedForNestedComplete |
Updates command definition fields in place. |
CommandClearedForNestedComplete |
Sets the command property to null. |
Supported Nested Annotations
Section titled “Supported Nested Annotations”| Annotation | Works on nested type |
|---|---|
FromEvent |
Yes |
ClearWith |
Yes |
SetFrom |
Yes |
AddFrom / SubtractFrom |
Yes |
SetFromContext |
Yes |
Increment / Decrement / Count |
Yes |
Join |
Supported on a root-level nested object, not on a nested object inside a child collection. |
Nested recursively |
Yes |
ChildrenFrom inside nested objects |
Not supported by the kernel. |
NoAutoMap |
Yes |
Best Practices
Section titled “Best Practices”Keep the populate events on the nested type and place clear annotations where the lifecycle is owned: on that type for a shared lifecycle, or on the parent property for an owner-specific one. Use AutoMap while names match and add explicit mappings where they differ.
For multiple independent objects, move to a child collection. Nested objects cannot contain child collections, and removal through a join is not supported for nested objects.
See Also
Section titled “See Also”- Children Collections — arrays of items managed independently within a parent
- Basic Mapping — getting started with model-bound projections
- Removal — removing root read models on an event