# saviopatrick/api-solid-node architecture

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

- Interactive diagram: https://gitdiagram.com/saviopatrick/api-solid-node
- Repository: https://github.com/saviopatrick/api-solid-node

## Overview

##### Main Components
- Client/API Request Layer: Represent external clients (e.g., mobile/web) that interact with your HTTP server.
- HTTP Layer: Include the HTTP server (see src/server.ts and src/app.ts) and the controllers (located in src/http/controllers like login.ts and register.ts). These components expose endpoints for registration, login, and (in planned features) check-ins and gym searches.
- Routing: Capture the routing mechanism (see src/http/routes.ts) that maps incoming HTTP requests to the appropriate controller functions.
- Use Cases/Business Logic: Highlight the use-case layer (in src/use-cases/) that implements the core functionalities (user registration, login, check-ins, and profile management) along with factories (in src/use-cases/factories/) that create specific use-case instances.
- Repository Layer: Split the repository layer into two parts:
- In-memory repositories (for testing purposes) from src/repositores/in-memory/.
- Prisma repositories (production-level implementations) found in src/repositores/prisma/ and src/lib/prisma.ts (which establishes the connection to the database).
- Database/ORM: The PostgreSQL database is accessed using Prisma. The prisma/schema.prisma file and migration directories describe the database schema and migrations.
- Error Handling: Components under src/errors (for various error cases like not-found, unauthorized, etc.) to represent a standardized error management mechanism.

##### Relationships and Interactions
- External Clients make HTTP requests to the API endpoints.
- The HTTP layer (server/app.ts) forwards requests to appropriate controllers (login, register, etc.).
- Controllers delegate business logic to the corresponding use-cases.
- Use-cases interact with the repository layer which abstracts data persistence.
- The Prisma repository interacts with the PostgreSQL database as defined in prisma/schema.prisma.
- Error handling modules are integrated throughout to handle exceptions and enforce business rules (e.g., duplicate email registration).
- JWT authentication is used to identify users and secure endpoints.

##### Architectural Patterns
- Layered Architecture: Separate concerns into distinct layers – HTTP server, controllers, use cases (application logic), and repositories (data access).
- Dependency Injection: Observe that use-case factories (src/use-cases/factories/) likely provide specific implementations (whether in-memory or Prisma-backed) to the use-case logic.
- Clean Architecture: The structure emphasizes clear separation between business rules (use cases) and external dependencies (repositories, controllers, database).

##### Relevant Technologies and Configurations
- Node.js/TypeScript as the implementation language.
- Prisma for ORM and migrations.
- PostgreSQL as the database.
- Docker (docker-compose.yml) indicates containerized deployment.
- ESLint/NPM configuration files (.eslintrc.json, package.json, etc.) for development standards and dependency management.
- JWT for authorization (as stated in the README regarding user identification).

##### Essential Architectural Elements
- Be as detailed as possible and capture not just the “layers,” but also how components relate:
- Use cases for register, login, and check-ins.
- Existence of both in-memory and Prisma repositories to reflect testing vs. production.
- How database migrations (prisma/migrations) feed into the PostgreSQL schema via Prisma.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["External Client"]:::client

    %% HTTP Layer
    subgraph "HTTP Layer"
        HTTPApp["HTTP Server (app.ts)"]:::http
        HTTPServer["HTTP Server (server.ts)"]:::http
        Routes["Routing (routes.ts)"]:::http
        LoginCtrl["Login Controller"]:::http
        RegisterCtrl["Register Controller"]:::http
    end

    %% Business Logic Layer
    subgraph "Business Logic"
        UseCaseFactories["Use Case Factories"]:::business
        LoginUC["Login Use Case"]:::business
        RegisterUC["Register Use Case"]:::business
        CheckInUC["Check-in Use Case"]:::business
    end

    %% Repository Layer
    subgraph "Repository Layer"
        subgraph "In-Memory Repositories"
            InMemUsers["In-Memory Users Repository"]:::repository
            InMemCheckIn["In-Memory Check-In Repository"]:::repository
        end
        subgraph "Prisma Repositories"
            PrismaUsers["Prisma Users Repository"]:::repository
            PrismaClient["Prisma Client"]:::repository
        end
    end

    %% Database/ORM Layer
    subgraph "Database/ORM"
        PostgresDB["((PostgreSQL Database))"]:::database
        PrismaSchema["Prisma Schema"]:::database
        PrismaMigrations["Prisma Migrations"]:::database
    end

    %% Error Handling and Deployment
    ErrorHandling["Error Handling"]:::error
    DockerCompose["Docker Compose"]:::container

    %% Connections
    Client -->|"sends request"| HTTPApp
    Client -->|"sends request"| HTTPServer

    HTTPApp -->|"forwards to"| Routes
    HTTPServer -->|"forwards to"| Routes

    Routes -->|"routes to"| LoginCtrl
    Routes -->|"routes to"| RegisterCtrl

    LoginCtrl -->|"delegates"| UseCaseFactories
    RegisterCtrl -->|"delegates"| UseCaseFactories

    UseCaseFactories -->|"creates"| LoginUC
    UseCaseFactories -->|"creates"| RegisterUC
    UseCaseFactories -->|"creates"| CheckInUC

    LoginUC -->|"uses"| PrismaUsers
    RegisterUC -->|"uses"| PrismaUsers
    CheckInUC -->|"uses"| InMemCheckIn

    PrismaUsers -->|"calls"| PrismaClient
    PrismaClient -->|"connects to"| PostgresDB

    PostgresDB -->|"defined by"| PrismaSchema
    PostgresDB -->|"migrated by"| PrismaMigrations

    LoginCtrl ---|"handles errors"| ErrorHandling
    RegisterCtrl ---|"handles errors"| ErrorHandling

    DockerCompose -->|"deploys"| HTTPServer
    DockerCompose -->|"deploys"| HTTPApp

    %% Click Events
    click HTTPApp "https://github.com/saviopatrick/api-solid-node/blob/main/src/app.ts"
    click HTTPServer "https://github.com/saviopatrick/api-solid-node/blob/main/src/server.ts"
    click Routes "https://github.com/saviopatrick/api-solid-node/blob/main/src/http/routes.ts"
    click LoginCtrl "https://github.com/saviopatrick/api-solid-node/blob/main/src/http/controllers/login.ts"
    click RegisterCtrl "https://github.com/saviopatrick/api-solid-node/blob/main/src/http/controllers/register.ts"
    click LoginUC "https://github.com/saviopatrick/api-solid-node/blob/main/src/use-cases/login.ts"
    click RegisterUC "https://github.com/saviopatrick/api-solid-node/blob/main/src/use-cases/register.ts"
    click CheckInUC "https://github.com/saviopatrick/api-solid-node/blob/main/src/use-cases/check-in.ts"
    click UseCaseFactories "https://github.com/saviopatrick/api-solid-node/tree/main/src/use-cases/factories/"
    click InMemUsers "https://github.com/saviopatrick/api-solid-node/blob/main/src/repositores/in-memory/in-memory-users-repository.ts"
    click InMemCheckIn "https://github.com/saviopatrick/api-solid-node/blob/main/src/repositores/in-memory/in-memory-check-in-repository.ts"
    click PrismaUsers "https://github.com/saviopatrick/api-solid-node/blob/main/src/repositores/prisma/prisma-users-repository.ts"
    click PrismaClient "https://github.com/saviopatrick/api-solid-node/blob/main/src/lib/prisma.ts"
    click PrismaSchema "https://github.com/saviopatrick/api-solid-node/blob/main/prisma/schema.prisma"
    click PrismaMigrations "https://github.com/saviopatrick/api-solid-node/tree/main/prisma/migrations/"
    click ErrorHandling "https://github.com/saviopatrick/api-solid-node/tree/main/src/errors/"
    click DockerCompose "https://github.com/saviopatrick/api-solid-node/blob/main/docker-compose.yml"

    %% Style Definitions
    classDef client fill:#8ecae6,stroke:#023047,stroke-width:2px;
    classDef http fill:#ffb703,stroke:#fb8500,stroke-width:2px;
    classDef business fill:#90be6d,stroke:#43aa8b,stroke-width:2px;
    classDef repository fill:#f9c74f,stroke:#f9844a,stroke-width:2px;
    classDef database fill:#a2d2ff,stroke:#4361ee,stroke-width:2px;
    classDef error fill:#fca5a5,stroke:#b91c1c,stroke-width:2px;
    classDef container fill:#d0f4de,stroke:#379683,stroke-width:2px;
```
