# ducgiangtrankma/nestjs-boilerplate architecture

> Architecture diagram of the GitHub repository ducgiangtrankma/nestjs-boilerplate, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ducgiangtrankma/nestjs-boilerplate
- Repository: https://github.com/ducgiangtrankma/nestjs-boilerplate

## Overview

##### 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”.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% CLI & Scaffolding Section
    subgraph "CLI & Scaffolding"
        CLI["\"CLI Tool\""]:::cli
        TEMPLATE["\"Template Engine\""]:::cli
        CLI -->|"uses"| TEMPLATE
    end

    %% NestJS Application Section
    subgraph "NestJS Application"
        %% Bootstrap Subgraph
        subgraph "Bootstrap"
            BOOT_MAIN["\"main.ts\""]:::appLogic
            BOOT_APP["\"app.module.ts\""]:::appLogic
            BOOT_MAIN -->|"initializes"| BOOT_APP
        end
        
        %% Configuration & Constants
        CONFIG["\"Configuration\""]:::appLogic
        CONSTANTS["\"Constants\""]:::appLogic
        
        %% API Endpoints
        API["\"API Endpoints\""]:::appLogic
        
        %% Database Integration
        DB["\"Database Integration\""]:::infra
        
        %% Cross-Cutting Components Subgraph
        subgraph "Cross-Cutting Components"
            DECORATORS["\"Decorators\""]:::appLogic
            EXCEPTIONS["\"Exceptions & Filters\""]:::appLogic
            GUARDS["\"Guards\""]:::appLogic
        end
        
        %% Internationalization
        I18N["\"Internationalization\""]:::appLogic
        
        %% Shared Libraries for External Integrations Subgraph
        subgraph "Shared Libraries"
            AWS["\"AWS S3\""]:::external
            RABBIT["\"RabbitMQ\""]:::external
            REDIS["\"Redis\""]:::external
        end
        
        %% Relationships within NestJS Application
        BOOT_APP -->|"calls"| API
        CONFIG -->|"feeds"| BOOT_APP
        CONSTANTS -->|"feeds"| BOOT_APP
        CONFIG -->|"configures"| API
        CONSTANTS -->|"influences"| API
        API -->|"queries"| DB
        API -->|"invokes"| DECORATORS
        API -->|"handles"| EXCEPTIONS
        API -->|"secures"| GUARDS
        I18N -->|"supports"| API
        API -->|"uses"| AWS
        API -->|"uses"| RABBIT
        API -->|"uses"| REDIS
    end

    %% Deployment & Environment Section
    subgraph "Deployment & Environment"
        DOCKER1["\"Dockerfile.dev\""]:::infra
        DOCKER2["\"docker-compose.dev.yml\""]:::infra
        ENV["\".env.sample\""]:::infra
    end

    %% Interactions between CLI and NestJS Application
    CLI -->|"generates project structure for"| BOOT_MAIN

    %% Styles
    classDef appLogic fill:#E0F7FA,stroke:#006064,stroke-width:2px;
    classDef infra fill:#FFECB3,stroke:#F57C00,stroke-width:2px;
    classDef cli fill:#E1BEE7,stroke:#8E24AA,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#C62828,stroke-width:2px;

    %% Click Events
    click CLI "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/cli"
    click TEMPLATE "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/template"
    click API "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/api"
    click CONFIG "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/config"
    click CONSTANTS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/constants"
    click DB "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/database"
    click DECORATORS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/decorators"
    click EXCEPTIONS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/exceptions"
    click GUARDS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/guards"
    click I18N "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/i18n"
    click AWS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/libs/aws"
    click RABBIT "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/libs/rabbitmq"
    click REDIS "https://github.com/ducgiangtrankma/nestjs-boilerplate/tree/main/src/libs/redis"
    click BOOT_MAIN "https://github.com/ducgiangtrankma/nestjs-boilerplate/blob/main/src/main.ts"
    click BOOT_APP "https://github.com/ducgiangtrankma/nestjs-boilerplate/blob/main/src/app.module.ts"
    click DOCKER1 "https://github.com/ducgiangtrankma/nestjs-boilerplate/blob/main/Dockerfile.dev"
    click DOCKER2 "https://github.com/ducgiangtrankma/nestjs-boilerplate/blob/main/docker-compose.dev.yml"
    click ENV "https://github.com/ducgiangtrankma/nestjs-boilerplate/blob/main/.env.sample"
```
