# joaovictormartins05/gympass-node-api architecture

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

- Interactive diagram: https://gitdiagram.com/joaovictormartins05/gympass-node-api
- Repository: https://github.com/joaovictormartins05/gympass-node-api

## Overview

##### Project Type and Purpose
- This is a back-end REST API (“full-stack” only on the server side) written in TypeScript.
- It follows a clean/hexagonal architecture with clearly separated layers (HTTP adapters, use-case interactors, repository interfaces/adapters).
- It persists data in PostgreSQL via Prisma ORM and provides user-registration and authentication (plus gyms and check-ins management).

##### File Structure
- Top-level config:  .env.example, docker-compose.yml, prisma/schema.prisma, migrations → indicates a database service in Docker.
- src/http: HTTP layer (controllers + routes).
- src/use-cases: business logic (authenticate, register).
- src/repositories: repository interfaces plus two adapters (in-memory for tests, Prisma for production).
- src/lib/prisma.ts: Prisma client instantiation.
- src/app.ts & src/server.ts: application setup and HTTP server bootstrap.

##### Main Components
a. HTTP Client (e.g., browser, mobile, Postman)
b. HTTP Server / API Gateway (Express/Fastify instance in src/server.ts)
c. Routing Layer (src/http/routes.ts)
d. Controllers (src/http/controllers/*.ts)
e. Use-Case Interactors (src/use-cases/*.ts)
f. Repository Interfaces (src/repositories/users-repository.ts)
g. Repository Adapters
– In-Memory Adapter (for local tests)
– Prisma Adapter (src/repositories/prisma/*.ts)
h. Prisma Client (src/lib/prisma.ts)
i. PostgreSQL Database (defined via docker-compose and prisma schema)
j. Environment / Config Layer (.env, src/env)
k. Test Harness (authenticate.spec.ts, register.spec.ts) – optional, can be a side component

##### Relationships and Data Flow
1) Client → sends HTTP request → HTTP Server
2) Server → routes.ts → Controller
3) Controller → calls appropriate Use-Case Interactor
4) Interactor → invokes Repository Interface
5) Repository Interface → at runtime bound to Prisma Adapter → uses Prisma Client → SQL to PostgreSQL
6) Database → returns rows → Prisma Client → Adapter → Interactor → Controller → HTTP response → Client
7) For tests, the same path but Repository Interface bound to In-Memory Adapter

##### Highlighted Architectural Patterns
- Hexagonal / Clean Architecture (layers with dependency inversion).
- Repository Pattern (abstract persistence behind an interface).
- Adapter Pattern (in-memory vs Prisma).
- Layered Request Flow (Controller → Use Case → Repository → DB).

##### Essential Details to Capture
– clear component names matching folder names
– two repository adapters (in-memory for test path, Prisma for production)
– Prisma schema and migrations block under the DB
– the flow of authentication tokens or credentials (authenticate.ts)
– environment loading (src/env) injecting DB connection string into Prisma Client

## Diagram (Mermaid)

```mermaid
flowchart TB
    Client["HTTP Client"]:::external

    subgraph "HTTP / API Layer"
        Server["HTTP Server"]:::http
        Routes["Routing Layer"]:::http
        AuthController["Authentication Controller"]:::http
        RegController["Registration Controller"]:::http
    end

    subgraph "Application Layer"
        AuthUseCase["Authenticate User Use Case"]:::app
        RegUseCase["Register User Use Case"]:::app
    end

    subgraph "Domain Layer"
        UserRepoInterface["User Repository Interface"]:::repo
    end

    subgraph "Infrastructure / Adapters"
        InMemAdapter["In-Memory Adapter"]:::infra
        PrismaAdapter["Prisma Adapter"]:::infra
        PrismaClient["Prisma Client"]:::infra
    end

    subgraph "Infrastructure / Data"
        Postgres[(PostgreSQL Database)]:::infra
        Schema["Prisma Schema"]:::infra
        Migrations["Migrations"]:::infra
        DockerComp["Docker Compose"]:::infra
    end

    subgraph "Configuration"
        EnvLoader["Env Loader"]:::infra
        EnvExample[".env.example"]:::infra
    end

    subgraph "Test Harness"
        AuthTest["Auth Use-Case Tests"]:::external
        RegTest["Register Use-Case Tests"]:::external
    end

    Client -->|"HTTP request"| Server
    Server -->|"routeRequest()"| Routes
    Routes -->|"to authenticate"| AuthController
    Routes -->|"to register"| RegController
    AuthController -->|"authenticate()"| AuthUseCase
    RegController -->|"register()"| RegUseCase
    AuthUseCase -->|"findUserByEmail()"| UserRepoInterface
    RegUseCase -->|"createUser()"| UserRepoInterface
    UserRepoInterface -->|"bound to"| PrismaAdapter
    UserRepoInterface -->|"bound to (test)"| InMemAdapter
    PrismaAdapter -->|"uses"| PrismaClient
    PrismaClient -->|"SQL queries"| Postgres
    Schema -->|"defines model"| PrismaClient
    Migrations -->|"apply migrations"| Postgres
    DockerComp -->|"orchestrates"| Postgres
    EnvExample -->|"sample vars"| EnvLoader
    EnvLoader -->|"DB_URL"| PrismaClient
    AuthTest -->|"tests"| AuthUseCase
    RegTest -->|"tests"| RegUseCase

    click Server "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/server.ts"
    click Routes "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/http/routes.ts"
    click AuthController "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/http/controllers/authenticate.ts"
    click RegController "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/http/controllers/register.ts"
    click AuthUseCase "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/use-cases/authenticate.ts"
    click RegUseCase "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/use-cases/register.ts"
    click UserRepoInterface "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/repositories/users-repository.ts"
    click InMemAdapter "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/repositories/in-memory/in-memory-repository.ts"
    click PrismaAdapter "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/repositories/prisma/prisma-users-repository.ts"
    click PrismaClient "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/lib/prisma.ts"
    click DockerComp "https://github.com/joaovictormartins05/gympass-node-api/blob/main/docker-compose.yml"
    click Schema "https://github.com/joaovictormartins05/gympass-node-api/blob/main/prisma/schema.prisma"
    click Migrations "https://github.com/joaovictormartins05/gympass-node-api/tree/main/prisma/migrations/*"
    click EnvExample "https://github.com/joaovictormartins05/gympass-node-api/blob/main/.env.example"
    click EnvLoader "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/env/index.ts"
    click AuthTest "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/use-cases/authenticate.spec.ts"
    click RegTest "https://github.com/joaovictormartins05/gympass-node-api/blob/main/src/use-cases/register.spec.ts"

    classDef http fill:#FFA500,stroke:#333,stroke-width:1px
    classDef app fill:#00AA00,stroke:#333,stroke-width:1px
    classDef repo fill:#FFFF00,stroke:#333,stroke-width:1px
    classDef infra fill:#0000FF,stroke:#333,stroke-width:1px,color:#FFF
    classDef external fill:#888888,stroke:#333,stroke-width:1px
```
