Cratis Chronicle model-bound projections: Clear nullable values
Use ClearWith in a Cratis Chronicle model-bound projection when an event removes a value, such as deleting a note or dropping a due date. The member must be nullable: the mapping writes null when the event is processed, including when rebuilding the read model from earlier events.
Basic Usage
Section titled “Basic Usage”Put ClearWith on the nullable member and name the event that clears it:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record MbClearingProjectNoted(string Note);
[EventType]public record MbClearingProjectNoteCleared;
[FromEvent<MbClearingProjectNoted>]public record MbClearingProjectNotes( [Key] Guid Id,
[ClearWith<MbClearingProjectNoteCleared>] string? Note);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.projections.SetValueimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbClearingProjectNoted(val note: String)
@EventTypedata class MbClearingProjectNoteCleared(val placeholder: Boolean = true)
@ReadModel@FromEvent(MbClearingProjectNoted::class)data class MbClearingProjectNotes( @SetFrom("note", MbClearingProjectNoted::class) @SetValue(MbClearingProjectNoteCleared::class, clear = true) val note: String? = null)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.projections.SetValue;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbClearingProjectNoted(String note) {}
@EventTyperecord MbClearingProjectNoteCleared() {}
@ReadModel@FromEvent(eventType = MbClearingProjectNoted.class)class MbClearingProjectNotes { @SetFrom(propertyPath = "note", eventType = MbClearingProjectNoted.class) @SetValue(eventType = MbClearingProjectNoteCleared.class, clear = true) public String note;}defmodule MyApp.Events.MbClearingProjectNoted do use Chronicle.Events.EventType, id: "mb-clearing-project-noted"
defstruct [:note]end
defmodule MyApp.Events.MbClearingProjectNoteCleared do use Chronicle.Events.EventType, id: "mb-clearing-project-note-cleared"
defstruct []end
defmodule MyApp.ReadModels.MbClearingProject do use Chronicle.ReadModels.ReadModel
defstruct project_id: nil, note: nil
from MyApp.Events.MbClearingProjectNoted, set: [note: :note]
clear_with MyApp.Events.MbClearingProjectNoteCleared, [:note]endimport { clearWith, eventType, fromEvent, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class MbClearingProjectNoted { note = '';}
@eventType()export class MbClearingProjectNoteCleared {}
@fromEvent(MbClearingProjectNoted)export class MbClearingProjectNotes { @setFrom(MbClearingProjectNoted, 'note') @clearWith(MbClearingProjectNoteCleared) @field(String) note: string | undefined = undefined;}MbClearingProjectNoted populates Note; MbClearingProjectNoteCleared returns it to null. A later MbClearingProjectNoted can set it again. Clearing changes the value at that point in the stream; it does not prevent future updates.
The Same Clear as a Null SetValue
Section titled “The Same Clear as a Null SetValue”SetValue with null builds the same mapping. Use ClearWith to name the clearing operation, or keep SetValue(null) alongside existing constant-value declarations:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record MbClearingInvoiceIssued(string Reference);
[EventType]public record MbClearingInvoiceVoided;
[FromEvent<MbClearingInvoiceIssued>]public record MbClearingInvoice( [Key] Guid Id,
[SetValue<MbClearingInvoiceVoided>(null)] string? Reference);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetValueimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbClearingInvoiceIssued(val reference: String)
@EventTypedata class MbClearingInvoiceVoided(val placeholder: Boolean = true)
@ReadModel@FromEvent(MbClearingInvoiceIssued::class)data class MbClearingInvoice( // AutoMap sets this from the matching "reference" property on MbClearingInvoiceIssued; SetValue // with clear = true is the only way to null it back out - @ClearWith targets CLASS only, so it // cannot sit on a plain scalar property the way the .NET client's [ClearWith<T>] can. @SetValue(MbClearingInvoiceVoided::class, clear = true) val reference: String? = null)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetValue;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbClearingInvoiceIssued(String reference) {}
@EventTyperecord MbClearingInvoiceVoided() {}
@ReadModel@FromEvent(eventType = MbClearingInvoiceIssued.class)class MbClearingInvoice { // AutoMap sets this from the matching "reference" field on MbClearingInvoiceIssued; SetValue with // clear = true is the only way to null it back out - @ClearWith targets CLASS only, so it cannot // sit on a plain scalar field the way the .NET client's [ClearWith<T>] can. @SetValue(eventType = MbClearingInvoiceVoided.class, clear = true) public String reference;}defmodule MyApp.Events.MbClearingSetValueNullNoted do use Chronicle.Events.EventType, id: "mb-clearing-set-value-null-noted"
defstruct [:note]end
defmodule MyApp.Events.MbClearingSetValueNullCleared do use Chronicle.Events.EventType, id: "mb-clearing-set-value-null-cleared"
defstruct []end
defmodule MyApp.ReadModels.MbClearingSetValueNullProject do use Chronicle.ReadModels.ReadModel
defstruct project_id: nil, note: nil
from MyApp.Events.MbClearingSetValueNullNoted, set: [note: :note]
# clear: [...] builds the same "$null" mapping as clear_with/2 — reach for whichever reads # better alongside the rest of a from/2 call's options. from MyApp.Events.MbClearingSetValueNullCleared, clear: [:note]endimport { eventType, fromEvent, Guid, setValue } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class MbClearingInvoiceIssued { @field(String) readonly reference: string;
constructor(reference: string) { this.reference = reference; }}
@eventType()export class MbClearingInvoiceVoided {}
@fromEvent(MbClearingInvoiceIssued)export class MbClearingInvoice { id: Guid = Guid.empty;
@setValue(MbClearingInvoiceVoided, null) @field(String) reference: string | null = null;}The Member Must Be Able to Hold No Value
Section titled “The Member Must Be Able to Hold No Value”A non-nullable member cannot represent a cleared value. CHR0048 reports the declaration as a build warning, scheduled to become an error in the next major release. Building the projection also throws CannotClearNonNullableMember, so leaving the warning unaddressed fails at startup.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record MbClearingShiftPlanned(string Assignee, int Hours);
[EventType]public record MbClearingShiftReleased;
[FromEvent<MbClearingShiftPlanned>]public record MbClearingShift( [Key] Guid Id,
// Nullable, so "nobody is assigned" is a state the member can actually hold. [ClearWith<MbClearingShiftReleased>] string? Assignee,
// Nullable value type, for the same reason: 0 hours is a number of hours, not the absence of one. [ClearWith<MbClearingShiftReleased>] int? Hours);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetValueimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbClearingShiftPlanned(val assignee: String, val hours: Int)
@EventTypedata class MbClearingShiftReleased(val placeholder: Boolean = true)
@ReadModel@FromEvent(MbClearingShiftPlanned::class)data class MbClearingShift( // Nullable, so "nobody is assigned" is a state the property can actually hold. @SetValue(MbClearingShiftReleased::class, clear = true) val assignee: String? = null,
// Nullable numeric type, for the same reason: 0 hours is a number of hours, not the absence of one. @SetValue(MbClearingShiftReleased::class, clear = true) val hours: Int? = null)import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetValue;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbClearingShiftPlanned(String assignee, int hours) {}
@EventTyperecord MbClearingShiftReleased() {}
@ReadModel@FromEvent(eventType = MbClearingShiftPlanned.class)class MbClearingShift { // A reference type, so "nobody is assigned" is a state the field can actually hold. @SetValue(eventType = MbClearingShiftReleased.class, clear = true) public String assignee;
// Boxed Integer, for the same reason: 0 hours is a number of hours, not the absence of one. A // primitive int could never be cleared this way. @SetValue(eventType = MbClearingShiftReleased.class, clear = true) public Integer hours;}defmodule MyApp.Events.MbClearingNullableNoted do use Chronicle.Events.EventType, id: "mb-clearing-nullable-noted"
defstruct [:note]end
defmodule MyApp.Events.MbClearingNullableCleared do use Chronicle.Events.EventType, id: "mb-clearing-nullable-cleared"
defstruct []end
defmodule MyApp.ReadModels.MbClearingNullableProject do use Chronicle.ReadModels.ReadModel
# The member a clear_with/2 or clear: target names has to default to `nil`. A struct field # that defaults to a concrete value such as "" is not nullable to the kernel, and clearing it # writes the type default back instead of no value — the same ambiguity .NET's # CannotClearNonNullableMember check exists to prevent. defstruct project_id: nil, note: nil
from MyApp.Events.MbClearingNullableNoted, set: [note: :note]
clear_with MyApp.Events.MbClearingNullableCleared, [:note]endimport { clearWith, eventType, fromEvent, setFrom } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()export class MbClearingShiftPlanned { @field(String) readonly assignee: string; @field(Number) readonly hours: number;
constructor(assignee: string, hours: number) { this.assignee = assignee; this.hours = hours; }}
@eventType()export class MbClearingShiftReleased {}
@fromEvent(MbClearingShiftPlanned)export class MbClearingShift { // Optional, so "nobody is assigned" is a state the member can actually hold. @setFrom(MbClearingShiftPlanned, 'assignee') @clearWith(MbClearingShiftReleased) @field(String) assignee: string | undefined = undefined;
// Optional for the same reason: 0 hours is a number of hours, not the absence of one. @setFrom(MbClearingShiftPlanned, 'hours') @clearWith(MbClearingShiftReleased) @field(Number) hours: number | undefined = undefined;}Writing "", 0, or DateTimeOffset.MinValue instead would record a specific value, not the absence of one. An empty string, for example, cannot tell a reader whether a note was cleared or was genuinely empty.
Choose the declaration that represents the fact you need:
| You mean | Declare |
|---|---|
| No value at all | A nullable member plus [ClearWith<TEvent>] |
| A specific value that happens to be the type default | [SetValue<TEvent>("")], [SetValue<TEvent>(0)], … |
Child Items
Section titled “Child Items”A nullable member on a child item clears the same way as a root member. Route the clearing event to the collection with its own ChildrenFrom declaration and the key that identifies the item:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record MbClearingTaskListStarted(string Name);
[EventType]public record MbClearingTaskAdded(Guid ListId, Guid TaskId, string Title, string Due);
[EventType]public record MbClearingTaskDeferred(Guid ListId, Guid TaskId);
public record MbClearingTask( [Key] Guid Id, [SetFrom<MbClearingTaskAdded>(nameof(MbClearingTaskAdded.Title))] string Title, [SetFrom<MbClearingTaskAdded>(nameof(MbClearingTaskAdded.Due))] [ClearWith<MbClearingTaskDeferred>] string? Due);
[FromEvent<MbClearingTaskListStarted>]public record MbClearingTaskList( [Key] Guid Id,
[ChildrenFrom<MbClearingTaskAdded>(key: nameof(MbClearingTaskAdded.TaskId), parentKey: nameof(MbClearingTaskAdded.ListId), identifiedBy: nameof(MbClearingTask.Id))] [ChildrenFrom<MbClearingTaskDeferred>(key: nameof(MbClearingTaskDeferred.TaskId), parentKey: nameof(MbClearingTaskDeferred.ListId), identifiedBy: nameof(MbClearingTask.Id))] IReadOnlyList<MbClearingTask> Tasks);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.ChildrenFromimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.projections.SetValueimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbClearingTaskListStarted(val name: String)
@EventTypedata class MbClearingTaskAdded(val listId: String, val taskId: String, val title: String, val due: String)
@EventTypedata class MbClearingTaskDeferred(val listId: String, val taskId: String)
data class MbClearingTask( val taskId: String = "",
@SetFrom("title", MbClearingTaskAdded::class) val title: String = "",
@SetFrom("due", MbClearingTaskAdded::class) @SetValue(MbClearingTaskDeferred::class, clear = true) val due: String? = null)
@ReadModel@FromEvent(MbClearingTaskListStarted::class)data class MbClearingTaskList( @ChildrenFrom(MbClearingTaskAdded::class, key = "taskId", identifiedBy = "taskId", parentKey = "listId") @ChildrenFrom(MbClearingTaskDeferred::class, key = "taskId", identifiedBy = "taskId", parentKey = "listId") val tasks: List<MbClearingTask> = emptyList())import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.ChildrenFrom;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.projections.SetValue;import io.cratis.chronicle.readModels.ReadModel;
import java.util.Collections;import java.util.List;
@EventTyperecord MbClearingTaskListStarted(String name) {}
@EventTyperecord MbClearingTaskAdded(String listId, String taskId, String title, String due) {}
@EventTyperecord MbClearingTaskDeferred(String listId, String taskId) {}
class MbClearingTask { public String taskId = "";
@SetFrom(propertyPath = "title", eventType = MbClearingTaskAdded.class) public String title = "";
@SetFrom(propertyPath = "due", eventType = MbClearingTaskAdded.class) @SetValue(eventType = MbClearingTaskDeferred.class, clear = true) public String due;}
@ReadModel@FromEvent(eventType = MbClearingTaskListStarted.class)class MbClearingTaskList { @ChildrenFrom(eventType = MbClearingTaskAdded.class, key = "taskId", identifiedBy = "taskId", parentKey = "listId") @ChildrenFrom(eventType = MbClearingTaskDeferred.class, key = "taskId", identifiedBy = "taskId", parentKey = "listId") public List<MbClearingTask> tasks = Collections.emptyList();}defmodule MyApp.Events.MbClearingChildItemAdded do use Chronicle.Events.EventType, id: "mb-clearing-child-item-added"
defstruct [:item_id, :note]end
defmodule MyApp.Events.MbClearingChildItemNoteCleared do use Chronicle.Events.EventType, id: "mb-clearing-child-item-note-cleared"
defstruct [:item_id]end
defmodule MyApp.ReadModels.MbClearingChildItem do defstruct item_id: nil, note: nilend
defmodule MyApp.ReadModels.MbClearingChildBasket do use Chronicle.ReadModels.ReadModel, types: [items: {:list, MyApp.ReadModels.MbClearingChildItem}]
defstruct basket_id: nil, items: []
children :items, identified_by: :item_id do from MyApp.Events.MbClearingChildItemAdded, key: :item_id, set: [item_id: :item_id, note: :note]
# The clearing event has to reach the child through the same `children` block the child's # own `from` is declared in, with the key that identifies which item to clear. clear_with MyApp.Events.MbClearingChildItemNoteCleared, [:note], key: :item_id endendimport { childrenFrom, eventType, fromEvent, Guid, setFrom, setValue } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class MbClearingTaskListStarted { @field(String) readonly name: string;
constructor(name: string) { this.name = name; }}
@eventType()class MbClearingTaskAdded { @field(Guid) readonly listId: Guid; @field(Guid) readonly taskId: Guid; @field(String) readonly title: string; @field(String) readonly due: string;
constructor(listId: Guid, taskId: Guid, title: string, due: string) { this.listId = listId; this.taskId = taskId; this.title = title; this.due = due; }}
@eventType()class MbClearingTaskDeferred { @field(Guid) readonly listId: Guid; @field(Guid) readonly taskId: Guid;
constructor(listId: Guid, taskId: Guid) { this.listId = listId; this.taskId = taskId; }}
class MbClearingTask { id: Guid = Guid.empty;
@setFrom(MbClearingTaskAdded, 'title') title = '';
@field(String) @setFrom(MbClearingTaskAdded, 'due') @setValue(MbClearingTaskDeferred, null) due: string | null = null;}
@fromEvent(MbClearingTaskListStarted)class MbClearingTaskList { id: Guid = Guid.empty;
@childrenFrom(MbClearingTaskAdded, 'taskId', 'id', 'listId') @childrenFrom(MbClearingTaskDeferred, 'taskId', 'id', 'listId') @field(Array, { genericArguments: [MbClearingTask] }) tasks: MbClearingTask[] = [];}Nested Objects
Section titled “Nested Objects”Put ClearWith on a member inside a nested type to clear only that member. Put it on the nullable Nested member itself to clear the whole object. The nested type can also declare its own clearing event, as described in Nested Objects.
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;
[EventType]public record MbClearingContractSigned(string Title, string NoticeGiven);
[EventType]public record MbClearingNoticeWithdrawn;
[EventType]public record MbClearingContractEnded;
[FromEvent<MbClearingContractSigned>]public record MbClearingContract( string Title,
// Clears this member of the nested object; the object itself stays. [ClearWith<MbClearingNoticeWithdrawn>] string? NoticeGiven);
public record MbClearingEmployee( [Key] Guid Id,
// Clears the whole nested object back to null. [Nested] [ClearWith<MbClearingContractEnded>] MbClearingContract? Contract);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.projections.SetValueimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbClearingContractSigned(val title: String, val noticeGiven: String)
@EventTypedata class MbClearingNoticeWithdrawn(val placeholder: Boolean = true)
@EventTypedata class MbClearingContractEnded(val placeholder: Boolean = true)
@EventTypedata class MbClearingEmployeeHired(val placeholder: Boolean = true)
@FromEvent(MbClearingContractSigned::class)@ClearWith(MbClearingContractEnded::class)data class MbClearingContract( @SetFrom("title", MbClearingContractSigned::class) val title: String = "",
// Clears this property of the nested object; the object itself stays. @SetFrom("noticeGiven", MbClearingContractSigned::class) @SetValue(MbClearingNoticeWithdrawn::class, clear = true) val noticeGiven: String? = null)
// The Kotlin client only recognizes a model-bound read model once it carries at least one root// @FromEvent - MbClearingContract's own @FromEvent above still drives the nested object.@ReadModel@FromEvent(MbClearingEmployeeHired::class)data class MbClearingEmployee( @Nested val contract: MbClearingContract? = null)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.projections.SetValue;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbClearingContractSigned(String title, String noticeGiven) {}
@EventTyperecord MbClearingNoticeWithdrawn() {}
@EventTyperecord MbClearingContractEnded() {}
@EventTyperecord MbClearingEmployeeHired() {}
@FromEvent(eventType = MbClearingContractSigned.class)@ClearWith(eventType = MbClearingContractEnded.class)class MbClearingContract { @SetFrom(propertyPath = "title", eventType = MbClearingContractSigned.class) public String title = "";
// Clears this field of the nested object; the object itself stays. @SetFrom(propertyPath = "noticeGiven", eventType = MbClearingContractSigned.class) @SetValue(eventType = MbClearingNoticeWithdrawn.class, clear = true) public String noticeGiven;}
// The Kotlin client only recognizes a model-bound read model once it carries at least one root// @FromEvent - MbClearingContract's own @FromEvent above still drives the nested object.@ReadModel@FromEvent(eventType = MbClearingEmployeeHired.class)class MbClearingEmployee { @Nested public MbClearingContract contract;}Elixir does not support this workflow yet.import { clearWith, eventType, fromEvent, Guid, nested, setValue } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class MbClearingContractSigned { @field(String) readonly title: string; @field(String) readonly noticeGiven: string;
constructor(title: string, noticeGiven: string) { this.title = title; this.noticeGiven = noticeGiven; }}
@eventType()class MbClearingNoticeWithdrawn {}
@eventType()class MbClearingContractEnded {}
@fromEvent(MbClearingContractSigned)class MbClearingContract { title = '';
// Clears this member of the nested object; the object itself stays. @field(String) @setValue(MbClearingNoticeWithdrawn, null) noticeGiven: string | null = null;}
@fromEvent(MbClearingContractSigned)class MbClearingEmployee { id: Guid = Guid.empty;
// Clears the whole nested object back to null. @field(MbClearingContract) @nested @clearWith(MbClearingContractEnded) contract: MbClearingContract | null = null;}When several models share the nested type, declaring the clear on the owning member keeps that owner’s event out of the shared type.
When This Is the Wrong Fit
Section titled “When This Is the Wrong Fit”ClearWith sets a member to null; it is not a collection-removal operation. Use RemovedWith to remove a selected child or delete the root read model, as described in Removal. Applying ClearWith to a ChildrenFrom collection is not a supported way to remove every item.
If the member should fall back to a specific default rather than have no value, declare that value with SetValue.
In the Projection Declaration Language
Section titled “In the Projection Declaration Language”In the projection declaration language, use clear to name the member an event empties:
projection Notes => MyApp.ReadModels.MbClearingProjectNotes from MbClearingProjectNoted note = note from MbClearingProjectNoteCleared clear noteUse the member path to clear a value inside a nested object. This is the declaration-language equivalent of the MbClearingContract attribute example above:
projection Employees => MyApp.ReadModels.MbClearingEmployee from MbClearingNoticeWithdrawn clear contract.noticeGivennote = null compiles to the same mapping, so existing declarations need no change. Prefer clear in new declarations to make the removal of a value explicit. Chronicle also writes clear when generating a declaration from a projection definition, regardless of the original spelling. See the Projection Declaration Language for the rest of the syntax.
In the Fluent API
Section titled “In the Fluent API”In the .NET fluent API, use Clear at the root, inside Children, or inside Nested, wherever you would use Set:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections;
[EventType]public record MbClearingFluentNoted(string Note);
[EventType]public record MbClearingFluentNoteCleared;
[EventType]public record MbClearingFluentSummarised(string Headline, string Note);
[EventType]public record MbClearingFluentSummaryNoteCleared;
[EventType]public record MbClearingFluentTaskAdded(Guid TaskId, string Title, string Note);
[EventType]public record MbClearingFluentTaskNoteCleared(Guid TaskId);
public record MbClearingFluentSummary(string Headline, string? Note);
public record MbClearingFluentTask([Key] Guid Id, string Title, string? Note);
public record MbClearingFluentProject( [Key] Guid Id, string? Note, MbClearingFluentSummary? Summary, IReadOnlyList<MbClearingFluentTask> Tasks);
public class MbClearingFluentProjectProjection : IProjectionFor<MbClearingFluentProject>{ public void Define(IProjectionBuilderFor<MbClearingFluentProject> builder) => builder .From<MbClearingFluentNoted>(_ => _ .Set(m => m.Note).To(e => e.Note)) .From<MbClearingFluentNoteCleared>(_ => _ .Clear(m => m.Note)) .Nested(m => m.Summary, summary => summary .From<MbClearingFluentSummarised>(_ => _ .Set(m => m.Headline).To(e => e.Headline) .Set(m => m.Note).To(e => e.Note)) .From<MbClearingFluentSummaryNoteCleared>(_ => _ .Clear(m => m.Note))) .Children(m => m.Tasks, tasks => tasks .IdentifiedBy(_ => _.Id) .From<MbClearingFluentTaskAdded>(_ => _ .UsingKey(e => e.TaskId) .Set(m => m.Title).To(e => e.Title) .Set(m => m.Note).To(e => e.Note)) .From<MbClearingFluentTaskNoteCleared>(_ => _ .UsingKey(e => e.TaskId) .Clear(m => m.Note)));}Kotlin does not support this workflow yet.`IFromBuilderFor`/`IChildFromBuilderFor` have no `clear()` method at all — the only clearing operationin the fluent builder is `INestedBuilderFor.clearWith()`, which clears an entire nested object, not asingle member of the root, a nested object, or a child item.Java does not support this workflow yet.`IFromBuilderFor`/`IChildFromBuilderFor` have no `clear()` method at all — the only clearing operationin the fluent builder is `INestedBuilderFor.clearWith()`, which clears an entire nested object, not asingle member of the root, a nested object, or a child item.defmodule MyApp.Events.MbClearingFluentNoted do use Chronicle.Events.EventType, id: "mb-clearing-fluent-noted"
defstruct [:note]end
defmodule MyApp.Events.MbClearingFluentCleared do use Chronicle.Events.EventType, id: "mb-clearing-fluent-cleared"
defstruct []end
defmodule MyApp.ReadModels.MbClearingFluentProject do use Chronicle.ReadModels.ReadModel
defstruct project_id: nil, note: nilend
defmodule MyApp.Projections.MbClearingFluentProjectProjection do use Chronicle.Projections.Projection, model: MyApp.ReadModels.MbClearingFluentProject
# Elixir has one projection language, not a separate attribute vs. fluent-builder split — # `clear_with/2` is available on a model-bound read model and on a declarative projection alike. from MyApp.Events.MbClearingFluentNoted, set: [note: :note]
clear_with MyApp.Events.MbClearingFluentCleared, [:note]endimport { eventType, Guid, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class MbClearingFluentNoted { @field(String) readonly note: string; constructor(note: string) { this.note = note; }}
@eventType()class MbClearingFluentNoteCleared {}
@eventType()class MbClearingFluentSummarised { @field(String) readonly headline: string; @field(String) readonly note: string; constructor(headline: string, note: string) { this.headline = headline; this.note = note; }}
@eventType()class MbClearingFluentSummaryNoteCleared {}
@eventType()class MbClearingFluentTaskAdded { @field(Guid) readonly taskId: Guid; @field(String) readonly title: string; @field(String) readonly note: string; constructor(taskId: Guid, title: string, note: string) { this.taskId = taskId; this.title = title; this.note = note; }}
@eventType()class MbClearingFluentTaskNoteCleared { @field(Guid) readonly taskId: Guid; constructor(taskId: Guid) { this.taskId = taskId; }}
class MbClearingFluentSummary { headline = ''; note: string | null = null;}
class MbClearingFluentTask { id: Guid = Guid.empty; title = ''; note: string | null = null;}
class MbClearingFluentProject { id: Guid = Guid.empty; note: string | null = null; summary: MbClearingFluentSummary | null = null; tasks: MbClearingFluentTask[] = [];}
@projection()class MbClearingFluentProjectProjection implements IProjectionFor<MbClearingFluentProject> { define(builder: IProjectionBuilderFor<MbClearingFluentProject>): void { builder .from(MbClearingFluentNoted, _ => _ .set(m => m.note).to(e => e.note)) .from(MbClearingFluentNoteCleared, _ => _ .set(m => m.note).toValue(null)) .nested<MbClearingFluentSummary>(m => m.summary, summary => summary .from(MbClearingFluentSummarised, _ => _ .set(m => m.headline).to(e => e.headline) .set(m => m.note).to(e => e.note)) .from(MbClearingFluentSummaryNoteCleared, _ => _ .set(m => m.note).toValue(null))) .children<MbClearingFluentTask>(m => m.tasks, tasks => tasks .identifiedBy(m => m.id) .from(MbClearingFluentTaskAdded, _ => _ .usingKey(e => e.taskId) .set(m => m.title).to(e => e.title) .set(m => m.note).to(e => e.note)) .from(MbClearingFluentTaskNoteCleared, _ => _ .usingKey(e => e.taskId) .set(m => m.note).toValue(null))); }}Set(...).ToValue(null) builds the same mapping and remains supported. Prefer Clear in new code so the operation reads like its model-bound counterpart, ClearWith.
The nullable rule still applies. C# generic constraints cannot reject a non-nullable reference member at this method’s signature. Building the projection throws CannotClearNonNullableMember instead, and CHR0048 reports both Clear and ToValue(null) at build time.
If you implement the builder interface yourself, implement Clear before using it. Its default interface implementation preserves compatibility for existing builders but throws ClearNotSupported when called.