# rasouza/node-clean-architecture architecture

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

- Interactive diagram: https://gitdiagram.com/rasouza/node-clean-architecture
- Repository: https://github.com/rasouza/node-clean-architecture

## Overview

##### Project Type and Core Architecture

- Domain (Core Business Layer)
- Application (Use Cases)
- Ports/Adapters (Interface Adapters)
- Infrastructure (External Frameworks/Tools)

##### Main Components

A. Core Components (Inner Layers):
- Domain Layer
- User Entity (domain/User.js)
- Repository Interfaces

- Application Layer
- Use Cases:
- CreateUser
- DeleteUser
- GetUser
- ListUsers
- UpdateUser

B. Interface Adapters:
- HTTP Controllers (ports/http)
- UsersController
- Routes Handler

C. Infrastructure (Outer Layer):
- Database
- MongoDB Implementation
- Mongoose Schemas
- Web Server (Fastify)
- Repositories
- UserRepositoryMongo
- UserRepositoryInMemory (for testing)

##### Second Ring (Application Layer)
- Use cases around the domain

##### Third Ring (Interface Adapters)
- Controllers and routes

##### Outer Ring (Infrastructure)
- External elements (MongoDB, Fastify, etc.)

##### Important Elements to Highlight

A. Dependency Flow:
- Dependency Injection container (infrastructure/bootstrap/container.js)

B. Data Flow:
- Typical request flow:
1. HTTP Request → Fastify
2. Routes → Controller
3. Controller → Use Case
4. Use Case → Domain
5. Repository Interface → MongoDB Implementation

C. Cross-Cutting Concerns:
- Logging system (infrastructure/bootstrap/logger.js)
- Error handling (infrastructure/webserver/errors.js)

##### Technical Details

- Technologies:
- Fastify (Web Framework)
- MongoDB/Mongoose (Database)
- Awilix (Dependency Injection)
- Jest (Testing)

##### Additional Notes

- A small section showing the testing setup with UserRepositoryInMemory

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Domain
        User["User Entity"]:::domain
    end

    subgraph Application
        CreateUser["Create User"]:::usecase
        GetUser["Get User"]:::usecase
        ListUsers["List Users"]:::usecase
        UpdateUser["Update User"]:::usecase
        DeleteUser["Delete User"]:::usecase
    end

    subgraph Adapters
        UsersController["Users Controller"]:::adapter
        Routes["Routes Handler"]:::adapter
        ErrorHandler["Error Handler"]:::adapter
    end

    subgraph Infrastructure
        Fastify["Fastify Server"]:::infra
        MongoDB["MongoDB"]:::database
        UserSchema["User Schema"]:::infra
        UserRepoMongo["User Repository MongoDB"]:::infra
        UserRepoMemory["User Repository InMemory"]:::infra
        Container["Dependency Container"]:::infra
        Logger["Logger"]:::infra
        DBBootstrap["Database Bootstrap"]:::infra
    end

    %% Dependencies and Data Flow
    Routes --> UsersController
    UsersController --> CreateUser
    UsersController --> GetUser
    UsersController --> ListUsers
    UsersController --> UpdateUser
    UsersController --> DeleteUser

    CreateUser --> User
    GetUser --> User
    ListUsers --> User
    UpdateUser --> User
    DeleteUser --> User

    UserRepoMongo --> MongoDB
    UserSchema --> MongoDB
    
    Container --> UserRepoMongo
    Container --> UserRepoMemory
    Container --> Logger
    Container --> DBBootstrap

    Fastify --> Routes
    Fastify --> ErrorHandler

    %% Click Events
    click User "https://github.com/rasouza/node-clean-architecture/blob/master/domain/User.js"
    click CreateUser "https://github.com/rasouza/node-clean-architecture/blob/master/application/CreateUser.js"
    click GetUser "https://github.com/rasouza/node-clean-architecture/blob/master/application/GetUser.js"
    click ListUsers "https://github.com/rasouza/node-clean-architecture/blob/master/application/ListUsers.js"
    click UpdateUser "https://github.com/rasouza/node-clean-architecture/blob/master/application/UpdateUser.js"
    click DeleteUser "https://github.com/rasouza/node-clean-architecture/blob/master/application/DeleteUser.js"
    click UsersController "https://github.com/rasouza/node-clean-architecture/blob/master/ports/http/UsersController.js"
    click Routes "https://github.com/rasouza/node-clean-architecture/blob/master/ports/http/routes.js"
    click ErrorHandler "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/webserver/errors.js"
    click MongoDB "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/database/mongo.js"
    click UserSchema "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/database/schemas/User.js"
    click UserRepoMongo "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/repositories/UserRepositoryMongo.js"
    click UserRepoMemory "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/repositories/UserRepositoryInMemory.js"
    click Container "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/bootstrap/container.js"
    click Logger "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/bootstrap/logger.js"
    click Fastify "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/webserver/fastify.js"
    click DBBootstrap "https://github.com/rasouza/node-clean-architecture/blob/master/infrastructure/bootstrap/database.js"

    %% Styles
    classDef domain fill:#2d5c8f,color:#fff
    classDef usecase fill:#5b9bd5,color:#fff
    classDef adapter fill:#70ad47,color:#fff
    classDef infra fill:#a5a5a5,color:#fff
    classDef database fill:#d4d4d4,color:#000

    %% Legend
    subgraph Legend
        L1["Domain Layer"]:::domain
        L2["Application Layer"]:::usecase
        L3["Adapter Layer"]:::adapter
        L4["Infrastructure Layer"]:::infra
        L5[("Database")]:::database
    end
```
