# tkosminov/nestjs-example architecture

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

- Interactive diagram: https://gitdiagram.com/tkosminov/nestjs-example
- Repository: https://github.com/tkosminov/nestjs-example

## Overview

##### Project type and purpose
- This is a monolithic backend application built with NestJS.
- It exposes GraphQL (with schema stitching, hot-reload, subscriptions, uploads, cache), REST endpoints (file uploads), WebSocket gateways, uses RabbitMQ for messaging, Redis for caching, JWT for auth, and PostgreSQL via TypeORM for persistence.
- There is no dedicated “frontend” code other than simple HTML samples; the focus is on demonstrating a robust NestJS server architecture.

##### File structure
- Top-level files: Dockerfile, docker-compose.yml, nginx.conf, config/.env, README, package.json, tsconfig → containerization, env management, build config.
- src/app.module.ts, main.ts → NestJS application bootstrap.
- src/graphql, src/upload/graphql, src/upload/rest, src/ws, src/oauth, src/jwt, src/rabbitmq, src/redis, src/typeorm, src/models → functional modules grouped by domain and integration.
- src/logger, src/cors, src/healthz → cross-cutting concerns (logging, CORS middleware, health check).
- config/, docker-related files → deployment and runtime configuration.

##### README for additional insights
- Describes required services: Redis, RabbitMQ, PostgreSQL.
- Commands for local and Docker usage.
- GraphQL features (stitching, reload), RabbitMQ, Redis, WebSocket, JWT, file upload, TypeORM migrations.

##### Final recommendations
- Be detailed: break the monolith into its constituent modules to show how each integrates.
- Cross-cutting middleware and global providers (logger, CORS, JWT).
- Flows (HTTP, WS, messaging) are visible with arrowheads and labels.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        ClientHTML["HTML Samples"]:::client
    end
    click ClientHTML "https://github.com/tkosminov/nestjs-example/blob/master/html/socket.html"

    %% Edge Layer
    subgraph "Edge Layer"
        direction TB
        Nginx["Nginx Proxy"]:::edge
    end
    ClientHTML -->|HTTP/WS Requests| Nginx
    click Nginx "https://github.com/tkosminov/nestjs-example/blob/master/nginx.conf"

    %% Application Layer
    subgraph "NestJS Application"    
        direction TB
        %% Bootstrap
        subgraph "Bootstrap"
            direction TB
            Main["main.ts"]:::bootstrap
            AppModule["app.module.ts"]:::bootstrap
            AppEnv["app.env.ts"]:::bootstrap
        end
        click Main "https://github.com/tkosminov/nestjs-example/blob/master/src/main.ts"
        click AppModule "https://github.com/tkosminov/nestjs-example/blob/master/src/app.module.ts"
        click AppEnv "https://github.com/tkosminov/nestjs-example/blob/master/src/app.env.ts"

        %% Cross-Cutting
        CORS["CORS Middleware"]:::cross
        LoggerModule["Logger Module"]:::cross
        Health["Health-Check"]:::cross

        click CORS "https://github.com/tkosminov/nestjs-example/blob/master/src/cors/cors.middleware.ts"
        click LoggerModule "https://github.com/tkosminov/nestjs-example/blob/master/src/logger/logger.module.ts"
        click Health "https://github.com/tkosminov/nestjs-example/blob/master/src/healthz/healthz.module.ts"

        %% Auth
        subgraph "OAuth & JWT"
            direction TB
            OAuthModule["OAuth Module"]:::auth
            JWTModule["JWT Module"]:::auth
        end
        click OAuthModule "https://github.com/tkosminov/nestjs-example/blob/master/src/oauth/oauth.module.ts"
        click JWTModule "https://github.com/tkosminov/nestjs-example/blob/master/src/jwt/jwt.module.ts"

        %% OAuth Sub-Entities
        subgraph "OAuth Entities"
            direction TB
            RecoveryKey["Recovery Key"]:::entity
            RefreshToken["Refresh Token"]:::entity
            UserEntity["User Entity"]:::entity
        end
        click RecoveryKey "https://github.com/tkosminov/nestjs-example/blob/master/src/oauth/recovery-key/recovery-key.module.ts"
        click RefreshToken "https://github.com/tkosminov/nestjs-example/blob/master/src/oauth/refresh-token/refresh-token.module.ts"
        click UserEntity "https://github.com/tkosminov/nestjs-example/blob/master/src/oauth/user/user.module.ts"

        %% Messaging & Cache
        RabbitMQ["RabbitMQ Module"]:::integration
        RedisModule["Redis Module"]:::integration
        click RabbitMQ "https://github.com/tkosminov/nestjs-example/blob/master/src/rabbitmq/rabbitmq.module.ts"
        click RedisModule "https://github.com/tkosminov/nestjs-example/blob/master/src/redis/redis.module.ts"

        %% Persistence
        TypeORMModule["TypeORM Module"]:::integration
        click TypeORMModule "https://github.com/tkosminov/nestjs-example/blob/master/src/typeorm/typeorm.module.ts"

        %% REPL
        REPL["REPL Module"]:::devops
        click REPL "https://github.com/tkosminov/nestjs-example/blob/master/src/repl.module.ts"

        %% File Upload
        FileUploadCore["Upload Core Module"]:::upload
        GraphQLUpload["GraphQL Upload"]:::upload
        RESTUpload["REST Upload"]:::upload

        click FileUploadCore "https://github.com/tkosminov/nestjs-example/blob/master/src/upload/upload.module.ts"
        click GraphQLUpload "https://github.com/tkosminov/nestjs-example/blob/master/src/upload/graphql/upload-graphql.module.ts"
        click RESTUpload "https://github.com/tkosminov/nestjs-example/blob/master/src/upload/rest/upload-rest.module.ts"

        %% WebSocket
        WS["WebSocket Gateway"]:::ws
        click WS "https://github.com/tkosminov/nestjs-example/blob/master/src/ws/ws.module.ts"

        %% GraphQL API
        subgraph "GraphQL API"
            direction TB
            GraphQLModule["GraphQL Module"]:::graphql
            subgraph "Hot-Reload"
                ReloaderMod["Reloader Module"]:::graphql
            end
            subgraph "Schema Stitching"
                StitchMod["Stitching Module"]:::graphql
            end
        end
        click GraphQLModule "https://github.com/tkosminov/nestjs-example/blob/master/src/graphql/graphql.module.ts"
        click ReloaderMod "https://github.com/tkosminov/nestjs-example/blob/master/src/graphql/reloader/reloader.module.ts"
        click StitchMod "https://github.com/tkosminov/nestjs-example/blob/master/src/graphql/stitching/stitching.module.ts"

        %% Domain Models
        subgraph "Domain Models"
            direction TB
            BookModule["Book Module"]:::model
            SectionModule["Section Module"]:::model
        end
        click BookModule "https://github.com/tkosminov/nestjs-example/blob/master/src/models/book/book.module.ts"
        click SectionModule "https://github.com/tkosminov/nestjs-example/blob/master/src/models/section/section.module.ts"

        %% Health check controller
        HealthController["healthz.controller.ts"]:::cross
        click HealthController "https://github.com/tkosminov/nestjs-example/blob/master/src/healthz/healthz.controller.ts"
    end

    Nginx -->|Forwards| Main

    %% Internal Flows
    Main --> AppModule
    AppModule --> CORS
    AppModule --> LoggerModule
    AppModule --> Health
    AppModule --> OAuthModule
    AppModule --> JWTModule
    AppModule --> RabbitMQ
    AppModule --> RedisModule
    AppModule --> TypeORMModule
    AppModule --> FileUploadCore
    AppModule --> WS
    AppModule --> GraphQLModule
    AppModule --> RESTUpload
    AppModule --> GraphQLUpload
    AppModule --> REPL
    AppModule --> BookModule
    AppModule --> SectionModule

    %% GraphQL internals
    GraphQLModule --> ReloaderMod
    GraphQLModule --> StitchMod

    %% File upload flows
    FileUploadCore --> GraphQLUpload
    FileUploadCore --> RESTUpload

    %% Persistence and integration
    TypeORMModule -->|ORM| PostgreSQL
    RedisModule -->|Cache| RedisCache
    RabbitMQ -->|Broker| RabbitBroker
    FileUploadCore -->|Store| FileStorage

    %% WebSocket Subscription
    WS -->|Push Updates| ClientHTML

    %% External Data Stores
    subgraph "Data & Integration"
        direction TB
        PostgreSQL["PostgreSQL"]:::db
        RedisCache["Redis Cache"]:::db
        RabbitBroker["RabbitMQ Broker"]:::db
        FileStorage["File System"]:::db
    end

    %% Dev & Ops
    subgraph "Dev & Ops"
        direction TB
        DockerFiles["Docker & Config"]:::devops
    end
    click DockerFiles "https://github.com/tkosminov/nestjs-example/blob/master/docker-compose.yml"

    %% Connections to Data & Ops
    TypeORMModule --> PostgreSQL
    RedisModule --> RedisCache
    RabbitMQ --> RabbitBroker
    FileUploadCore --> FileStorage

    %% Styles
    classDef client fill:#E8F0FE,stroke:#4285F4,color:#000
    classDef edge fill:#F1F3F4,stroke:#9AA0A6,color:#000
    classDef bootstrap fill:#E6F4EA,stroke:#34A853,color:#000
    classDef cross fill:#FFF4E5,stroke:#FBBC05,color:#000
    classDef auth fill:#FCE8E6,stroke:#EA4335,color:#000
    classDef integration fill:#E8F0FE,stroke:#4285F4,color:#000
    classDef devops fill:#E6F4EA,stroke:#34A853,color:#000
    classDef upload fill:#F3E6FF,stroke:#A142F4,color:#000
    classDef ws fill:#E6F4EA,stroke:#34A853,color:#000
    classDef graphql fill:#E8F0FE,stroke:#4285F4,color:#000
    classDef model fill:#FFF4E5,stroke:#FBBC05,color:#000
    classDef db fill:#F3E6FF,stroke:#A142F4,color:#000
```
