NestJS provides first-class support for CQRS through the @nestjs/cqrs module, making it one of the most streamlined frameworks for building event-sourced applications in the Node.js ecosystem. This tutorial walks through building a complete CQRS and Event Sourcing system with NestJS, from project scaffolding through production-ready event handling, projections, and API endpoints.
Project Setup
Start with a fresh NestJS project and install the required dependencies.
Configure TypeORM for PostgreSQL in app.module.ts:
Database Schema
Create the events table and read model tables.
Domain Events
Define your domain events as plain classes that NestJS's event bus can publish.
Event Store Service
Build the event store as a NestJS service wrapping PostgreSQL operations.
Order Aggregate
The aggregate enforces business rules and produces events.
Commands and Handlers
NestJS CQRS module provides decorators for command and query handlers.
Need a second opinion on your system design architecture?
I run free 30-minute strategy calls for engineering teams tackling this exact problem.
Book a Free CallEvent Handlers (Projections)
Event handlers build read models asynchronously.
Query Side
API Controller
Wire everything together with a REST controller.
Module Wiring
Testing
Conclusion
NestJS's @nestjs/cqrs module provides a clean, decorator-driven approach to CQRS that reduces boilerplate without hiding the underlying patterns. The command bus, query bus, and event bus are injected as standard NestJS providers, making testing straightforward. Combined with TypeORM for persistence and PostgreSQL for both the event store and read models, you get a production-capable system with a small dependency footprint.
The key advantage of NestJS for CQRS is the modular architecture — each bounded context becomes a NestJS module with its own commands, queries, events, and projections, cleanly separated and independently testable.