# gitdagray/nestjs-course architecture

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

- Interactive diagram: https://gitdiagram.com/gitdagray/nestjs-course
- Repository: https://github.com/gitdagray/nestjs-course

## Overview

##### Project Type and Purpose
- This repository is a multi-chapter, hands-on NestJS tutorial. Each top-level folder (lesson01–lesson06) is a standalone NestJS REST API illustrating progressively advanced concepts: from controllers and providers, through DTO validation and error handling, to Prisma-backed database access and cross-cutting concerns (CORS, rate limiting, logging, exception filters).

##### File Structure
- Each lesson is structured as a standard NestJS application:
- nest-cli.json, tsconfig.json/tsconfig.build.json, package.json, ESLint/Prettier configs
- src/ containing main.ts (application entry), app.module.ts (root module), controllers, services, and any feature modules (users, employees, database, my-logger)
- test/ and src/**/*.spec.ts for e2e and unit tests
- Lessons 5 and 6 add:
- prisma/ folder with schema.prisma and migration scripts
- database module for PrismaClient injections
- cross-cutting filters and services (all-exceptions.filter.ts, my-logger)
- README sets the educational context and links each lesson.

##### README for Additional Insights
- Node.js + TypeScript + NestJS
- Prisma ORM + Neon Serverless Postgres
- Class-Validator for DTO validation
- Middleware: CORS, rate limiting, exception filters, custom logger

a. Main Components
- Client (e.g., browser, Postman, front-end)
- NestJS Application
– Entry Point (main.ts)
– AppModule (root DI container)
– Feature Modules (UsersModule, EmployeesModule, DatabaseModule, MyLoggerModule)
– Controllers (handling HTTP routes)
– Services (business logic)
– Pipes/Filters (validation pipe, exception filter)
- Prisma Client (injected via DatabaseModule)
- PostgreSQL Database (Neon serverless)
- External Cross-cutting Services (rate limiter, CORS middleware, custom logger)
b. Relationships & Interactions
1. HTTP request → NestJS middleware (CORS, rate limiting)
2. Validation Pipe transforms/validates DTOs
3. Controller receives validated request
4. Controller invokes Service via DI
5. Service calls PrismaClient to query/update DB
6. PrismaClient communicates with PostgreSQL
7. DB response bubbles up through Service → Controller
8. Exception Filter catches unhandled errors, formats response
9. Response returned to Client
c. Architectural Patterns & Principles
- Modular Monolith: logically separated modules, each with its own controller/service
- Dependency Injection: Nest’s built-in DI container wires controllers, services, PrismaClient
- Layered Architecture: Controllers (API layer) → Services (Business layer) → Prisma/DB (Data layer)
- Validation & Error Handling: DTOs with class-validator, global exception filters
- Configuration & Migrations: Prisma schema + migration scripts for reproducible DB state
d. Key Technologies & Frameworks
- NestJS (modules, controllers, providers)
- TypeScript + Node.js
- Prisma ORM, Neon Postgres
- class-validator & class-transformer
- Jest (unit/e2e testing)
- Middleware: CORS, rate limiting libraries

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["Client (Browser/Postman)"]:::external

    %% Middleware & Filters
    subgraph "Cross-Cutting Middleware & Filters"
        CORS["CORS Middleware"]:::middleware
        RateLimiter["Rate Limiting"]:::middleware
        ValidationPipe["Validation Pipe"]:::filter
        ExceptionFilter["Global Exception Filter"]:::filter
    end

    %% Entry Point and Root Module
    MainTS["main.ts"]:::module
    AppModule["AppModule"]:::module

    %% Feature Modules
    subgraph "UsersModule" 
        UsersController["UsersController"]:::controller
        UsersService["UsersService"]:::service
        CreateUserDTO["create-user.dto.ts"]:::dto
        UpdateUserDTO["update-user.dto.ts"]:::dto
    end

    subgraph "EmployeesModule"
        EmployeesController["EmployeesController"]:::controller
        EmployeesService["EmployeesService"]:::service
    end

    subgraph "DatabaseModule"
        DatabaseService["DatabaseService"]:::service
        PrismaSchema["schema.prisma"]:::database
        Migrations["migrations"]:::database
    end

    subgraph "MyLoggerModule"
        MyLoggerService["MyLoggerService"]:::service
    end

    %% External Systems
    PostgresDB["Neon PostgreSQL"]:::database

    %% Tests
    E2ETests["app.e2e-spec.ts"]:::test
    UnitTests["unit/e2e specs (*.spec.ts)"]:::test

    %% Flow
    Client -->|"HTTP Request"| CORS
    CORS --> RateLimiter
    RateLimiter --> ValidationPipe
    ValidationPipe -->|"validateDTO()"| MainTS
    MainTS --> AppModule
    AppModule --> UsersModule
    AppModule --> EmployeesModule
    AppModule --> DatabaseModule
    AppModule --> MyLoggerModule

    %% Users Flow
    UsersModule --> UsersController
    UsersController -->|"calls"| UsersService
    UsersService --> DatabaseService
    ValidationPipe --> CreateUserDTO
    ValidationPipe --> UpdateUserDTO

    %% Employees Flow
    EmployeesModule --> EmployeesController
    EmployeesController -->|"calls"| EmployeesService
    EmployeesService --> DatabaseService

    %% Database Flow
    DatabaseService --> PrismaSchema
    DatabaseService --> Migrations
    DatabaseService -->|"executeQuery()"| PostgresDB
    PostgresDB -->|"queryResult"| DatabaseService

    %% Exception Handling
    UsersService -- "error" --> ExceptionFilter
    EmployeesService -- "error" --> ExceptionFilter
    ExceptionFilter -->|"formatError()"| Client

    %% Logging
    MyLoggerModule --> MyLoggerService
    AppModule --> MyLoggerService

    %% Tests connections
    E2ETests --> AppModule
    UnitTests --> UsersService
    UnitTests --> EmployeesService

    %% Click Events
    click MainTS "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/main.ts"
    click AppModule "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/app.module.ts"
    click UsersModule "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/users/users.module.ts"
    click UsersController "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/users/users.controller.ts"
    click UsersService "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/users/users.service.ts"
    click CreateUserDTO "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/users/dto/create-user.dto.ts"
    click UpdateUserDTO "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/users/dto/update-user.dto.ts"
    click EmployeesModule "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/employees/employees.module.ts"
    click EmployeesController "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/employees/employees.controller.ts"
    click EmployeesService "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/employees/employees.service.ts"
    click DatabaseModule "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/database/database.module.ts"
    click DatabaseService "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/database/database.service.ts"
    click PrismaSchema "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/prisma/schema.prisma"
    click Migrations "https://github.com/gitdagray/nestjs-course/tree/main/lesson06/prisma/migrations"
    click ExceptionFilter "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/all-exceptions.filter.ts"
    click MyLoggerModule "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/my-logger/my-logger.module.ts"
    click MyLoggerService "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/src/my-logger/my-logger.service.ts"
    click E2ETests "https://github.com/gitdagray/nestjs-course/blob/main/lesson06/test/app.e2e-spec.ts"

    %% Styling
    classDef external fill:#f9f,stroke:#333,stroke-width:1px;
    classDef middleware fill:#e0b3ff,stroke:#9255b0,stroke-width:1px;
    classDef filter fill:#d1f2ff,stroke:#1f8ea6,stroke-width:1px;
    classDef module fill:#cce5ff,stroke:#339af0,stroke-width:1px;
    classDef controller fill:#d4edda,stroke:#28a745,stroke-width:1px;
    classDef service fill:#fff3cd,stroke:#ffc107,stroke-width:1px;
    classDef dto fill:#f0f0f0,stroke:#999,stroke-width:1px;
    classDef database fill:#f5c6cb,stroke:#e55353,stroke-width:1px;
    classDef test fill:#e2e3e5,stroke:#6c757d,stroke-width:1px;
```
