Modeling events in Cratis Chronicle
An event in Cratis Chronicle records a business fact that happened, rather than an instruction to do something or a copy of the current state. Model that fact explicitly so a projection can build a view of it, a reactor can act on it, and a later reader can understand the change.
Start with the decision the system accepted. For an order, placing it and applying a discount are meaningful facts; overwriting an order row does not explain either decision. The event’s name and payload should preserve that distinction.
Name events as facts, in the past tense
Section titled “Name events as facts, in the past tense”Use a past-tense phrase in the domain’s language: OrderPlaced, AddressChanged, PaymentCaptured, or BookReturned. CreateOrder names a command, which expresses intent and can still be rejected. OrderState names a model, not the change that produced it. If the name sounds like an instruction, identify the fact that becomes true after the instruction succeeds.
using Cratis.Chronicle.Events;
public record ModelingEventsAddress(string Street, string City);
// A fact that happened[EventType]public record ModelingEventsAddressChanged(ModelingEventsAddress Address);
// An intent (that's a command) or a state blob (that's a read model) — not an event[EventType]public record ModelingEventsUpdateAddress(ModelingEventsAddress Address);import io.cratis.chronicle.events.EventType
data class ModelingEventsAddress(val street: String, val city: String)
// A fact that happened@EventTypedata class ModelingEventsAddressChanged(val address: ModelingEventsAddress)
// An intent (that's a command) or a state blob (that's a read model) — not an event@EventTypedata class ModelingEventsUpdateAddress(val address: ModelingEventsAddress)import io.cratis.chronicle.events.EventType;
record ModelingEventsAddress(String street, String city) {}
// A fact that happened@EventTyperecord ModelingEventsAddressChanged(ModelingEventsAddress address) {}
// An intent (that's a command) or a state blob (that's a read model) — not an event@EventTyperecord ModelingEventsUpdateAddress(ModelingEventsAddress address) {}defmodule MyApp.ModelingEventsAddress do defstruct [:street, :city]end
# A fact that happeneddefmodule MyApp.Events.ModelingEventsAddressChanged do use Cratis.Chronicle.Events.EventType, id: "modeling-events-address-changed"
defstruct [:address]end
# An intent (that's a command) or a state blob (that's a read model) — not an eventdefmodule MyApp.Events.ModelingEventsUpdateAddress do use Cratis.Chronicle.Events.EventType, id: "modeling-events-update-address"
defstruct [:address]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
class ModelingEventsAddress { @field(String) readonly street: string; @field(String) readonly city: string;
constructor(street: string, city: string) { this.street = street; this.city = city; }}
// A fact that happened@eventType()class ModelingEventsAddressChanged { @field(ModelingEventsAddress) readonly address: ModelingEventsAddress;
constructor(address: ModelingEventsAddress) { this.address = address; }}
// An intent (that's a command) or a state blob (that's a read model) — not an event@eventType()class ModelingEventsUpdateAddress { @field(ModelingEventsAddress) readonly address: ModelingEventsAddress;
constructor(address: ModelingEventsAddress) { this.address = address; }}One event, one purpose
Section titled “One event, one purpose”Give each event one meaningful change to describe. A generic CustomerUpdated with optional name, address, and status fields makes every consumer infer which change occurred. Separate facts let a consumer subscribe to the change it needs without interpreting unrelated fields.
using Cratis.Chronicle.Events;
public record ModelingEventsCustomerName(string Value);public record ModelingEventsEmail(string Value);public record ModelingEventsDeactivationReason(string Value);public record ModelingEventsCustomerAddress(string Street, string City);
// One event trying to be everything — consumers must guess what changed[EventType]public record ModelingEventsCustomerUpdated( string? Name, ModelingEventsCustomerAddress? Address, ModelingEventsEmail? Email, bool? Deactivated);
// Distinct facts — each consumer subscribes to exactly what it cares about[EventType]public record ModelingEventsCustomerRenamed(ModelingEventsCustomerName Name);
[EventType]public record ModelingEventsCustomerAddressChanged(ModelingEventsCustomerAddress Address);
[EventType]public record ModelingEventsCustomerDeactivated(ModelingEventsDeactivationReason Reason);import io.cratis.chronicle.events.EventType
data class ModelingEventsCustomerName(val value: String)data class ModelingEventsEmail(val value: String)data class ModelingEventsDeactivationReason(val value: String)data class ModelingEventsCustomerAddress(val street: String, val city: String)
// One event trying to be everything — consumers must guess what changed@EventTypedata class ModelingEventsCustomerUpdated( val name: ModelingEventsCustomerName?, val address: ModelingEventsCustomerAddress?, val email: ModelingEventsEmail?, val deactivated: Boolean?)
// Distinct facts — each consumer subscribes to exactly what it cares about@EventTypedata class ModelingEventsCustomerRenamed(val name: ModelingEventsCustomerName)
@EventTypedata class ModelingEventsCustomerAddressChanged(val address: ModelingEventsCustomerAddress)
@EventTypedata class ModelingEventsCustomerDeactivated(val reason: ModelingEventsDeactivationReason)import io.cratis.chronicle.events.EventType;
record ModelingEventsCustomerName(String value) {}record ModelingEventsEmail(String value) {}record ModelingEventsDeactivationReason(String value) {}record ModelingEventsCustomerAddress(String street, String city) {}
// One event trying to be everything — consumers must guess what changed@EventTyperecord ModelingEventsCustomerUpdated( ModelingEventsCustomerName name, ModelingEventsCustomerAddress address, ModelingEventsEmail email, Boolean deactivated) {}
// Distinct facts — each consumer subscribes to exactly what it cares about@EventTyperecord ModelingEventsCustomerRenamed(ModelingEventsCustomerName name) {}
@EventTyperecord ModelingEventsCustomerAddressChanged(ModelingEventsCustomerAddress address) {}
@EventTyperecord ModelingEventsCustomerDeactivated(ModelingEventsDeactivationReason reason) {}defmodule MyApp.ModelingEventsCustomerName do defstruct [:value]end
defmodule MyApp.ModelingEventsEmail do defstruct [:value]end
defmodule MyApp.ModelingEventsDeactivationReason do defstruct [:value]end
defmodule MyApp.ModelingEventsCustomerAddress do defstruct [:street, :city]end
# One event trying to be everything — consumers must guess what changeddefmodule MyApp.Events.ModelingEventsCustomerUpdated do use Cratis.Chronicle.Events.EventType, id: "modeling-events-customer-updated"
defstruct [:name, :address, :email, :deactivated]end
# Distinct facts — each consumer subscribes to exactly what it cares aboutdefmodule MyApp.Events.ModelingEventsCustomerRenamed do use Cratis.Chronicle.Events.EventType, id: "modeling-events-customer-renamed"
defstruct [:name]end
defmodule MyApp.Events.ModelingEventsCustomerAddressChanged do use Cratis.Chronicle.Events.EventType, id: "modeling-events-customer-address-changed"
defstruct [:address]end
defmodule MyApp.Events.ModelingEventsCustomerDeactivated do use Cratis.Chronicle.Events.EventType, id: "modeling-events-customer-deactivated"
defstruct [:reason]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
class ModelingEventsCustomerName { @field(String) readonly value: string;
constructor(value: string) { this.value = value; }}
class ModelingEventsEmail { @field(String) readonly value: string;
constructor(value: string) { this.value = value; }}
class ModelingEventsDeactivationReason { @field(String) readonly value: string;
constructor(value: string) { this.value = value; }}
class ModelingEventsCustomerAddress { @field(String) readonly street: string; @field(String) readonly city: string;
constructor(street: string, city: string) { this.street = street; this.city = city; }}
// One event trying to be everything — consumers must guess what changed@eventType()class ModelingEventsCustomerUpdated { @field(ModelingEventsCustomerName) readonly name?: ModelingEventsCustomerName; @field(ModelingEventsCustomerAddress) readonly address?: ModelingEventsCustomerAddress; @field(ModelingEventsEmail) readonly email?: ModelingEventsEmail; @field(Boolean) readonly deactivated?: boolean;
constructor(name?: ModelingEventsCustomerName, address?: ModelingEventsCustomerAddress, email?: ModelingEventsEmail, deactivated?: boolean) { this.name = name; this.address = address; this.email = email; this.deactivated = deactivated; }}
// Distinct facts — each consumer subscribes to exactly what it cares about@eventType()class ModelingEventsCustomerRenamed { @field(ModelingEventsCustomerName) readonly name: ModelingEventsCustomerName;
constructor(name: ModelingEventsCustomerName) { this.name = name; }}
@eventType()class ModelingEventsCustomerAddressChanged { @field(ModelingEventsCustomerAddress) readonly address: ModelingEventsCustomerAddress;
constructor(address: ModelingEventsCustomerAddress) { this.address = address; }}
@eventType()class ModelingEventsCustomerDeactivated { @field(ModelingEventsDeactivationReason) readonly reason: ModelingEventsDeactivationReason;
constructor(reason: ModelingEventsDeactivationReason) { this.reason = reason; }}When optional data hides a separate fact
Section titled “When optional data hides a separate fact”Avoid nullable properties that stand in for a separate business fact. In the example below, placing an order and applying a discount describe different changes. Separate events make the distinction visible instead of asking each consumer to interpret an optional discount.
This is modeling guidance, not a prohibition in Chronicle’s schema system: nullable properties are supported. The question is whether absence describes the fact accurately or hides another change that deserves its own name.
using Cratis.Chronicle.Events;
public record ModelingEventsOrderId(Guid Value);public record ModelingEventsMoney(decimal Amount, string Currency);
// Nullable smell — "sometimes there's a discount, sometimes not"[EventType]public record ModelingEventsOrderPlacedWithNullableDiscount( ModelingEventsOrderId Id, ModelingEventsMoney Total, ModelingEventsMoney? Discount);
// Two facts[EventType]public record ModelingEventsOrderPlaced(ModelingEventsOrderId Id, ModelingEventsMoney Total);
[EventType]public record ModelingEventsDiscountApplied(ModelingEventsOrderId Id, ModelingEventsMoney Amount);import io.cratis.chronicle.events.EventType
data class ModelingEventsOrderId(val value: String)data class ModelingEventsMoney(val amount: Double, val currency: String)
// Nullable smell — "sometimes there's a discount, sometimes not"@EventTypedata class ModelingEventsOrderPlacedWithNullableDiscount( val id: ModelingEventsOrderId, val total: ModelingEventsMoney, val discount: ModelingEventsMoney?)
// Two facts@EventTypedata class ModelingEventsOrderPlaced(val id: ModelingEventsOrderId, val total: ModelingEventsMoney)
@EventTypedata class ModelingEventsDiscountApplied(val id: ModelingEventsOrderId, val amount: ModelingEventsMoney)import io.cratis.chronicle.events.EventType;
record ModelingEventsOrderId(String value) {}record ModelingEventsMoney(double amount, String currency) {}
// Nullable smell — "sometimes there's a discount, sometimes not"@EventTyperecord ModelingEventsOrderPlacedWithNullableDiscount( ModelingEventsOrderId id, ModelingEventsMoney total, ModelingEventsMoney discount) {}
// Two facts@EventTyperecord ModelingEventsOrderPlaced(ModelingEventsOrderId id, ModelingEventsMoney total) {}
@EventTyperecord ModelingEventsDiscountApplied(ModelingEventsOrderId id, ModelingEventsMoney amount) {}defmodule MyApp.ModelingEventsOrderId do defstruct [:value]end
defmodule MyApp.ModelingEventsMoney do defstruct [:amount, :currency]end
# Nullable smell — "sometimes there's a discount, sometimes not"defmodule MyApp.Events.ModelingEventsOrderPlacedWithNullableDiscount do use Cratis.Chronicle.Events.EventType, id: "modeling-events-order-placed-with-nullable-discount"
defstruct [:id, :total, :discount]end
# Two factsdefmodule MyApp.Events.ModelingEventsOrderPlaced do use Cratis.Chronicle.Events.EventType, id: "modeling-events-order-placed"
defstruct [:id, :total]end
defmodule MyApp.Events.ModelingEventsDiscountApplied do use Cratis.Chronicle.Events.EventType, id: "modeling-events-discount-applied"
defstruct [:id, :amount]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
class ModelingEventsOrderId { @field(String) readonly value: string;
constructor(value: string) { this.value = value; }}
class ModelingEventsMoney { @field(Number) readonly amount: number; @field(String) readonly currency: string;
constructor(amount: number, currency: string) { this.amount = amount; this.currency = currency; }}
// Nullable smell — "sometimes there's a discount, sometimes not"@eventType()class ModelingEventsOrderPlacedWithNullableDiscount { @field(ModelingEventsOrderId) readonly id: ModelingEventsOrderId; @field(ModelingEventsMoney) readonly total: ModelingEventsMoney; @field(ModelingEventsMoney) readonly discount?: ModelingEventsMoney;
constructor(id: ModelingEventsOrderId, total: ModelingEventsMoney, discount?: ModelingEventsMoney) { this.id = id; this.total = total; this.discount = discount; }}
// Two facts@eventType()class ModelingEventsOrderPlaced { @field(ModelingEventsOrderId) readonly id: ModelingEventsOrderId; @field(ModelingEventsMoney) readonly total: ModelingEventsMoney;
constructor(id: ModelingEventsOrderId, total: ModelingEventsMoney) { this.id = id; this.total = total; }}
@eventType()class ModelingEventsDiscountApplied { @field(ModelingEventsOrderId) readonly id: ModelingEventsOrderId; @field(ModelingEventsMoney) readonly amount: ModelingEventsMoney;
constructor(id: ModelingEventsOrderId, amount: ModelingEventsMoney) { this.id = id; this.amount = amount; }}For existing schemas that allow nullable properties, explicit null and absence are equivalent
and read back as null; missing non-nullable values use the type default where the schema permits it.
A present nullable value such as decimal? holding 0 stays a value. Do not use JSON property
presence to model a separate fact, and use schema-aware ContentComparison / VerifyContent
rather than raw JSON equality to compare appended and stored content. See
Schema representation.
Capture the decision, not the field write
Section titled “Capture the decision, not the field write”When moving from CRUD, it is tempting to emit CustomerUpdated whenever a row changes. Name the business transition instead. AddressChanged records an address change, but it does not by itself say whether the customer moved or corrected a typo. If that distinction matters to the business, capture it rather than expecting a consumer to reconstruct the reason later.
Carry what was true then — and only that
Section titled “Carry what was true then — and only that”Capture values whose historical meaning matters. An order can reference a product by identifier, but looking up that product’s current price cannot explain the price accepted for the order. Record the accepted price in the event.
References are useful for identity; they are not substitutes for historical values. Include enough data to interpret the fact later, without copying every field a particular screen might want. Derive presentation-specific values in a read model.
The five-year test
Section titled “The five-year test”Before committing to an event type, ask whether someone could understand it in five years without the code that produced it. Check the name, identifiers, units, and values that explain the fact. A field named Amount needs a clear currency or other unit; a reference needs a clear meaning. This review is cheaper before consumers depend on the schema.
Events change by migration, never by editing
Section titled “Events change by migration, never by editing”When the same fact needs a new shape, define a new generation and an event type migration rather than silently changing the stored contract. If the business meaning has changed, introduce a different event type instead of making an old name mean two things. Evolve an event’s shape walks through the generation and migration workflow.
Where this leads
Section titled “Where this leads”- Event and Event Type — the mechanics.
- Projections, reducers, and reactors — what consumes your well-modeled events.
- When to use event sourcing — and when modeling events isn’t worth it.