# harshitclub/node_scalable_backend architecture

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

- Interactive diagram: https://gitdiagram.com/harshitclub/node_scalable_backend
- Repository: https://github.com/harshitclub/node_scalable_backend

## Overview

##### Project Type & Purpose
- This is a Node.js/TypeScript backend service exposing REST endpoints for user authentication (signup, login, profile) and email verification.
- It uses Prisma for database access/migrations, Redis for caching & job queuing (Bull), and a worker process to send emails asynchronously.

##### Core Components to Model
a. Clients
– Web/mobile front end or API consumer.
b. API Service (Express.js)
– src/index.ts
– src/routes/auth.routes.ts → src/controllers/auth.controller.ts
– src/middlewares (auth, errorHandler, morgan)
– src/configs (logger, config, mailer, Prisma, RedisBull, RedisCache)
c. Database
– PostgreSQL/MySQL (as defined in prisma/schema.prisma & docker-compose.yml)
– Accessed via Prisma Client
d. Redis
– RedisCache (caching layer)
– RedisBull (job queue for emails)
e. Worker Process
– src/workers/email.worker.ts
– Subscribes to email.queue (src/queues/email.queue.ts)
– Uses src/utils/sendMail.ts & src/templates/verifyEmail.ts
f. External Services
– SMTP/Email provider (via mailer.ts)

##### Relationships & Data Flow
1) Client → HTTP Request → Express API
2) API Layer
– Authentication routes invoke controllers → business logic
– Controllers use Prisma client → Database
– Controllers push email jobs → RedisBull queue
– Optionally read/write cache → RedisCache
3) Worker
– Polls the RedisBull queue
– Processes jobs (render templates + sendMail) → External SMTP
4) API Response → Client

##### Key Architectural Patterns
- Monolith + Sidecar Worker: single codebase for API and a separate worker process managed by PM2 (ecosystem.config.js).
- Layered (Router → Controller → Utility/Config → DB/Cache/Queue)
- Asynchronous Queue for non-blocking email dispatch (Bull + Redis).
- Centralized error handling & logging (morgan, custom appError).
- Infrastructure as Code: Dockerfile + docker-compose.yml spin up API, Worker, Redis, Database.

##### Technologies & Frameworks
- Runtime: Node.js, TypeScript
- Web Framework: Express.js
- ORM: Prisma
- Caching/Queue: Redis, Bull
- Worker Manager: PM2 (ecosystem.config.js)
- Email: nodemailer (mailer.ts)
- Testing: YAML-based tests (tests/*.yml)
- CI/Reporting: report.json (likely code coverage/report)

##### Final Checklist
[ ] Grouping/containers for Docker services
[ ] Callouts for key modules (Prisma, RedisBull, Nodemailer)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client (Web/Mobile)"]:::external

    %% Docker Network
    subgraph "Docker Network"
        direction TB

        %% API Service Container
        subgraph "API Service (Express.js)" 
            direction TB
            API_Index["src/index.ts"]:::api
            Auth_Routes["auth.routes.ts"]:::api
            Auth_Controller["auth.controller.ts"]:::api
            Auth_Middleware["authMiddleware.ts"]:::middleware
            Error_Middleware["errorHandler.ts"]:::middleware
            Morgan_Middleware["morganMiddleware.ts"]:::middleware
            Config[/"Configuration Layer"/]:::config
            JWT["jwt.ts"]:::util
            Password["password.ts"]:::util
            AppError["appError.ts"]:::util
            JWTPayload["JWTPayload.ts"]:::types
            ExpressD["express.d.ts"]:::types
            Auth_Validator["auth.validator.ts"]:::validator
            User_Validator["user.validator.ts"]:::validator

            API_Index --> Auth_Routes
            Auth_Routes --> Auth_Controller
            Auth_Controller --> PrismaClient
            Auth_Controller --> RedisCache
            Auth_Controller --> RedisBull
            Auth_Controller --> AppError
            Auth_Controller --> JWT
            Auth_Controller --> Password
            Auth_Routes --> Auth_Validator
            Auth_Controller --> User_Validator
            Auth_Middleware --> JWTPayload
            Auth_Middleware --> AppError
            API_Index --> Auth_Middleware
            API_Index --> Error_Middleware
            API_Index --> Morgan_Middleware
            API_Index --> Config
            Config --> Logger
            Config --> Mailer
            Config --> PrismaConfig
            Config --> RedisBullConfig
            Config --> RedisCacheConfig
        end

        %% Worker Container
        subgraph "Worker Process" 
            direction TB
            Email_Worker["email.worker.ts"]:::worker
            Email_Queue["email.queue.ts"]:::queue
            SendMail["sendMail.ts"]:::worker
            Template["verifyEmail.ts"]:::worker

            Email_Worker --> Email_Queue
            Email_Worker --> SendMail
            SendMail --> Template
            Email_Worker --> RedisBull
        end

        %% Datastores
        subgraph "Datastores"
            direction TB
            PrismaClient[(Database)]:::db
            RedisCache[(Redis Cache)]:::cache
            RedisBull[(Redis Queue)]:::queue_store
        end

        %% External SMTP
        SMTP["SMTP Provider"]:::external

        %% Orchestration Files
        Dockerfile["Dockerfile"]:::infra
        DockerCompose["docker-compose.yml"]:::infra
        PM2Config["ecosystem.config.js"]:::infra
    end

    %% Data Flows
    Client -->|"HTTP/HTTPS"| API_Index
    PrismaClient -->|"SELECT/INSERT"| DatabaseInstance[(PostgreSQL/MySQL)]:::db
    RedisCache -->|"GET/SET"| Redis
    RedisBull -->|"PUSH job"| Redis
    Email_Worker -->|"DEQUEUE job"| Redis
    Email_Worker -->|"SMTP" | SMTP
    API_Index -->|Deploy| Dockerfile
    API_Index -->|Compose| DockerCompose
    Email_Worker -->|PM2| PM2Config

    %% Click Events
    click API_Index "https://github.com/harshitclub/node_scalable_backend/blob/main/src/index.ts"
    click Auth_Routes "https://github.com/harshitclub/node_scalable_backend/blob/main/src/routes/auth.routes.ts"
    click Auth_Controller "https://github.com/harshitclub/node_scalable_backend/blob/main/src/controllers/auth.controller.ts"
    click Auth_Middleware "https://github.com/harshitclub/node_scalable_backend/blob/main/src/middlewares/authMiddleware.ts"
    click Error_Middleware "https://github.com/harshitclub/node_scalable_backend/blob/main/src/middlewares/errorHandler.ts"
    click Morgan_Middleware "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/morganMiddleware.ts"
    click Config "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/config.ts"
    click Logger "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/logger.ts"
    click Mailer "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/mailer.ts"
    click PrismaConfig "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/prisma.ts"
    click RedisBullConfig "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/redisBull.ts"
    click RedisCacheConfig "https://github.com/harshitclub/node_scalable_backend/blob/main/src/configs/redisCache.ts"
    click PrismaClient "https://github.com/harshitclub/node_scalable_backend/blob/main/prisma/schema.prisma"
    click Email_Queue "https://github.com/harshitclub/node_scalable_backend/blob/main/src/queues/email.queue.ts"
    click Email_Worker "https://github.com/harshitclub/node_scalable_backend/blob/main/src/workers/email.worker.ts"
    click SendMail "https://github.com/harshitclub/node_scalable_backend/blob/main/src/utils/sendMail.ts"
    click Template "https://github.com/harshitclub/node_scalable_backend/blob/main/src/templates/verifyEmail.ts"
    click JWT "https://github.com/harshitclub/node_scalable_backend/blob/main/src/utils/jwt.ts"
    click Password "https://github.com/harshitclub/node_scalable_backend/blob/main/src/utils/password.ts"
    click AppError "https://github.com/harshitclub/node_scalable_backend/blob/main/src/utils/appError.ts"
    click JWTPayload "https://github.com/harshitclub/node_scalable_backend/blob/main/src/types/JWTPayload.ts"
    click ExpressD "https://github.com/harshitclub/node_scalable_backend/blob/main/src/types/express.d.ts"
    click Auth_Validator "https://github.com/harshitclub/node_scalable_backend/blob/main/src/validators/auth.validator.ts"
    click User_Validator "https://github.com/harshitclub/node_scalable_backend/blob/main/src/validators/user.validator.ts"
    click Dockerfile "https://github.com/harshitclub/node_scalable_backend/tree/main/Dockerfile"
    click DockerCompose "https://github.com/harshitclub/node_scalable_backend/blob/main/docker-compose.yml"
    click PM2Config "https://github.com/harshitclub/node_scalable_backend/blob/main/ecosystem.config.js"

    %% Styles
    classDef api fill:#D0E6FF,stroke:#1E90FF,stroke-width:2px
    classDef middleware fill:#F0F8FF,stroke:#87CEFA,stroke-width:2px
    classDef config fill:#E8F5E9,stroke:#2E7D32,stroke-width:2px
    classDef util fill:#FFF3E0,stroke:#FFB74D,stroke-width:2px
    classDef types fill:#EDE7F6,stroke:#5E35B1,stroke-width:2px
    classDef validator fill:#FCE4EC,stroke:#C2185B,stroke-width:2px
    classDef worker fill:#E1F5FE,stroke:#0288D1,stroke-width:2px
    classDef queue fill:#FFF8E1,stroke:#F9A825,stroke-width:2px
    classDef db fill:#E8F5E9,stroke:#43A047,stroke-width:2px
    classDef cache fill:#FFFDE7,stroke:#FBC02D,stroke-width:2px
    classDef queue_store fill:#FFF3E0,stroke:#FB8C00,stroke-width:2px
    classDef external fill:#ECEFF1,stroke:#78909C,stroke-width:2px
    classDef infra fill:#ECEFF1,stroke:#546E7A,stroke-width:2px
```
