How ducgiangtrankma/nestjs-boilerplate works
Main Components
• • CLI Tool: Represented by the “cli” directory. It’s responsible for scaffolding new projects. It uses the “template” folder to create production-ready boilerplate code.
• • Core Backend Application: This is defined inside the “src” directory (mirroring the structure found in the “template” folder). Key areas include:
• API Endpoints: Found in the “src/api” folder, which is broken down into feature areas (e.g., auth and user controllers, services, DTOs, strategies, etc.).
• Configuration & Constants: “src/config” and “src/constants” contain environment configurations, application constants, and settings.
• Database Integration: “src/database” contains the configuration and initialization for the database connection.
• Utilities, Decorators, Filters, Exceptions, Guards: These core modules handle cross-cutting concerns like validation, error handling, authentication (JWT and local strategies), and role management.
• Internationalization: The “src/i18n” directory illustrates multi-language support.
• Shared Libraries: “src/libs” houses integrations with external systems such as AWS S3, RabbitMQ configuration, and Redis, each with its dedicated submodule.
• • Deployment & Environment: Outside the source code, there are Docker files (Dockerfile.dev, docker-compose.dev.yml) and environment samples (.env.sample), which indicate containerization and deployment settings.
Relationships and Interactions
• The CLI tool uses the “template” folder as its blueprint to create new projects, making the CLI an entry point for project generation.
• The “src/main.ts” acts as the application bootstrapper, importing the “src/app.module.ts” that aggregates the various API modules, configuration layers, and libraries.
• Within the API, controllers in “src/api” interact with service classes to handle incoming HTTP requests. These services in turn interact with data models, external libraries (like RabbitMQ for message queues or Redis for caching), and perform business logic.
• Common cross-cutting layers such as decorators, exception filters, and guards (in “src/decorators”, “src/filters”, “src/guards”) are wired into the module system to provide security, error handling, and request validation.
• External integrations (AWS S3, RabbitMQ, Redis) are encapsulated in “src/libs” modules, each being imported into the required modules for operations like file storage, microservice communication, or caching.
• The configuration and constants layers feed settings into various parts of the application ensuring that things like database URIs, JWT secrets, and external service URLs are set at boot time.
Architectural Patterns and Design Principles
• • Modular Architecture: The project follows NestJS’s module-based design, clearly separating concerns (API endpoints, authentication, database, etc.).
• • Dependency Injection: Components like controllers, services, and guards are loosely coupled, relying on NestJS’s DI.
• • Separation of Concerns: API logic, configuration, shared libraries, and CLI functionality are separated into distinct directories.
• • Microservices Patterns: The integration with RabbitMQ and Redis indicates possible event-driven architecture and caching strategies.
• API Modules (delineate “Auth” and “User” at least)
• Config & Constants (feeding configuration into the system)
• Database Module (linking to a “Database” symbol representing MongoDB or TypeORM)
• Cross-Cutting Components (Decorators, Filters, Guards)
• Internationalization (i18n)
• Libraries for External Integrations:
⁃ AWS S3 module
⁃ RabbitMQ/Broker module
⁃ Redis module
• Connections indicating:
• Startup process via “main.ts” → “app.module.ts”
• Data flow from HTTP requests to controllers, onward to services, and finally to the database or external integrations.
• The CLI tool outputs a new project structure that mirrors the core structure seen in “template”.