# royib/clean-architecture-nestjs architecture

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

- Interactive diagram: https://gitdiagram.com/royib/clean-architecture-nestjs
- Repository: https://github.com/royib/clean-architecture-nestjs

## Overview

##### Project’s Nature and Purpose
- This project is a backend API built with NestJS and TypeScript that follows the principles of Clean Architecture. Its purpose is to structure the business logic (use cases and entities) independently from the implementation details (such as database access and external CRM services).

##### File Structure and Map to Architectural Layers
- Core Domain and Business Logic:
- Use Cases:
- Use cases for handling business rules are in “src/use-cases” (further partitioned by domain – author, book, genre). This layer orchestrates how the core business rules are applied to specific functions.
- Controllers:
- The “src/controllers” folder contains the entry points for HTTP requests. These represent the “gateways” into the use cases.
- Services:
- “src/services” holds service modules for handling external operations, grouped further into CRM services and data services. They act as intermediaries that connect controllers and use-case logic with the underlying implementations.
- Frameworks (Implementation Details):
- The “src/frameworks” directory leverages specific implementations for external concerns such as data persistence and CRM integrations:
- Under “src/frameworks/data-services/mongo”, you have the models, data services modules, and repository implementations for MongoDB.
- Under “src/frameworks/crm-services/salesforce”, you have the specific modules and service implementations for integrating with Salesforce.
- Configuration and Bootstrapping:
- Files like “src/configuration” and “src/main.ts” provide setup and bootstrap details. While these aren’t core to the layered design, they support the overall infrastructure.

##### Main Components
- Inner Core:
- Entities (business objects) – represent the domain model.
- DTOs and Abstracts – define contracts and data structures used by the use cases.
- Business Use Cases:
- Represented by the “use-cases” modules, each with specific logic for author, book, and genre functionalities.
- Interface/Delivery Mechanisms:
- Controllers – serve as the entry points translating HTTP (or other) requests into calls to use cases.
- External Interfaces:
- Frameworks – including:
- Data Services (Mongo) for persistence.
- CRM Services (Salesforce) for external integrations.
- Service Layer:
- Contains additional orchestration logic that can be presented as intermediaries between use cases and frameworks.

##### Relationships and Data Flows
- Controllers call use cases.
- Use cases invoke the domain entities and leverage abstractions defined in the core.
- Use cases indirectly access external systems and persistence layers through the service layer and framework implementations.
- That the innermost layers (entities and use cases) are independent of the outer layers (frameworks).

##### Important Architectural Patterns and Design Principles
- Clean Architecture:
- That any external detail (database, third-party CRM) is isolated in the outer layers.
- SOLID Principles:
- The abstract classes and DTOs support a design where concrete implementations can be swapped without impacting core logic.
- Modular Separation:
- Each domain (author, book, genre) has its own use-case folder with corresponding factories, ensuring separation concerning business functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration and Bootstrapping Layer
    subgraph "Configuration & Bootstrapping"
        CONF["Configuration (index.ts)"]:::configuration
        MAIN["Main (main.ts)"]:::configuration
    end

    %% Controllers Layer
    subgraph "Controllers"
        APP_CTRL["App Controller"]:::controller
        AUTHOR_CTRL["Author Controller"]:::controller
        BOOK_CTRL["Book Controller"]:::controller
        GENRE_CTRL["Genre Controller"]:::controller
    end

    %% Business Use Cases Layer
    subgraph "Business Use Cases"
        AUTHOR_USE["Author Use Case"]:::usecase
        BOOK_USE["Book Use Case"]:::usecase
        GENRE_USE["Genre Use Case"]:::usecase
    end

    %% Core Domain Layer
    CORE["Core Domain (Entities, DTOs, Abstracts)"]:::core

    %% Service Layer
    SERVICE["Service Layer"]:::service

    %% External Frameworks Layer
    subgraph "Framework Implementations"
        DATA_SERV["Data Services (Mongo)"]:::framework
        CRM_SERV["CRM Services (Salesforce)"]:::framework
    end

    %% Connections for vertical dependency flow
    CONF --- MAIN
    MAIN -->|"bootstraps"| APP_CTRL
    APP_CTRL -->|"invokes"| AUTHOR_USE
    APP_CTRL -->|"invokes"| BOOK_USE
    APP_CTRL -->|"invokes"| GENRE_USE

    AUTHOR_CTRL -->|"calls"| AUTHOR_USE
    BOOK_CTRL -->|"calls"| BOOK_USE
    GENRE_CTRL -->|"calls"| GENRE_USE

    AUTHOR_USE -->|"uses"| CORE
    BOOK_USE -->|"uses"| CORE
    GENRE_USE -->|"uses"| CORE

    DATA_SERV -->|"provides to"| SERVICE
    CRM_SERV -->|"provides to"| SERVICE
    SERVICE -->|"supports"| AUTHOR_USE
    SERVICE -->|"supports"| BOOK_USE
    SERVICE -->|"supports"| GENRE_USE

    %% Click Events
    click CONF "https://github.com/royib/clean-architecture-nestjs/blob/main/src/configuration/index.ts"
    click MAIN "https://github.com/royib/clean-architecture-nestjs/blob/main/src/main.ts"

    click APP_CTRL "https://github.com/royib/clean-architecture-nestjs/blob/main/src/controllers/app.controller.ts"
    click AUTHOR_CTRL "https://github.com/royib/clean-architecture-nestjs/blob/main/src/controllers/author.controller.ts"
    click BOOK_CTRL "https://github.com/royib/clean-architecture-nestjs/blob/main/src/controllers/book.controller.ts"
    click GENRE_CTRL "https://github.com/royib/clean-architecture-nestjs/blob/main/src/controllers/genre.controller.ts"

    click AUTHOR_USE "https://github.com/royib/clean-architecture-nestjs/tree/main/src/use-cases/author"
    click BOOK_USE "https://github.com/royib/clean-architecture-nestjs/tree/main/src/use-cases/book"
    click GENRE_USE "https://github.com/royib/clean-architecture-nestjs/tree/main/src/use-cases/genre"

    click CORE "https://github.com/royib/clean-architecture-nestjs/tree/main/src/core"
    click SERVICE "https://github.com/royib/clean-architecture-nestjs/tree/main/src/services"

    click DATA_SERV "https://github.com/royib/clean-architecture-nestjs/tree/main/src/frameworks/data-services/mongo"
    click CRM_SERV "https://github.com/royib/clean-architecture-nestjs/tree/main/src/frameworks/crm-services/salesforce"

    %% Styles
    classDef configuration fill:#C7D2FE,stroke:#000,stroke-width:1px;
    classDef controller fill:#A7D8F4,stroke:#000,stroke-width:1px;
    classDef usecase fill:#FDE68A,stroke:#000,stroke-width:1px;
    classDef core fill:#BBF7D0,stroke:#000,stroke-width:1px;
    classDef service fill:#FBCFE8,stroke:#000,stroke-width:1px;
    classDef framework fill:#FDA4AF,stroke:#000,stroke-width:1px;
```
