# geversonloch/clean-node-api architecture

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

- Interactive diagram: https://gitdiagram.com/geversonloch/clean-node-api
- Repository: https://github.com/geversonloch/clean-node-api

## Overview

##### Project type and purpose
- This is a backend‑only RESTful API built with Node.js and Express, following Clean Architecture principles.
- Its core feature is user signup (AddAccount), using TDD, SOLID principles, and a modular layered structure.

##### File structure
- Top‑level layers under `src/`:
- presentation (controllers, HTTP helpers, errors)
- domain (models, use‑case interfaces)
- data (use‑case implementations, protocol interfaces)
- infrastructure (cryptography, database adapters)
- main (Express adapters, config, factories, middleware, routes, server bootstrap)
- utils (email validation adapter)
- Tests are co‑located with each implementation, reflecting TDD.
- Config files: `.env`, Express app setup, MongoDB connection in `main/config`.
- Build/deploy: no separate infra code beyond MongoDB adapter; deployment is via Node process.

##### Key insights from README
- Clean Architecture with well‑defined layers
- TDD with Jest + MongoDB in‑memory
- Tech stack: Express, MongoDB, bcrypt, dotenv, module‑alias, fast‑glob for dynamic route loading

- Infrastructure Layer
- External Services
1. Client → HTTP Request → Express Server
2. Express Server → Route Loader → Route Matches → ExpressRouteAdapter
3. Adapter → Controller.handle(httpRequest)
4. Controller → Calls Use‑Case (AddAccount) via Domain Interface
5. Use‑Case → Calls Encrypter + AddAccountRepository interfaces
6. Encrypter Adapter (bcrypt) → returns hashed password
7. Use‑Case → Calls MongoDBAdapter to persist account
8. MongoDBAdapter → communicates with MongoDB cluster
9. If errors occur, log via LogErrorRepository → MongoDB log collection
10. Use‑Case returns result → Controller constructs HTTP response
11. Response → back through Express → Client
c. Architectural patterns & design principles
- Dependency Inversion: Domain layer defines interfaces, data & infra implement them
- Factory pattern in `main/factories/signup.ts` to assemble controller + use‑case + adapters
- Decorator pattern for logging controller errors (`LogControllerDecorator`)
- Middleware pipeline (body-parser, CORS, content-type JSON)
d. Relevant technologies
- Express (HTTP server, routes)
- Jest + Jest‑MongoDB (testing)
- MongoDB (data store)
- bcrypt (password hashing)
- dotenv (environment config)
- fast‑glob (dynamic route discovery)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External
    Client(["External Client<br/>(Browser/Postman)"]):::external

    %% Presentation Layer
    subgraph "Presentation Layer" 
        direction TB
        ERAdapter["ExpressRouteAdapter"]:::adapter
        Controller["SignupController"]:::presentation
        HTTPHelper["HTTP Helpers"]:::presentation
        ErrorHandlers["Error Handlers"]:::presentation
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        UseCase["AddAccount Use-Case"]:::domain
        AccountModel["AccountModel"]:::domain
        AddAccountModel["AddAccountModel"]:::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        DbAddAccount["DbAddAccount (Use-Case Impl)"]:::data
        AddAccountRepo["AddAccountRepository Interface"]:::data
        EncrypterIF["Encrypter Interface"]:::data
        DbAdapterIF["DatabaseAdapter Interface"]:::data
        LogErrorRepoIF["LogErrorRepository Interface"]:::data
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        BcryptAdapter["BcryptAdapter (Cryptography)"]:::infra
        MongoAdapter["MongoDBAdapter"]:::infra
        AccountRepo["Account Repository"]:::infra
        LogRepo["Log Repository"]:::infra
    end

    %% Main/Framework Layer
    subgraph "Main (Framework & Config)"
        direction TB
        Server["HTTP & Express Server"]:::framework
        RouteLoader["Route Loader & Express Routes"]:::framework
        Factories["Signup Factory"]:::framework
        Decorator["LogControllerDecorator"]:::framework
        Middlewares["Middleware Pipeline"]:::framework
        ConfigApp["Express App Setup"]:::framework
        EnvConfig["Env Configuration"]:::framework
        DBConn["DB Connection"]:::framework
    end

    %% External Services
    subgraph "External Resources"
        direction TB
        MongoDB["MongoDB Cluster"]:::external
        DotEnv[".env Configuration Service"]:::external
    end

    %% Connections
    Client -->|"HTTP Request"| Server
    Server -->|"loads routes"| RouteLoader
    RouteLoader -->|"matches signup route"| ERAdapter
    ERAdapter -->|"handle(httpRequest)"| Decorator
    Decorator -->|"wraps"| Controller
    Controller -->|"calls execute()"| UseCase
    UseCase -->|"validates with"| AddAccountModel
    UseCase -->|"validates with"| AccountModel
    UseCase -->|"calls encrypt() on"| EncrypterIF
    EncrypterIF -->|"implemented by"| BcryptAdapter
    UseCase -->|"calls add() on"| AddAccountRepo
    AddAccountRepo -->|"implemented by"| AccountRepo
    UseCase -->|"on error logs via"| LogErrorRepoIF
    LogErrorRepoIF -->|"implemented by"| LogRepo
    UseCase -->|"returns result"| Controller
    Controller -->|"builds response via"| HTTPHelper
    Controller -->|"throws errors using"| ErrorHandlers
    Controller -->|"returns HTTP response"| Server
    Server -->|"HTTP Response"| Client
    MongoAdapter -->|"connects to"| MongoDB
    DBConn -->|"initializes"| MongoAdapter
    EnvConfig -->|"loads env vars"| DotEnv
    ConfigApp -->|"sets up app"| Server
    Middlewares -->|"applies to"| Server
    Factories -->|"wires dependencies for signup"| Controller

    %% Click Events
    click Server "https://github.com/geversonloch/clean-node-api/blob/master/src/main/server.ts"
    click RouteLoader "https://github.com/geversonloch/clean-node-api/blob/master/src/main/config/routes.ts"
    click ERAdapter "https://github.com/geversonloch/clean-node-api/blob/master/src/main/adapters/express-route-adapter.ts"
    click Controller "https://github.com/geversonloch/clean-node-api/blob/master/src/presentation/controllers/signup.ts"
    click HTTPHelper "https://github.com/geversonloch/clean-node-api/blob/master/src/presentation/helpers/http-helper.ts"
    click ErrorHandlers "https://github.com/geversonloch/clean-node-api/blob/master/src/presentation/errors/internal-server-error.ts"
    click ErrorHandlers "https://github.com/geversonloch/clean-node-api/blob/master/src/presentation/errors/invalid-param-error.ts"
    click ErrorHandlers "https://github.com/geversonloch/clean-node-api/blob/master/src/presentation/errors/missing-param-error.ts"
    click UseCase "https://github.com/geversonloch/clean-node-api/blob/master/src/domain/usecases/add-account.interface.ts"
    click AccountModel "https://github.com/geversonloch/clean-node-api/blob/master/src/domain/models/account-model.interface.ts"
    click AddAccountModel "https://github.com/geversonloch/clean-node-api/blob/master/src/domain/models/add-account-model.interface.ts"
    click DbAddAccount "https://github.com/geversonloch/clean-node-api/blob/master/src/data/usecases/add-acount/db-add-account.ts"
    click AddAccountRepo "https://github.com/geversonloch/clean-node-api/blob/master/src/data/protocols/add-account-repository.interface.ts"
    click EncrypterIF "https://github.com/geversonloch/clean-node-api/blob/master/src/data/protocols/encrypter.interface.ts"
    click DbAdapterIF "https://github.com/geversonloch/clean-node-api/blob/master/src/data/protocols/database-adapter.interface.ts"
    click LogErrorRepoIF "https://github.com/geversonloch/clean-node-api/blob/master/src/data/protocols/log-error-repository.interface.ts"
    click BcryptAdapter "https://github.com/geversonloch/clean-node-api/blob/master/src/infrastructure/cryptography/bcrypt-adapter.ts"
    click MongoAdapter "https://github.com/geversonloch/clean-node-api/blob/master/src/infrastructure/db/mongodb/mongodb-adapter.ts"
    click AccountRepo "https://github.com/geversonloch/clean-node-api/blob/master/src/infrastructure/db/mongodb/account-repository/account.ts"
    click LogRepo "https://github.com/geversonloch/clean-node-api/blob/master/src/infrastructure/db/mongodb/log-repository/log.ts"
    click Factories "https://github.com/geversonloch/clean-node-api/blob/master/src/main/factories/signup.ts"
    click Decorator "https://github.com/geversonloch/clean-node-api/blob/master/src/main/decorators/log-controller-decorator.ts"
    click Middlewares "https://github.com/geversonloch/clean-node-api/blob/master/src/main/config/middlewares.ts"
    click ConfigApp "https://github.com/geversonloch/clean-node-api/blob/master/src/main/config/app.ts"
    click EnvConfig "https://github.com/geversonloch/clean-node-api/blob/master/src/main/config/env-config.ts"
    click DBConn "https://github.com/geversonloch/clean-node-api/blob/master/src/main/config/db-connection.ts"
    click RouteLoader "https://github.com/geversonloch/clean-node-api/blob/master/src/main/routes/signup-routes.ts"
    click Middlewares "https://github.com/geversonloch/clean-node-api/blob/master/src/main/middlewares/body-parser.ts"
    click Middlewares "https://github.com/geversonloch/clean-node-api/blob/master/src/main/middlewares/content-type-json.ts"
    click Middlewares "https://github.com/geversonloch/clean-node-api/blob/master/src/main/middlewares/cors.ts"
    click EnvConfig "https://github.com/geversonloch/clean-node-api/blob/master/src/utils/email-validator-adapter.ts"

    %% Styles
    classDef external fill:#dddddd,stroke:#333,stroke-width:1px
    classDef framework fill:#aaaaaa,stroke:#333,stroke-width:1px
    classDef adapter fill:#ccccff,stroke:#333,stroke-width:1px
    classDef presentation fill:#bbe1fa,stroke:#333,stroke-width:1px
    classDef domain fill:#d4edda,stroke:#333,stroke-width:1px
    classDef data fill:#ffe082,stroke:#333,stroke-width:1px
    classDef infra fill:#d1c4e9,stroke:#333,stroke-width:1px
```
