Chronicle events: define and append facts
A Chronicle event records a fact about something that happened in your system, such as an order being placed. Append it to an event sequence to keep that fact in an ordered history that projections, reducers, and reactors can process.
In .NET, mark the record with [EventType] to identify it as an event. Chronicle uses the event type identity, rather than the .NET runtime type, to identify the stored event. The record describes the event’s content, while appending adds it to the sequence for observers to process:
using Cratis.Chronicle.Events;
[EventType]public record EventsIndexEmployeeRegistered(string FirstName, string LastName);import io.cratis.chronicle.events.EventType
@EventTypedata class EventsIndexTypeEmployeeRegistered(val firstName: String = "", val lastName: String = "")import io.cratis.chronicle.events.EventType;
@EventTyperecord EventsIndexTypeEmployeeRegistered(String firstName, String lastName) {}defmodule MyApp.Events.EventsIndexTypeEmployeeRegistered do use Cratis.Chronicle.Events.EventType, id: "events-index-type-employee-registered"
defstruct [:first_name, :last_name]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class EventsIndexEmployeeRegistered { @field(String) firstName: string; @field(String) lastName: string;
constructor(firstName: string, lastName: string) { this.firstName = firstName; this.lastName = lastName; }}Event Sequences and the Event Log
Section titled “Event Sequences and the Event Log”Events live in event sequences, which are ordered, append-only streams. Each event receives a monotonically increasing sequence number that makes it possible to replay events, resume processing, and reason about how far consumers have progressed.
Chronicle includes a specialized event sequence called the event log. It is the default sequence used throughout the system and is exposed through the IEventLog API.
using Cratis.Chronicle.Events;using Cratis.Chronicle.EventSequences;
public class EventsIndexEmployeesService(IEventLog eventLog){ public Task RegisterEmployee(EventSourceId employeeId, string firstName, string lastName) => eventLog.Append(employeeId, new EventsIndexEmployeeRegistered(firstName, lastName));}import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.events.EventType
@EventTypedata class EventsIndexLogEmployeeRegistered(val firstName: String = "", val lastName: String = "")
/** * The event log is the default event sequence, exposed through [IEventStore.eventLog]. */suspend fun registerEmployee(store: IEventStore, employeeId: String, firstName: String, lastName: String) = store.eventLog.append(employeeId, EventsIndexLogEmployeeRegistered(firstName, lastName))import io.cratis.chronicle.EventStore;import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.eventSequences.AppendResult;
import io.cratis.chronicle.java.EventLogJavaBridge;
@EventTyperecord EventsIndexLogEmployeeRegistered(String firstName, String lastName) {}
class EventsIndexEventLog { // The event log is the default event sequence, exposed through EventStore.getEventLog(). AppendResult registerEmployee(EventStore store, String employeeId, String firstName, String lastName) { return EventLogJavaBridge.append(store.getEventLog(), employeeId, new EventsIndexLogEmployeeRegistered(firstName, lastName), null); }}defmodule MyApp.Events.EventsIndexEmployeeRegistered do use Cratis.Chronicle.Events.EventType, id: "events-index-employee-registered"
defstruct [:first_name, :last_name]end
defmodule MyApp.EventsIndexEmployeesService do alias MyApp.Events.EventsIndexEmployeeRegistered
def register_employee(employee_id, first_name, last_name) do Cratis.Chronicle.append(employee_id, %EventsIndexEmployeeRegistered{ first_name: first_name, last_name: last_name }) endendimport { IEventStore } from '@cratis/chronicle';
class EventsIndexEmployeesService { constructor(private readonly store: IEventStore) {}
async registerEmployee(employeeId: string, firstName: string, lastName: string): Promise<void> { await this.store.eventLog.append(employeeId, new EventsIndexEmployeeRegistered(firstName, lastName)); }}Working with Events
Section titled “Working with Events”Start by choosing an event source id, the identifier that groups one entity’s events. Then append a single event and check its result. If one decision produces several events that must be stored together, use Appending many. A unit of work lets a workflow stage events before committing them as a batch.
When the decision depends on state you read, add concurrency protection so a competing append can invalidate that decision. Atomic storage keeps a batch together; it does not by itself protect the facts used to decide what to write.
For the rest of the event lifecycle:
- Appending with tags attaches metadata for grouping and filtering.
- Cross-cutting properties adds shared content to events in the .NET client.
- Getting events reads stored facts; Getting state folds events into state.
- Observing appends receives client-side append notifications.
- Filtering reducers and reactors selects which events an observer processes.
- Revision, Compensation, and Redaction address corrections and removal. Choose the operation that matches what needs to change.