1. Your first event
To record a book arriving in Cratis Chronicle, define a BookAdded event, give the book an identity, and append the event to the log. By the end of this chapter you will have one recorded fact to inspect, ready to feed the catalog in the next chapter.
Before writing code, make sure your kernel is running and your project has a connected client. The C# append example takes an IEventStore; use the event-store handle from that setup. The other client tabs use their corresponding connection and registration instructions.
Think in facts, not rows
Section titled “Think in facts, not rows”A Book row can describe a book you have now. Here we also want to retain the fact that it was added, so we record that change as an event. Name events in the past tense: BookAdded describes something that happened, not a request that might still fail.
In C#, define the event as a record marked with [EventType]. It carries the title and ISBN:
using Cratis.Chronicle.Events;
[EventType]public record BookAdded(string Title, string Isbn);import io.cratis.chronicle.events.EventType
@EventTypedata class BookAdded(val title: String, val isbn: String)import io.cratis.chronicle.events.EventType;
@EventTyperecord BookAdded(String title, String isbn) {}defmodule MyApp.Events.BookAdded do use Cratis.Chronicle.Events.EventType, id: "book-added"
defstruct [:title, :isbn]endimport { eventType } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class BookAdded { @field(String) title: string; @field(String) isbn: string;
constructor(title: string, isbn: string) { this.title = title; this.isbn = isbn; }}With no explicit name on [EventType], Chronicle uses BookAdded as the event type’s stored identity. Keep that identity when you later rename a class: changing the default name does not rename existing history. Event types explains how to preserve it.
The title and ISBN are required here because this fact says both were known when the book arrived. For other events, decide what was actually known at that moment. Missing information may belong in a later event rather than in a partially filled record.
Give the book an identity
Section titled “Give the book an identity”The event source identifies which book this event belongs to. Later borrow and return events use that same identity, keeping the book’s history together. In C#, use a BookId rather than passing an untyped Guid throughout your application; code that expects a BookId can then distinguish it from a member’s ID:
using Cratis.Chronicle.Events;
public record BookId(Guid Value) : EventSourceId<Guid>(Value){ public static BookId New() => new(Guid.NewGuid());}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.BookId do use Cratis.Chronicle.Concept, type: :uuid, event_source_id: true
def new do <<a::32, b::16, _::4, c::12, _::2, d::14, e::48>> = :crypto.strong_rand_bytes(16)
value = :io_lib.format("~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b", [ a, b, c, Bitwise.bor(0x8000, d), e ]) |> IO.iodata_to_binary()
%__MODULE__{value: value} endendTypeScript does not support this workflow yet.EventSourceId<Guid> wraps a Guid as a domain-specific value using ConceptAs<T>, with conversions to Chronicle’s untyped event-source key. That lets the append example pass a BookId directly. Arc also recognizes event-source ID types when resolving where a command appends events; you do not need Arc for this tutorial. The event source concept explains how events are grouped.
Kotlin, Java, and TypeScript use plain strings for event-source IDs rather than this C# typed wrapper. Elixir can mark a Chronicle.Concept with event_source_id: true; its client tab shows a BookId concept. Those are different client conventions, not the same type-safety guarantee.
Append it
Section titled “Append it”Use your connected event store to append BookAdded under the book’s ID. The C# helper creates a BookId, appends the title and ISBN, and returns the ID so you can use it for later events:
using Cratis.Chronicle;
public static class TutorialFirstEventAppend{ public static async Task<BookId> AddBook(IEventStore eventStore) { var book = BookId.New(); await eventStore.EventLog.Append(book, new BookAdded("The Pragmatic Programmer", "978-0135957059")); return book; }}import io.cratis.chronicle.IEventStoreimport java.util.UUID
class TutorialFirstEventAppend { suspend fun addBook(eventStore: IEventStore): String { val bookId = UUID.randomUUID().toString() eventStore.eventLog.append(bookId, BookAdded("The Pragmatic Programmer", "978-0135957059")) return bookId }}import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.BlockingEventStore;import java.util.UUID;
class TutorialFirstEventAppend { String addBook(IEventStore eventStore) { var bookId = UUID.randomUUID().toString();
new BlockingEventStore(eventStore).getEventLog().append( bookId, new BookAdded("The Pragmatic Programmer", "978-0135957059"));
return bookId; }}defmodule MyApp.TutorialFirstEventAppend do alias MyApp.Events.BookAdded
def add_book do book_id = Base.encode16(:crypto.strong_rand_bytes(8), case: :lower)
:ok = Cratis.Chronicle.append(book_id, %BookAdded{ title: "The Pragmatic Programmer", isbn: "978-0135957059" })
book_id endendimport { Guid, IEventStore } from '@cratis/chronicle';
class TutorialFirstEventAppend { async addBook(eventStore: IEventStore): Promise<string> { const bookId = Guid.create().toString(); await eventStore.eventLog.append(bookId, new BookAdded('The Pragmatic Programmer', '978-0135957059')); return bookId; }}In C#, call TutorialFirstEventAppend.AddBook with your IEventStore and await the result. For another client, run its append example after the setup has registered the event type. A successful append validates the event against its registered schema, assigns a sequence number, and stores it in the event log, the ordered history from which observers read.
To see the result, open the workbench at https://localhost:35000, select the event store and namespace your client used, and browse its event log. Find BookAdded with the title The Pragmatic Programmer and ISBN 978-0135957059. Running the helper again creates a new book ID and another event; it does not update the first book.
What you did
Section titled “What you did”You defined BookAdded, associated it with a book ID, and appended it to the log. You can inspect the recorded fact, but you have not built a queryable catalog yet. In Building a read model, you will map these events onto the book view the librarian needs.