nestjs-cqrs-ddd-example

Introduction

In this repository, I will demonstrate how to implement CQRS (Command Query Responsibility Segregation), DDD (Domain-Driven Design), and Event Sourcing patterns using the NestJS framework. These patterns can significantly help in managing complex business logic, improving scalability, and maintaining a clean architecture in your applications.

While there are many high-quality articles on the theories behind these patterns and strategies available online, this guide aims to provide a practical, hands-on example.

Join me as I delve into the intricacies of CQRS, DDD, and Event Sourcing with NestJS, offering valuable insights and practical examples to help you implement these patterns effectively in your projects.

CQRS

CQRS (Command Query Responsibility Segregation) is a pattern that separates the models used to update data (commands) from those used to read data (queries). This separation can be useful in complex domains and high-performance applications, allowing for independent scaling and optimization of read and write operations. However, CQRS adds significant complexity and is only suitable for specific parts of a system. Most systems that fit a CRUD model may not benefit from CQRS. Properly evaluating the domain and application needs is crucial before implementation.

For more details, visit Martin Fowler’s article on CQRS.

Event Sourcing

Event Sourcing is a way to persist state changes as a sequence of events rather than storing the current state. Each event represents a state change and is stored in an append-only event log. This pattern allows rebuilding the current state by replaying events, provides a natural audit log, and facilitates debugging by allowing developers to see exactly what happened in the system. It can also support complex scenarios like temporal queries and retroactive corrections.

For more details, visit Martin Fowler’s article on EventSourcing

DDD

Domain-Driven Design (DDD) is an approach to software development that emphasizes the importance of a domain model, which is a rich understanding of the business processes and rules. Originating from Eric Evans’s 2003 book, DDD involves creating a Ubiquitous Language that integrates domain terminology into software systems, evolving models over time, and categorizing objects into Entities, Value Objects, and Service Objects. Strategic Design in DDD includes organizing large domains into Bounded Contexts, providing a structured way to handle complex domains.

For more details, visit Martin Fowler’s article on DDD.

Strategies for managing data

RDBMS (master - slave)

RDBMS(master-slave)

Event stream + Event handler + Key-value store(projection model)

Event stream + Event handler + Key-value store(projection model)

Event store + Event sourcing

Event store + Event sourcing

Event store + Event sourcing + Key-value store(snapshot)

Event store + Event sourcing + Key-value store(snapshot)

Everything

Everything

Getting started

Prerequisites

Script

Fetch codes

git clone https://github.com/kimyh03/nestjs-cqrs-ddd-example

Install packages

npm install

Run databases

docker compose up -d

Run

npm run start