# lumitech-co/lumitech-node-fastify-template architecture

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

- Interactive diagram: https://gitdiagram.com/lumitech-co/lumitech-node-fastify-template
- Repository: https://github.com/lumitech-co/lumitech-node-fastify-template

## Overview

##### Project type and purpose
- This is a back-end application template (not full-stack), designed to bootstrap production-ready REST APIs in Node.js using Fastify.
- It provides built-in support for Swagger docs, Docker, Prisma/ PostgreSQL, dependency injection (Awilix), validation (Zod), and code-generation CLIs for modules and repositories.

##### File structure
- Top-level:
- cli/ – code generators for modules & repositories
- src/ – main application code
- test/ – unit and integration tests
- Dockerfile & docker-compose.yml – container orchestration
- GitHub workflows, linting, commit hooks
- Under src/:
- plugins/ – Fastify plugins (DI, CORS, JWT, Swagger, Zod, error handling, env, Prisma)
- lib/ – abstracted third-party services & helpers (hashing, Awilix config, validation schemas)
- database/ – Prisma schema, migrations, generic repository generator + custom repositories
- modules/ – feature modules (routes → handlers → services → utilities)
- types/ – global TS types & DI container types
- server.ts & index.ts – Fastify server bootstrap
- Patterns: layered modular architecture, repository pattern, DI container, plugin-based extension.

##### README for additional insights
- Stack: TypeScript, Fastify, Zod, Swagger, Awilix, Prisma, PostgreSQL, Vitest
- Architectural highlights:
- Dependency Injection via Awilix plugin
- Swagger auto-docs from route schemas
- Prisma ORM + generic repository generator
- Docker Compose network for isolation
- Conventional commits, pre-commit hooks

a. Main components
1. Client / API Consumer (e.g., Postman, frontend, CURL)
2. Fastify HTTP Server
– Core server instance (server.ts)
– Plugin layer:
- env loader → cors → jwt auth → prisma client → awilix DI → validation (Zod) → swagger docs → error handler
3. DI Container (Awilix)
– Registers: log, config, emailService, prismaClient, repositories, services, handlers
4. Application Modules
– Routes (message.route.ts)
– Handlers (message.handler.ts)
– Services (message.service.ts + message.util.ts)
5. Data Access Layer
– Generic repository generator (generate.repository.ts)
– Custom repositories (message.repository.ts)
6. Database
– Prisma Client ↔ PostgreSQL container
7. CLI Tools (module-gen, repository-gen)
8. Testing Harness
– Vitest unit & integration tests
– Docker Compose test environment
b. Relationships & data flows
- Request flow: Client → Fastify HTTP Server → Plugin chain → Route matching → Handler → Service → Repository → Prisma → PostgreSQL → back up chain → Client
- DI resolution: Server bootstrap → Awilix plugin instantiates & wires dependencies for handlers/services/repos
- Swagger: Plugin extracts schemas from routes to generate /docs endpoint
- CLI: Standalone node scripts invoked outside of runtime to scaffold new modules/repositories
- Test flows: Tests spin up in-memory or containerized DB, boot Fastify + DI and hit services or endpoints directly.
c. Architectural patterns & principles
- Layered architecture: transport → application → domain → persistence
- Dependency Injection (Inversion of Control)
- Repository pattern with generic CRUD generator
- Plugin-based extension (Fastify)
- Modular code-gen for consistency and DRY
d. Key technologies & frameworks
- Node.js + TypeScript
- Fastify & Fastify plugins
- Awilix DI
- Prisma ORM → PostgreSQL
- Zod validation
- Swagger (fastify-swagger)
- Docker & Docker Compose
- Vitest & Docker for testing

##### Detail-level advice
- Layer in a swim-lane or stacked fashion.
- A section for “External Services/Utilities” (e.g., email provider in lib, future Redis, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client / API Consumer"]:::external

    %% Server
    subgraph "Fastify HTTP Server" 
        direction TB
        Index["index.ts"]:::server
        ServerCore["server.ts"]:::server
        subgraph "Plugin Layer"
            direction TB
            Env["env"]:::plugin
            Cors["cors"]:::plugin
            JWT["jwt"]:::plugin
            PrismaPlugin["prisma"]:::plugin
            AwilixPlugin["awilix DI"]:::plugin
            Zod["zod"]:::plugin
            Swagger["swagger"]:::plugin
            ErrorHandler["error handler"]:::plugin
        end
    end

    %% DI Container
    subgraph "Dependency Injection (Awilix)" 
        direction TB
        AwilixConfig["awilix.ts"]:::di
        ResolverReg["resolver-registration.ts"]:::di
    end

    %% Application Modules
    subgraph "Application Modules" 
        direction TB
        SubApp["application.ts"]:::modules
        subgraph "Message Module"
            direction TB
            MsgIndex["index.ts"]:::modules
            MsgRoute["message.route.ts"]:::modules
            MsgHandler["message.handler.ts"]:::modules
            MsgService["message.service.ts"]:::modules
            MsgUtil["message.util.ts"]:::modules
        end
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        direction TB
        GenRepo["generate.repository.ts"]:::data
        MsgRepo["message.repository.ts"]:::data
    end

    %% Database
    subgraph "Database (Prisma + PostgreSQL)" 
        direction TB
        Schema["schema.prisma"]:::database
        PrismaType["prisma.type.ts"]:::database
        Migrations["migrations"]:::database
        Database["PostgreSQL"]:::database
    end

    %% CLI Tools
    subgraph "CLI Tools"
        direction TB
        CLIModule["module-gen"]:::cli
        CLIRepo["repository-gen"]:::cli
    end

    %% Testing Harness
    subgraph "Testing Harness"
        direction TB
        UnitTests["unit tests"]:::test
        IntTests["integration tests"]:::test
        TestSetup["test setup"]:::test
        DockerTest["docker-compose.test.yml"]:::test
        VitestUnit["vitest-unit.config.ts"]:::test
        VitestInt["vitest-int.config.ts"]:::test
    end

    %% Flows
    Client -->|"HTTP Request"| Index
    Index --> ServerCore
    ServerCore --> Env
    Env --> Cors
    Cors --> JWT
    JWT --> PrismaPlugin
    PrismaPlugin --> AwilixPlugin
    AwilixPlugin --> Zod
    Zod --> Swagger
    Swagger --> ErrorHandler
    ErrorHandler --> SubApp
    AwilixPlugin --> AwilixConfig
    AwilixConfig --> ResolverReg
    ResolverReg --> MsgHandler
    SubApp --> MsgRoute
    MsgRoute --> MsgHandler
    MsgHandler --> MsgService
    MsgService --> MsgUtil
    MsgService --> MsgRepo
    MsgRepo --> GenRepo
    MsgRepo --> Database
    Database --> PrismaPlugin
    Swagger -->|"Swagger JSON"| Client

    CLIModule -.->|scaffolding| MsgIndex
    CLIRepo -.->|scaffolding| MsgRepo

    UnitTests -.-> MsgService
    IntTests -.-> ServerCore
    TestSetup -.-> Database

    %% Click Events
    click CLIModule "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/cli/module-gen"
    click CLIRepo "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/cli/repository-gen"
    click Index "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/index.ts"
    click ServerCore "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/server.ts"
    click Env "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/env.ts"
    click Cors "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/cors.ts"
    click JWT "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/jwt.ts"
    click PrismaPlugin "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/prisma.ts"
    click AwilixPlugin "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/awilix.ts"
    click Zod "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/zod.ts"
    click Swagger "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/swagger.ts"
    click ErrorHandler "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/plugins/error.ts"
    click AwilixConfig "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/lib/awilix/awilix.ts"
    click ResolverReg "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/lib/awilix/resolver-registration.ts"
    click MsgRoute "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/message/message.route.ts"
    click MsgHandler "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/message/message.handler.ts"
    click MsgService "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/message/message.service.ts"
    click MsgUtil "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/message/message.util.ts"
    click MsgIndex "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/message/index.ts"
    click SubApp "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/modules/application.ts"
    click GenRepo "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/database/repositories/generate.repository.ts"
    click MsgRepo "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/database/repositories/message/message.repository.ts"
    click Schema "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/database/prisma/schema.prisma"
    click PrismaType "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/src/database/prisma/prisma.type.ts"
    click Migrations "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/src/database/prisma/migrations"
    click UnitTests "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/test/unit/modules/message"
    click IntTests "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/test/int/modules/message"
    click TestSetup "https://github.com/lumitech-co/lumitech-node-fastify-template/tree/main/test/int/setup"
    click DockerTest "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/docker-compose.test.yml"
    click VitestUnit "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/vitest-unit.config.ts"
    click VitestInt "https://github.com/lumitech-co/lumitech-node-fastify-template/blob/main/vitest-int.config.ts"

    %% Styles
    classDef external fill:#EFEFEF,stroke:#333,stroke-width:1px
    classDef server fill:#D6EFFF,stroke:#333,stroke-width:1px
    classDef plugin fill:#BFD7FF,stroke:#333,stroke-width:1px
    classDef di fill:#FFE2AC,stroke:#333,stroke-width:1px
    classDef modules fill:#D3F8E2,stroke:#333,stroke-width:1px
    classDef data fill:#E4D1FF,stroke:#333,stroke-width:1px
    classDef database fill:#C8C8C8,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef cli fill:#FFD6D6,stroke:#333,stroke-width:1px
    classDef test fill:#F0F0F0,stroke:#333,stroke-width:1px
```
