A modular and scalable Node.js framework for building efficient, enterprise-ready backend architectures with NestJS.
This project demonstrates a modular monolith architecture using Nest, incorporating CQRS and Event Sourcing patterns. It supports dynamic event store providers and adheres to Domain-Driven Design principles, providing a scalable and maintainable foundation for application development.
The application follows a modular monolith approach with clear bounded contexts. Each module encapsulates its domain logic, application services, and infrastructure concerns. The architecture is layered as follows:
- Domain Layer: Entities, value objects, and domain services.
- Application Layer: Commands, queries, handlers, and facades.
- Infrastructure Layer: Repositories, event stores, and external integrations.
- Interface Layer: Controllers and API endpoints.
The event store integration is achieved through a factory pattern with dynamic modules, allowing seamless switching between MongoDB, Kafka, and EventStoreDB.
Each module resides under the src/modules directory and includes:
- Application: Commands, queries, handlers, services, and DTOs.
- Domain: Entities and interfaces.
- Infrastructure: Repositories and ORM entities.
- Interfaces: Controllers and API definitions.
This separation ensures a clear structure and promotes maintainability.
- Modular monolith architecture with clear bounded contexts
- CQRS and Event Sourcing patterns
- Dynamic event store integration (MongoDB, Kafka, EventStoreDB)
- JWT authentication and authorization
- Comprehensive logging, custom decorators, interceptors, and exception handling
- Dockerized setup with support for SQLite and PostgreSQL
- Domain-Driven Design (DDD): Ensures alignment with business domains and promotes a ubiquitous language.
- Onion Architecture: Separates concerns and enforces dependency inversion.
- Scalability: Modular design allows for independent scaling of modules.
- Maintainability: Clear separation of concerns facilitates easier maintenance and testing.
- Flexibility: Dynamic event store integration allows for easy switching between different providers.
The project is structured to evolve from a Proof of Concept (PoC) to a Production-Ready (PMC) application. It includes:
- Comprehensive testing strategies
- CI/CD pipeline configurations
- Environment-based configurations
- Scalable and maintainable codebase
- Clone the repository:
git clone https://github.com/deadislove/nestJS-modular-monolith-cqrs-event-sourcing-architecture-template.git
cd nestJS-modular-monolith-cqrs-event-sourcing-architecture-template
- Install dependencies:
$ npm install
- Install dependencies:
cp .env.example .env
- Compile and run the project
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
- Run tests:
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
- Run the application:
Navigate to http://localhost:3000 in your browser.
For Docker-based setup, refer to the docker-compose.sqlite.yml or docker-compose.postgresql.yml files.
- Run with SQLite (default)
docker-compose -f docker-compose.sqlite.yml up
# or
docker-compose -f docker-compose.sqlite.yml up --build
- Run with PostgreSQL
docker-compose -f docker-compose.postgresql.yml up
# or
docker-compose -f docker-compose.postgresql.yml up --build
Application logs are automatically saved to the logs/ directory with daily log rotation (e.g., application-2025-04-27.log).
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.
If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
$ npm install -g mau
$ mau deploy
With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
Check out a few resources that may come in handy when working with NestJS:
- Visit the NestJS Documentation to learn more about the framework.
- For questions and support, please visit our Discord channel.
- To dive deeper and get more hands-on experience, check out our official video courses.
- Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
- Need help with your project (part-time to full-time)? Check out our official enterprise support.
- To stay in the loop and get updates, follow us on X and LinkedIn.
- Looking for a job, or have a job to offer? Check out our official Jobs board.
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Da-Wei Lin
- Website - David Weblog
- Twitter - @nestframework
Nest is MIT licensed.