Skip to content

CQRS in TypeScript and Node.js with Cratis Arc

CQRS (Command Query Responsibility Segregation) separates commands that change state from queries that read it. In a task application, registering a task is a command; listing tasks is a query with its own result model. Both can use the same database. Event sourcing is a separate choice about how you store changes.

Cratis Arc for TypeScript provides decorated commands and queries on Node.js. Its packages are published to npm at version 1.0.0, starting with @cratis/arc.core. The @cratis/chronicle event-sourcing client is also published to npm and can be used without Arc. Start with the command and query below if you want Arc to serve your HTTP operations; use Chronicle directly if you already have that layer.

Piece What it does Distribution and limits
Arc for TypeScript Commands, queries, validation, authorization, tenancy and observable queries on Node.js Published to npm as @cratis/arc.core
Arc hosts Standalone host, Express, Fastify and Hono adapters Published to npm as @cratis/arc.express, @cratis/arc.fastify and @cratis/arc.hono
Persistence Tenant-scoped MongoDB collections and Drizzle SQL queries Published to npm as @cratis/arc.mongodb and @cratis/arc.drizzle
Arc Chronicle integration Appends events a command returns; serves projected read models Published to npm as @cratis/arc.chronicle; bounded and experimental, see the capability reference
Frontend proxies @cratis/arc and @cratis/arc.react packages; typed clients generated from backend source Frontend packages released; see generator coverage in the capability reference
@cratis/chronicle Chronicle client: events, reducers, reactors, projections Published to npm

Arc for TypeScript does not have full parity with Arc on .NET. Its capability reference lists each capability, the spec that proves it, and where it deliberately differs. Read it before you design around a feature.

In the Tasks sample, a command is a class with fields and a handle method. A query is a static method on a read model. These excerpts use TaskId, TaskTitle and Tasks from that sample:

@command()
export class RegisterTask {
@field(TaskId) id!: TaskId;
@field(TaskTitle) title!: TaskTitle;
handle(tasks: Tasks): TaskId {
tasks.register(this.id, this.title);
return this.id;
}
}
@readModel()
export class TaskItem {
@field(TaskId) id!: TaskId;
@field(TaskTitle) title!: TaskTitle;
@query(service(Tasks))
static allTasks(tasks: Tasks): TaskItem[] { return tasks.all(); }
}

RegisterTask passes the input to the sample’s Tasks service; allTasks reads from that service and returns TaskItem results. @field comes from @cratis/fundamentals; the other decorators come from @cratis/arc.core. Arc decodes the fields, runs the command or query in a service scope, and serves it over the same HTTP contract as Arc on .NET.

The sample discovers these types from a Features/ folder, producing routes such as POST /api/tasks/registration/register-task and GET /api/tasks/listing/all-tasks. The Arc for TypeScript README and the Arc for TypeScript docs show the full setup, including how to run the samples.

Validation runs before handle(), and a request that fails it never reaches your code. Observable queries stream results over SSE or WebSocket. Both are listed in the capability reference with their limits.

Arc does not need an event log. The Chronicle integration makes handle() return the fact instead of writing it, and Arc appends it after authorization and validation succeed:

@eventType()
export class AuthorRegistered {
@field(AuthorName) name: AuthorName;
constructor(name: AuthorName = new AuthorName('')) { this.name = name; }
}
@command()
export class RegisterAuthor {
@key() @field(AuthorId) id!: AuthorId;
@field(AuthorName) name!: AuthorName;
handle(): AuthorRegistered { return new AuthorRegistered(this.name); }
}

A model-bound projection then builds the read model from the event, and the query reads it:

@readModel()
@fromEvent(AuthorRegistered)
export class Author {
@field(AuthorId) id!: AuthorId;
@field(AuthorName) name!: AuthorName;
@query()
static allAuthors(models: ChronicleReadModels): Promise<Author[]> {
return models.getAll(Author);
}
}

Storing the event and projecting it are separate steps. A query sent immediately after a command can read the model as it was before the event. The add event sourcing guide shows how to wait for the projection and covers the setup, including a local Chronicle kernel.

If you already have an HTTP layer, you can use the published @cratis/chronicle client directly. Events, reducers, projections and reactors are decorated classes, and you append through store.eventLog.append. You own the command handling and HTTP endpoints; Chronicle stores the events and processes the read models. Event sourcing in TypeScript and Node.js shows the install steps and a first append.

CQRS helps when a write needs different input and rules from the data a screen reads. Keeping those contracts separate lets you change one without making it serve both jobs. For a CRUD API over one table, Express or NestJS with an ORM may involve less code to maintain. Coming from Express and NestJS maps the concepts.

Choose Arc for TypeScript when you want to stay on Node.js and keep the Arc programming model and HTTP contract. Arc on .NET and Kotlin and Java are the other backends; neither is a drop-in Node.js server. Staying on Node.js with your own command handling and the Chronicle client is another path. The TypeScript capability reference records differences from .NET, and CQRS explained covers the pattern independently of a framework.

Is there a CQRS framework for TypeScript and Node.js?

Section titled “Is there a CQRS framework for TypeScript and Node.js?”

Yes. Arc for TypeScript provides commands, queries, validation and authorization on Express, Fastify, Hono or a standalone host. Its packages are published to npm, and it does not claim full .NET parity.

Can I do CQRS in Node.js with event sourcing?

Section titled “Can I do CQRS in Node.js with event sourcing?”

Yes. The @cratis/chronicle client is released and works on its own. The Arc Chronicle integration is published as @cratis/arc.chronicle; it is bounded and experimental, with no full .NET parity claim.

Does the frontend get typed commands and queries?

Section titled “Does the frontend get typed commands and queries?”

Yes. Arc’s proxy generator reads the decorated backend source and emits typed commands, queries and observable queries for the published @cratis/arc client. Check the capability reference for the source shapes it handles and the limits on generated metadata.

Do I have to use TypeScript on both sides?

Section titled “Do I have to use TypeScript on both sides?”

No. The same frontend packages work with Arc backends in C# and Kotlin and Java. See Arc backends.

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.