|
1 | | -<p align="center"> |
2 | | - <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a> |
3 | | -</p> |
| 1 | +# NestJS Modular Monolith CQRS Event Sourcing Architecture Template 🚀 |
4 | 2 |
|
5 | | -[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456 |
6 | | -[circleci-url]: https://circleci.com/gh/nestjs/nest |
| 3 | + |
| 4 | + |
| 5 | + |
7 | 6 |
|
8 | | -<p align="center">A modular and scalable <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient, enterprise-ready backend architectures with NestJS.</p> |
| 7 | +Welcome to the **NestJS Modular Monolith CQRS Event Sourcing Architecture Template**. This repository provides a scalable NestJS project template that supports modular monolith architecture, CQRS (Command Query Responsibility Segregation), and Event Sourcing. Built with Domain-Driven Design (DDD) and Onion Architecture principles, this template is ideal for developing proof-of-concept (PoC) projects and production-ready backend systems. |
9 | 8 |
|
10 | | -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a> |
11 | | -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a> |
12 | | -<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a> |
13 | | -<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a> |
14 | | -<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a> |
15 | | -<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a> |
16 | | -<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a> |
17 | | - <a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a> |
18 | | - <a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a> |
19 | | - <a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a> |
20 | | -</p> |
21 | | - <!--[](https://opencollective.com/nest#backer) |
22 | | - [](https://opencollective.com/nest#sponsor)--> |
| 9 | +## Table of Contents |
23 | 10 |
|
24 | | - |
25 | | - <!--CLONE-BADGE--> |
| 11 | +- [Features](#features) |
| 12 | +- [Technologies](#technologies) |
| 13 | +- [Getting Started](#getting-started) |
| 14 | +- [Project Structure](#project-structure) |
| 15 | +- [Usage](#usage) |
| 16 | +- [Contributing](#contributing) |
| 17 | +- [License](#license) |
| 18 | +- [Releases](#releases) |
26 | 19 |
|
27 | | -## 🧭 Project Overview |
| 20 | +## Features |
28 | 21 |
|
29 | | -This project demonstrates a modular monolith architecture using [Nest](https://github.com/nestjs/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. |
| 22 | +- **Modular Monolith Architecture**: Organize your codebase into distinct modules for better maintainability. |
| 23 | +- **CQRS Support**: Separate read and write operations for improved performance and scalability. |
| 24 | +- **Event Sourcing**: Store state changes as a sequence of events, allowing for better auditing and recovery. |
| 25 | +- **Domain-Driven Design**: Focus on the core domain and its logic, improving communication and collaboration among team members. |
| 26 | +- **Onion Architecture**: Promote a clean separation of concerns and dependency management. |
| 27 | +- **Flexible Database Support**: Choose from MongoDB, PostgreSQL, SQLite, or other databases as per your requirements. |
| 28 | +- **Docker and Docker Compose**: Easily deploy your application using containerization. |
30 | 29 |
|
31 | | -## 🏗️ Project Structure & Architecture |
| 30 | +## Technologies |
32 | 31 |
|
33 | | -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: |
| 32 | +This template incorporates a variety of technologies to enhance functionality and performance: |
34 | 33 |
|
35 | | -- Domain Layer: Entities, value objects, and domain services. |
36 | | -- Application Layer: Commands, queries, handlers, and facades. |
37 | | -- Infrastructure Layer: Repositories, event stores, and external integrations. |
38 | | -- Interface Layer: Controllers and API endpoints. |
| 34 | +- **NestJS**: A progressive Node.js framework for building efficient, reliable, and scalable server-side applications. |
| 35 | +- **CQRS**: Command Query Responsibility Segregation pattern for separating read and write operations. |
| 36 | +- **Event Sourcing**: Storing state changes as a sequence of events. |
| 37 | +- **MongoDB**: A NoSQL database for high-performance data storage. |
| 38 | +- **Kafka**: A distributed streaming platform for building real-time data pipelines. |
| 39 | +- **EventStoreDB**: A database specifically designed for event sourcing. |
| 40 | +- **PostgreSQL**: An open-source relational database. |
| 41 | +- **SQLite**: A lightweight database engine. |
| 42 | +- **TypeScript**: A superset of JavaScript that compiles to plain JavaScript. |
39 | 43 |
|
40 | | -The event store integration is achieved through a factory pattern with dynamic modules, allowing seamless switching between MongoDB, Kafka, and EventStoreDB. |
| 44 | +## Getting Started |
41 | 45 |
|
42 | | -## 🧩 Modular Breakdown |
43 | | - |
44 | | -Each module resides under the src/modules directory and includes: |
45 | | - |
46 | | -- Application: Commands, queries, handlers, services, and DTOs. |
47 | | -- Domain: Entities and interfaces. |
48 | | -- Infrastructure: Repositories and ORM entities. |
49 | | -- Interfaces: Controllers and API definitions. |
50 | | - |
51 | | -This separation ensures a clear structure and promotes maintainability. |
52 | | - |
53 | | -## ✨ Key Features |
54 | | - |
55 | | -- Modular monolith architecture with clear bounded contexts |
56 | | -- CQRS and Event Sourcing patterns |
57 | | -- Dynamic event store integration (MongoDB, Kafka, EventStoreDB) |
58 | | -- JWT authentication and authorization |
59 | | -- Comprehensive logging, custom decorators, interceptors, and exception handling |
60 | | -- Dockerized setup with support for SQLite and PostgreSQL |
61 | | - |
62 | | -## 🧠 Design Principles and Benefits |
63 | | - |
64 | | -- Domain-Driven Design (DDD): Ensures alignment with business domains and promotes a ubiquitous language. |
65 | | -- Onion Architecture: Separates concerns and enforces dependency inversion. |
66 | | -- Scalability: Modular design allows for independent scaling of modules. |
67 | | -- Maintainability: Clear separation of concerns facilitates easier maintenance and testing. |
68 | | -- Flexibility: Dynamic event store integration allows for easy switching between different providers. |
69 | | - |
70 | | -## 🚀 PoC to PMC Ready |
71 | | - |
72 | | -The project is structured to evolve from a Proof of Concept (PoC) to a Production-Ready (PMC) application. It includes: |
73 | | - |
74 | | -- Comprehensive testing strategies |
75 | | -- CI/CD pipeline configurations |
76 | | -- Environment-based configurations |
77 | | -- Scalable and maintainable codebase |
78 | | - |
79 | | -## ⚡ Quick Start |
| 46 | +To get started with this template, clone the repository and install the dependencies. Follow these steps: |
80 | 47 |
|
81 | 48 | 1. Clone the repository: |
82 | 49 |
|
83 | | -```bash |
84 | | -git clone https://github.com/deadislove/nestJS-modular-monolith-cqrs-event-sourcing-architecture-template.git |
85 | | -cd nestJS-modular-monolith-cqrs-event-sourcing-architecture-template |
86 | | -``` |
87 | | - |
88 | | -2. Install dependencies: |
| 50 | + ```bash |
| 51 | + git clone https://github.com/aligcgl/nestJS-modular-monolith-cqrs-event-sourcing-architecture-template.git |
| 52 | + ``` |
89 | 53 |
|
90 | | -```bash |
91 | | -$ npm install |
92 | | -``` |
| 54 | +2. Navigate into the project directory: |
93 | 55 |
|
94 | | -3. Install dependencies: |
| 56 | + ```bash |
| 57 | + cd nestJS-modular-monolith-cqrs-event-sourcing-architecture-template |
| 58 | + ``` |
95 | 59 |
|
96 | | -```bash |
97 | | -cp .env.example .env |
98 | | -``` |
| 60 | +3. Install the dependencies: |
99 | 61 |
|
100 | | -4. Compile and run the project |
| 62 | + ```bash |
| 63 | + npm install |
| 64 | + ``` |
101 | 65 |
|
102 | | -```bash |
103 | | -# development |
104 | | -$ npm run start |
| 66 | +4. Set up your environment variables. Create a `.env` file based on the provided `.env.example` file. |
105 | 67 |
|
106 | | -# watch mode |
107 | | -$ npm run start:dev |
| 68 | +5. Start the application: |
108 | 69 |
|
109 | | -# production mode |
110 | | -$ npm run start:prod |
111 | | -``` |
| 70 | + ```bash |
| 71 | + npm run start:dev |
| 72 | + ``` |
112 | 73 |
|
113 | | -5. Run tests: |
| 74 | +Your application should now be running on `http://localhost:3000`. |
114 | 75 |
|
115 | | -```bash |
116 | | -# unit tests |
117 | | -$ npm run test |
| 76 | +## Project Structure |
118 | 77 |
|
119 | | -# e2e tests |
120 | | -$ npm run test:e2e |
| 78 | +The project is organized into several key directories: |
121 | 79 |
|
122 | | -# test coverage |
123 | | -$ npm run test:cov |
| 80 | +``` |
| 81 | +src/ |
| 82 | +├── modules/ # Contains feature modules |
| 83 | +│ ├── users/ # User module |
| 84 | +│ ├── products/ # Product module |
| 85 | +│ └── orders/ # Order module |
| 86 | +├── shared/ # Shared utilities and constants |
| 87 | +├── infrastructure/ # Infrastructure-related code (database, messaging) |
| 88 | +└── main.ts # Entry point of the application |
124 | 89 | ``` |
125 | 90 |
|
126 | | -6. Run the application: |
| 91 | +### Modules |
127 | 92 |
|
128 | | -Navigate to http://localhost:3000 in your browser. |
| 93 | +Each module contains its own controllers, services, and entities. This separation allows for easier testing and maintenance. |
129 | 94 |
|
130 | | -For Docker-based setup, refer to the docker-compose.sqlite.yml or docker-compose.postgresql.yml files. |
| 95 | +### Shared |
131 | 96 |
|
132 | | -- Run with SQLite (default) |
| 97 | +The shared directory includes common utilities, constants, and interfaces that can be used across different modules. |
133 | 98 |
|
134 | | -```bash |
135 | | -docker-compose -f docker-compose.sqlite.yml up |
136 | | -# or |
137 | | -docker-compose -f docker-compose.sqlite.yml up --build |
138 | | -``` |
| 99 | +### Infrastructure |
139 | 100 |
|
140 | | -- Run with PostgreSQL |
| 101 | +This directory contains the configuration for databases, messaging systems, and other infrastructure components. |
141 | 102 |
|
142 | | -```bash |
143 | | -docker-compose -f docker-compose.postgresql.yml up |
144 | | -# or |
145 | | -docker-compose -f docker-compose.postgresql.yml up --build |
146 | | -``` |
| 103 | +## Usage |
147 | 104 |
|
148 | | -## Logging |
| 105 | +To utilize this template effectively, consider the following: |
149 | 106 |
|
150 | | -Application logs are automatically saved to the logs/ directory with daily log rotation (e.g., application-2025-04-27.log). |
| 107 | +1. **Define your domain**: Identify the core business logic and domain entities that your application will manage. |
151 | 108 |
|
152 | | -## Deployment |
| 109 | +2. **Create modules**: Structure your application into modules based on the domain entities. Each module should encapsulate its own logic and dependencies. |
153 | 110 |
|
154 | | -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](https://docs.nestjs.com/deployment) for more information. |
| 111 | +3. **Implement CQRS**: Separate your commands (writes) and queries (reads) to optimize performance and maintainability. |
155 | 112 |
|
156 | | -If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps: |
| 113 | +4. **Utilize Event Sourcing**: Implement event sourcing to track changes in your application state. This allows for better auditing and the ability to replay events if needed. |
157 | 114 |
|
158 | | -```bash |
159 | | -$ npm install -g mau |
160 | | -$ mau deploy |
161 | | -``` |
| 115 | +5. **Deploy with Docker**: Use Docker and Docker Compose for deployment. Create a `docker-compose.yml` file to define your services and run them with a single command. |
162 | 116 |
|
163 | | -With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure. |
| 117 | +## Contributing |
164 | 118 |
|
165 | | -## Resources |
| 119 | +Contributions are welcome! If you would like to contribute to this project, please follow these steps: |
166 | 120 |
|
167 | | -Check out a few resources that may come in handy when working with NestJS: |
| 121 | +1. Fork the repository. |
| 122 | +2. Create a new branch for your feature or bug fix. |
| 123 | +3. Make your changes and commit them. |
| 124 | +4. Push your branch to your forked repository. |
| 125 | +5. Create a pull request describing your changes. |
168 | 126 |
|
169 | | -- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework. |
170 | | -- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy). |
171 | | -- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/). |
172 | | -- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks. |
173 | | -- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com). |
174 | | -- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com). |
175 | | -- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs). |
176 | | -- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com). |
| 127 | +Please ensure that your code adheres to the project's coding standards and is well-documented. |
177 | 128 |
|
178 | | -## Support |
| 129 | +## License |
179 | 130 |
|
180 | | -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](https://docs.nestjs.com/support). |
| 131 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
181 | 132 |
|
182 | | -## Stay in touch |
| 133 | +## Releases |
183 | 134 |
|
184 | | -- Author - [Da-Wei Lin](https://www.linkedin.com/in/da-wei-lin-689a35107/) |
185 | | -- Website - [David Weblog](https://davidskyspace.com/) |
186 | | -- Twitter - [@nestframework](https://twitter.com/nestframework) |
| 135 | +For the latest releases, please visit the [Releases section](https://github.com/aligcgl/nestJS-modular-monolith-cqrs-event-sourcing-architecture-template/releases). You can download and execute the latest files from there. |
187 | 136 |
|
188 | | -## License |
| 137 | +--- |
189 | 138 |
|
190 | | -Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE). |
| 139 | +This README provides a comprehensive overview of the NestJS Modular Monolith CQRS Event Sourcing Architecture Template. Use it as a guide to navigate the project and make the most of its features. |
0 commit comments