Event sourcing in TypeScript and Node.js
With event sourcing, a Node.js application records business facts as events instead of overwriting the state those facts produced. An EmployeeHired event preserves the hiring fact; a read model derives the employee details a screen needs. You can process the recorded events again to build a different view without replacing the original facts.
Cratis Chronicle stores and processes those events. @cratis/chronicle is its TypeScript client for Node.js, not an embedded event store: the client connects to the Chronicle kernel, a separate server. The .NET, Kotlin/Java, and Elixir clients can reach that kernel too, over its gRPC/protobuf boundary. Storage is pluggable, with MongoDB as the default and PostgreSQL, SQL Server, SQLite, and in-memory providers implemented in the kernel.
The client uses @eventType to identify events and @reducer and @projection to describe the state built from them. @reactor marks code that responds to events with side effects; @constraint describes rules enforced when appending. The client also covers transactions, jobs, webhooks, compliance and personally identifiable information (PII) handling, and OpenTelemetry. It builds on the @cratis/chronicle.contracts gRPC contracts package.
Install
Section titled “Install”npm install @cratis/chronicle @cratis/fundamentals reflect-metadataThe client supports standard decorators (TypeScript 5.2 or later) and legacy decorators. Your project needs:
reflect-metadata, imported once at the top of your entry point;- an ES module project (
"type": "module"inpackage.json, a matchingmodulesetting), since the package is published as ESM and the example below uses top-levelawait.
For standard decorators, leave experimentalDecorators and emitDecoratorMetadata unset in tsconfig.json. For the legacy option, set both to true.
The Console sample’s tsconfig.json is a working reference.
A taste
Section titled “A taste”This excerpt illustrates the append flow: define a hiring event, connect to the server, and append one fact to an employee’s history. It assumes a local development kernel; Get started with Chronicle covers starting one. For a complete project and the decorator setup for the current client, follow the TypeScript client docs.
import 'reflect-metadata';import { field } from '@cratis/fundamentals';import { ChronicleClient, ChronicleOptions, eventType } from '@cratis/chronicle';
@eventType()class EmployeeHired { @field(String) firstName!: string; @field(String) lastName!: string;
constructor(firstName: string, lastName: string) { this.firstName = firstName; this.lastName = lastName; }}
const client = new ChronicleClient(ChronicleOptions.development());const store = await client.getEventStore('MyStore');const result = await store.eventLog.append('employee-123', new EmployeeHired('Jane', 'Doe'));console.log(`Appended at sequence number ${result.sequenceNumber.value}`);client.dispose();ChronicleOptions.development() selects the local development connection. The event source identifier, employee-123, groups facts about this employee; use it again for later events about the same person. The printed sequence number identifies the appended event. It does not mean a reducer or projection has already updated a read model: that processing is asynchronous. The TypeScript client docs cover read-model processing and connection strings for other environments.
Is event sourcing a fit for this service?
Section titled “Is event sourcing a fit for this service?”Keep events when the history is part of the problem: explaining a change or building another view from past facts. You still need to decide which facts to record and how to evolve their contracts. A new view can use only the information that history contains, and screens reading asynchronously built models must allow for a delay after a write.
If the service only needs current values, a database update avoids that extra processing. Compare the options in Event sourcing in TypeScript and Node.js: Chronicle with Arc.TypeScript, Emmett, and the KurrentDB Node client, including when an alternative is the better choice.
Where to go next
Section titled “Where to go next”Want the command and query side too? See CQRS in TypeScript and Node.js.
Looking for another language? See Chronicle in your language for the .NET, Kotlin/Java, Elixir, and Go clients.