# jbuget/nodejs-clean-architecture-app architecture

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

- Interactive diagram: https://gitdiagram.com/jbuget/nodejs-clean-architecture-app
- Repository: https://github.com/jbuget/nodejs-clean-architecture-app

## Overview

##### Project Type and Purpose
- The project is a backend API built with Hapi.js. It cleanly separates concerns using Clean Architecture principles and integrates Domain Driven Design. It includes layers to handle use cases (application business rules), core business/domain logic, and supporting infrastructure (such as ORMs, security, web server configuration), with adapters (controllers, routes, serializers) for interfacing with external systems.

##### File Structure
- The top-level files (e.g., index.js, README.md, docker-compose.yml) show that the entry point is index.js while configuration and deployment are facilitated externally.
- Under the /lib directory you have distinct subdirectories:
- application - Contains use cases and security services. These are the business rules (like CreateUser, DeleteUser, etc.).
- domain - Contains your core business objects (e.g., User) and related interfaces (e.g., UserRepository) that define contracts.
- infrastructure - Houses configurations, database ORMs (with support for both Mongoose and Sequelize), repository implementations (in-memory, Mongo, SQLite), security (JWT-based), and the web server components including OAuth strategies.
- interfaces - Contains controllers (route handlers), route definitions, and serializers that format data in/out for HTTP requests.
- The separate test folder further confirms unit/functional tests for key business use cases and controllers.

##### README for Additional Insights
- The README explicitly documents the Clean Architecture layers, providing examples of how the application is divided.

##### Final Checklist for the Principal Software Engineer
- That every component (from routes to repositories) is represented and clearly labeled.
- Incorporate notes about the use of multiple ORM options and the Service Locator pattern.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "External"
        EC["External Client"]:::external
    end

    subgraph "Web Server Layer"
        HS["Hapi.js Web Server"]:::web
    end

    subgraph "Interface / Adapter Layer"
        subgraph "Routes & Plugins"
            R1["Route: hello.js"]:::interface
            R2["Route: private.js"]:::interface
            R3["Route: users.js"]:::interface
            OP["OAuth Plugin"]:::interface
        end
        subgraph "Controllers & Serializers"
            AC["Authorization Controller"]:::interface
            HC["Hello Controller"]:::interface
            UC["Users Controller"]:::interface
            USerSerial["User Serializer"]:::interface
        end
    end

    subgraph "Application Layer"
        CU["CreateUser"]:::application
        DU["DeleteUser"]:::application
        GU["GetUser"]:::application
        LU["ListUsers"]:::application
        SH["SayHello"]:::application
        GAT["GetAccessToken"]:::application
        VAT["VerifyAccessToken"]:::application
        AppSec["Access Token Manager (App)"]:::application
    end

    subgraph "Domain Layer"
        UDM["User (Domain Model)"]:::domain
        URI["User Repository (Domain Interface)"]:::domain
    end

    subgraph "Infrastructure Layer"
        Cfg["Config & Service Locator"]:::infrastructure
        ORM_M["ORM: Mongoose (User Schema)"]:::infrastructure
        ORM_Sm["ORM: Sequelize (User Model)"]:::infrastructure
        ORM_Ss["ORM: Sequelize (Sequelize.js)"]:::infrastructure
        URInM["User Repo InMemory"]:::infrastructure
        URIM["User Repo Mongo"]:::infrastructure
        URIS["User Repo SQLite"]:::infrastructure
        JWTI["JWT Access Token Manager (Infra)"]:::infrastructure
    end

    %% Connections Flow
    EC -->|"sendsRequest"| HS

    HS -->|"routes"| R1
    HS -->|"routes"| R2
    HS -->|"routes"| R3
    HS -->|"plugins"| OP

    R1 -->|"calls"| HC
    R2 -->|"calls"| AC
    R3 -->|"calls"| UC

    HC -->|"invokes"| SH
    UC -->|"invokes"| CU
    UC -->|"invokes"| DU
    UC -->|"invokes"| GU
    UC -->|"invokes"| LU
    AC -->|"invokes"| GAT
    AC -->|"invokes"| VAT

    %% Use Cases to Application Security and Domain
    GAT -->|"dependsOn"| AppSec
    VAT -->|"dependsOn"| AppSec
    AppSec -->|"uses"| JWTI

    CU -->|"uses"| URI
    DU -->|"uses"| URI
    GU -->|"uses"| URI
    LU -->|"uses"| URI

    URI -->|"implementedBy"| URInM
    URI -->|"implementedBy"| URIM
    URI -->|"implementedBy"| URIS
    URI -->|"models"| UDM

    %% Infrastructure Support Connections
    Cfg -->|"configures"| URInM
    Cfg -->|"configures"| URIM
    Cfg -->|"configures"| URIS

    URIM -->|"uses"| ORM_M
    URIS -->|"uses"| ORM_Sm
    URIS -->|"uses"| ORM_Ss

    %% Controller uses Serializer
    UC -->|"formatsWith"| USerSerial

    %% Click Events
    click HS "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/index.js"
    click R1 "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/routes/hello.js"
    click R2 "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/routes/private.js"
    click R3 "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/routes/users.js"
    click OP "https://github.com/jbuget/nodejs-clean-architecture-app/tree/master/lib/infrastructure/webserver/oauth"
    click AC "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/controllers/AuthorizationController.js"
    click HC "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/controllers/HelloController.js"
    click UC "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/controllers/UsersController.js"
    click USerSerial "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/interfaces/serializers/UserSerializer.js"
    click CU "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/CreateUser.js"
    click DU "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/DeleteUser.js"
    click GU "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/GetUser.js"
    click LU "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/ListUsers.js"
    click SH "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/SayHello.js"
    click GAT "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/GetAccessToken.js"
    click VAT "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/use_cases/VerifyAccessToken.js"
    click AppSec "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/application/security/AccessTokenManager.js"
    click UDM "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/domain/User.js"
    click URI "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/domain/UserRepository.js"
    click Cfg "https://github.com/jbuget/nodejs-clean-architecture-app/tree/master/lib/infrastructure/config"
    click ORM_M "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/orm/mongoose/schemas/User.js"
    click ORM_Sm "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/orm/sequelize/models/User.js"
    click ORM_Ss "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/orm/sequelize/sequelize.js"
    click URInM "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/repositories/UserRepositoryInMemory.js"
    click URIM "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/repositories/UserRepositoryMongo.js"
    click URIS "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/repositories/UserRepositorySQLite.js"
    click JWTI "https://github.com/jbuget/nodejs-clean-architecture-app/blob/master/lib/infrastructure/security/JwtAccessTokenManager.js"

    %% Styles
    classDef external fill:#FFCCCC,stroke:#333,stroke-width:2px;
    classDef web fill:#CCFFCC,stroke:#333,stroke-width:2px;
    classDef interface fill:#CCCCFF,stroke:#333,stroke-width:2px;
    classDef application fill:#FFFFCC,stroke:#333,stroke-width:2px;
    classDef domain fill:#FFEECC,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#DDEEFF,stroke:#333,stroke-width:2px;

    class EC external;
    class HS web;
    class R1,R2,R3,OP,AC,HC,UC,USerSerial interface;
    class CU,DU,GU,LU,SH,GAT,VAT,AppSec application;
    class UDM,URI domain;
    class Cfg,ORM_M,ORM_Sm,ORM_Ss,URInM,URIM,URIS,JWTI infrastructure;
```
