# chaweroty/habittabackend architecture

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

- Interactive diagram: https://gitdiagram.com/chaweroty/habittabackend
- Repository: https://github.com/chaweroty/habittabackend

## Overview

##### Project type and purpose
- This is a Node.js-based RESTful backend service (Express.js) called “habittaBackend”
- It exposes HTTP endpoints for user and subscription management, persists data in a SQL database, and uses JWT for auth

##### File structure
- Top-level files:
- package.json / package-lock.json → Node dependencies and scripts
- database.sql → initial schema or seed data
- src/ directory follows a layered architecture:
- src/server.js & src/app.js → express app setup and server bootstrap
- src/config/database.js → DB connection config
- src/container/DIContainer.js → custom dependency-injection setup
- src/routes/*.js → HTTP route definitions (authRoutes, userRoutes)
- src/middleware/*.js → cross-cutting concerns (auth, validation, error handling)
- src/controllers/*.js → translate requests to service calls
- src/services/*.js → business logic, orchestrates repositories and utils
- src/repositories/*.js → data access layer (UserRepository, SubscriptionRepository)
- src/models/*.js → ORM or data models (User, Subscription)
- src/schemas/*.js → request-validation schemas (e.g. Joi)
- src/utils/*.js → helper functions (hashing, JWT handling)
- src/scripts/initDb.js → DB initialization script

##### Extract insights from README
- Minimal README. We rely primarily on code structure and naming to infer architecture.

##### Detail
- Call out error handling in the middleware chain
- Where validation and authentication occur
- Architectural elements—even small ones like utility modules—to give a complete picture

## Diagram (Mermaid)

```mermaid
flowchart TB
    ExternalClients["External Clients"]:::infra
    Server["Express Server<br/>(src/app.js, src/server.js)"]:::infra
    DI["DI Container<br/>(src/container/DIContainer.js)"]:::config

    subgraph "API Surface"
        AuthRoutes["Auth Routes<br/>(src/routes/authRoutes.js)"]:::api
        UserRoutes["User Routes<br/>(src/routes/userRoutes.js)"]:::api
    end

    subgraph "Middleware Layer"
        AuthMW["Auth Middleware<br/>(src/middleware/auth.js)"]:::middleware
        ValMW["Validation Middleware<br/>(src/middleware/validation.js)"]:::middleware
        ErrMW["Error Handler<br/>(src/middleware/errorHandler.js)"]:::middleware
    end

    subgraph "Controller Layer"
        UserController["User Controller<br/>(src/controllers/UserController.js)"]:::controller
    end

    subgraph "Service Layer"
        UserService["User Service<br/>(src/services/UserService.js)"]:::service
    end

    subgraph "Repository Layer"
        UserRepo["User Repository<br/>(src/repositories/UserRepository.js)"]:::repository
        SubRepo["Subscription Repository<br/>(src/repositories/SubscriptionRepository.js)"]:::repository
    end

    subgraph "Models"
        UserModel["User Model<br/>(src/models/User.js)"]:::model
        SubModel["Subscription Model<br/>(src/models/Subscription.js)"]:::model
    end

    subgraph "Utilities"
        HashUtil["Hash Utils<br/>(src/utils/hash.js)"]:::util
        JwtUtil["JWT Utils<br/>(src/utils/jwt.js)"]:::util
    end

    subgraph "Infrastructure"
        ConfigDB["DB Config<br/>(src/config/database.js)"]:::config
        Database["SQL Database<br/>(database.sql)"]:::infra
        InitDb["Init DB Script<br/>(src/scripts/initDb.js)"]:::script
    end

    ExternalClients -->|"HTTP Request"| Server
    Server -->|"bootstrap"| DI
    DI --> AuthRoutes
    DI --> UserRoutes
    AuthRoutes -->|"route match"| AuthMW
    UserRoutes -->|"route match"| ValMW
    AuthMW -->|"authenticated request"| UserController
    ValMW -->|"validated request"| UserController
    UserController -->|"invoke business logic"| UserService
    UserService -->|"data operations"| UserRepo
    UserService -->|"data operations"| SubRepo
    UserRepo -->|"SQL Query"| Database
    SubRepo -->|"SQL Query"| Database
    UserService -->|"hash/jwt"| HashUtil
    AuthMW -->|"verify token"| JwtUtil
    InitDb -->|"read config"| ConfigDB
    InitDb -->|"setup schema"| Database
    ConfigDB -->|"connect"| Database

    click Server "https://github.com/chaweroty/habittabackend/blob/main/src/server.js"
    click Server "https://github.com/chaweroty/habittabackend/blob/main/src/app.js"
    click ConfigDB "https://github.com/chaweroty/habittabackend/blob/main/src/config/database.js"
    click Database "https://github.com/chaweroty/habittabackend/blob/main/database.sql"
    click DI "https://github.com/chaweroty/habittabackend/blob/main/src/container/DIContainer.js"
    click AuthRoutes "https://github.com/chaweroty/habittabackend/blob/main/src/routes/authRoutes.js"
    click UserRoutes "https://github.com/chaweroty/habittabackend/blob/main/src/routes/userRoutes.js"
    click AuthMW "https://github.com/chaweroty/habittabackend/blob/main/src/middleware/auth.js"
    click ValMW "https://github.com/chaweroty/habittabackend/blob/main/src/middleware/validation.js"
    click ErrMW "https://github.com/chaweroty/habittabackend/blob/main/src/middleware/errorHandler.js"
    click UserController "https://github.com/chaweroty/habittabackend/blob/main/src/controllers/UserController.js"
    click UserService "https://github.com/chaweroty/habittabackend/blob/main/src/services/UserService.js"
    click UserRepo "https://github.com/chaweroty/habittabackend/blob/main/src/repositories/UserRepository.js"
    click SubRepo "https://github.com/chaweroty/habittabackend/blob/main/src/repositories/SubscriptionRepository.js"
    click UserModel "https://github.com/chaweroty/habittabackend/blob/main/src/models/User.js"
    click SubModel "https://github.com/chaweroty/habittabackend/blob/main/src/models/Subscription.js"
    click HashUtil "https://github.com/chaweroty/habittabackend/blob/main/src/utils/hash.js"
    click JwtUtil "https://github.com/chaweroty/habittabackend/blob/main/src/utils/jwt.js"
    click InitDb "https://github.com/chaweroty/habittabackend/blob/main/src/scripts/initDb.js"

    classDef api fill:#FFDDAA,stroke:#FFAA00
    classDef middleware fill:#FFDDDD,stroke:#FF5555
    classDef controller fill:#DDFFDD,stroke:#55AA55
    classDef service fill:#DDEEFF,stroke:#5599CC
    classDef repository fill:#FFEE99,stroke:#CCAA00
    classDef model fill:#EECCFF,stroke:#AA00AA
    classDef util fill:#DDDDFF,stroke:#0000FF
    classDef config fill:#FFFFCC,stroke:#AAAA00
    classDef script fill:#CCFFCC,stroke:#008800
    classDef infra fill:#CCCCCC,stroke:#333333
```
