# devsanjithm/express-prisma-ts architecture

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

- Interactive diagram: https://gitdiagram.com/devsanjithm/express-prisma-ts
- Repository: https://github.com/devsanjithm/express-prisma-ts

## Overview

##### High-Level Components

a. External Layer:
- Client Applications (Mobile/Web)
- Third-party Services (SMTP Server for emails)
- AWS S3 (indicated by fileUpload.aws.ts)

b. API Layer:
- Express.js Server
- API Routes (v1)
- Authentication/Authorization
- Rate Limiting
- Request Validation
- XSS Protection

c. Core Application Layer:
- Controllers
- Services
- Middleware
- Validation Layer

d. Data Layer:
- PostgreSQL Database
- Prisma ORM
- Cache Service

**[External Clients/Services Layer]**
- ↕
**[API Gateway Layer]**
- ↕
**[Core Application Layer]**
- ↕
**[Data Layer]**

##### Key Components and Relationships

a. API Gateway Layer should show:
- Routes (auth.route, user.route, fileUpload.route, etc.)
- Security Middleware (auth, rateLimiter, xss)
- Request Validation
- Error Handling

b. Core Application Layer should show:
- Controllers (auth.controller, user.controller, etc.)
- Services (auth.service, email.service, token.service, etc.)
- Business Logic Flow

c. Data Layer should show:
- Prisma ORM
- PostgreSQL Database
- Cache Service

##### Important Flows to Highlight

a. Authentication Flow:
**Client → Auth Routes → Auth Controller → Auth Service → Token Service → Database**

b. Data Flow:
**Client → Routes → Validation → Controller → Service → Prisma → Database**

##### Additional Elements

- Version information (v1)
- Environment configuration flow
- Test coverage paths

##### Technical Details

- Express.js as the main framework
- Prisma as the ORM
- PostgreSQL as the database
- Passport.js for authentication
- JWT for token management
- Winston/Morgan for logging
- Joi for validation
- PM2 for process management

Remember to include notes about scalability considerations, especially regarding the database and caching layers, and how the architecture supports the features mentioned in the README (like Docker support, testing, and security measures).

This system design should clearly show how the project implements the MVC pattern while maintaining clear separation of concerns between routes, controllers, services, and data access layers.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph External["External Layer"]
        Client["Client Applications"]
        SMTP["SMTP Server"]
        S3["AWS S3"]:::aws
    end

    subgraph Gateway["API Gateway Layer"]
        Express["Express.js Server"]
        Routes["API Routes v1"]:::routes
        AuthRoute["Auth Routes"]:::routes
        UserRoute["User Routes"]:::routes
        FileRoute["File Upload Routes"]:::routes
        
        Security["Security Layer"]
        RateLimit["Rate Limiter"]:::security
        XSS["XSS Protection"]:::security
        Validate["Request Validation"]:::security
        AuthMid["Auth Middleware"]:::security
    end

    subgraph Core["Core Application Layer"]
        Controllers["Controllers Layer"]:::controller
        AuthController["Auth Controller"]:::controller
        UserController["User Controller"]:::controller
        FileController["File Upload Controller"]:::controller
        
        Services["Services Layer"]:::service
        AuthService["Auth Service"]:::service
        TokenService["Token Service"]:::service
        EmailService["Email Service"]:::service
        CacheService["Cache Service"]:::service
        
        Validation["Validation Rules"]:::validation
        Utils["Utilities"]:::utils
        ErrorHandler["Error Handling"]:::utils
        Response["Response Handler"]:::utils
        AsyncHandler["Async Handler"]:::utils
        Encryption["Data Encryption"]:::utils
    end

    subgraph Data["Data Layer"]
        Prisma["Prisma ORM"]:::data
        PostgreSQL["PostgreSQL"]:::database
        PrismaActions["Database Actions"]:::data
    end

    subgraph Documentation["Documentation Layer"]
        Swagger["Swagger/OpenAPI"]:::docs
        APIComponents["API Components"]:::docs
    end

    subgraph Logging["Logging System"]
        Winston["Winston Logger"]:::logging
        Morgan["Morgan HTTP Logger"]:::logging
    end

    %% Relationships
    Client --> Express
    Express --> Routes
    Routes --> AuthRoute & UserRoute & FileRoute
    
    AuthRoute --> AuthController
    UserRoute --> UserController
    FileRoute --> FileController
    
    Security --> RateLimit & XSS & Validate & AuthMid
    
    Controllers --> Services
    Services --> Prisma
    Prisma --> PostgreSQL
    
    AuthService --> TokenService
    AuthService --> EmailService
    Services --> CacheService
    
    EmailService --> SMTP
    FileController --> S3

    %% Click Events
    click Routes "https://github.com/devsanjithm/express-prisma-ts/tree/main/src/routes/v1/"
    click AuthRoute "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/routes/v1/auth.route.ts"
    click UserRoute "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/routes/v1/user.route.ts"
    click FileRoute "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/routes/v1/fileUpload.route.ts"
    click AuthMid "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/middlewares/auth.ts"
    click RateLimit "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/middlewares/rateLimiter.ts"
    click XSS "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/middlewares/xss.ts"
    click Validate "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/middlewares/validate.ts"
    click Controllers "https://github.com/devsanjithm/express-prisma-ts/tree/main/src/controllers/"
    click Services "https://github.com/devsanjithm/express-prisma-ts/tree/main/src/services/"
    click Validation "https://github.com/devsanjithm/express-prisma-ts/tree/main/src/validations/"
    click Prisma "https://github.com/devsanjithm/express-prisma-ts/blob/main/prisma/schema.prisma"
    click PrismaActions "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/config/prismaActions.prisma.ts"
    click CacheService "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/services/cache.service.ts"
    click S3 "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/config/fileUpload.aws.ts"
    click EmailService "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/services/email.service.ts"
    click AuthController "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/controllers/auth.controller.ts"
    click AuthService "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/services/auth.service.ts"
    click TokenService "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/services/token.service.ts"
    click Winston "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/config/logger.ts"
    click Morgan "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/config/morgan.ts"
    click Swagger "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/docs/swaggerDef.ts"
    click APIComponents "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/docs/components.yml"
    click ErrorHandler "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/utils/ApiError.ts"
    click Response "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/utils/response.ts"
    click AsyncHandler "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/utils/catchAsync.ts"
    click Encryption "https://github.com/devsanjithm/express-prisma-ts/blob/main/src/utils/encryption.ts"

    %% Styling
    classDef default fill:#fff,stroke:#333,stroke-width:2px;
    classDef routes fill:#90EE90,stroke:#333;
    classDef security fill:#FFB6C1,stroke:#333;
    classDef controller fill:#FFE4B5,stroke:#333;
    classDef service fill:#87CEEB,stroke:#333;
    classDef validation fill:#DDA0DD,stroke:#333;
    classDef utils fill:#F0E68C,stroke:#333;
    classDef data fill:#98FB98,stroke:#333;
    classDef database fill:#B0C4DE,stroke:#333;
    classDef docs fill:#FFA07A,stroke:#333;
    classDef logging fill:#E6E6FA,stroke:#333;
    classDef aws fill:#FF9900,stroke:#333;
```
