# rcasachi/fastify-ts-boilerplate architecture

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

- Interactive diagram: https://gitdiagram.com/rcasachi/fastify-ts-boilerplate
- Repository: https://github.com/rcasachi/fastify-ts-boilerplate

## Overview

##### Project Type & Purpose
- Project Type: This is a backend boilerplate built in TypeScript using Fastify as the HTTP framework.
- Purpose: It provides a starting point for building Fastify-based services. The boilerplate includes core functionalities such as configuration management, database integration (with Prisma for ORM), middleware support, custom request handling, and module separation for different domains (like "users" and "profiles").

##### File Structure
- Top-Level Files/Folders:
- The root-level files (.env.example, package.json, tsconfig.json) and directories (.github) indicate standard configurations for a Node.js project.
- The main source code resides in the src directory.
- Core Folder:
- Contains the backbone of the framework including configuration (env.ts), database (with migrations and Prisma models), and a custom framework layer (controllers, middleware, dependency injection, etc.).
- Server-specific code is found under src/core/server, with Fastify specifically in src/core/server/fastify/fastify.service.ts.
- Modules Folder:
- Contains domain-specific functionality, for example, separate modules for "profiles" and "users". Each module includes controllers, DTOs, entities, interfaces, schemas, and use cases which suggests an adherence to separation of concerns and a modular design.
- Other Supportive Directories:
- Middlewares and repositories in core offer cross-cutting concerns like logging and abstracted database access.

##### Breakdown

a. Main Components to Include:
- HTTP Server (Fastify Service): Represented by the src/core/server/fastify elements, this is the entry point for handling HTTP requests.
- Application Layer (Controllers & Routes): Under each module (e.g., users and profiles), controllers handle incoming requests and pass control to underlying business logic.
- Business Logic Layer (Use Cases): Module-specific use case files implement the core business logic.
- Data Access Layer (Repositories & Prisma Service): Under src/core/database and src/core/repositories, these components deal with database interactions using Prisma.
- Core Framework & Configuration: Components in src/core/framework and src/core/config which provide dependency injection, middleware processing, configuration management, etc.
- Middlewares: Such as the log middleware which processes requests/responses.

b. Relationships and Interactions:
- Incoming HTTP requests enter through the Fastify server.
- Requests are routed to the corresponding controller within a module.
- Controllers invoke use cases (business logic) which then utilize repository services to interact with the database.
- Configuration, dependency injection, and middleware components ensure that components are loosely coupled and easily testable.

c. Architectural Patterns & Design Principles:
- Modular Design: Separation into modules for "users" and "profiles", promoting reusability and maintainability.
- Layered Architecture: Clear separation between the controller (presentation layer), business logic (use cases), and data access (repositories with Prisma).
- Dependency Injection & Inversion of Control: Provided by the custom framework (containers, inject.ts) to ensure components do not tightly depend on one another.
- Use of ORM (Prisma): Abstracts direct database calls and simplifies migrations and model definitions.

d. Key Technologies & Frameworks:
- Fastify: The underlying HTTP server framework.
- TypeScript: For static type checking and clearer code structure.
- Prisma: For ORM and database schema management.
- Custom framework components: For controllers, middleware, and dependency injection built within the src/core/framework directory.

##### Detailed and Clear Representation
- Component is separated and clearly labeled.
- Interactions, dependencies, and flows between all the major layers.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% HTTP Layer
    subgraph "HTTP Layer"
        A["HTTP Server (Fastify)"]:::http
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        M["Log Middleware"]:::middleware
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        UCtrl["Users Controller"]:::controller
        PCtrl["Profiles Controller"]:::controller
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        UUse["Users Use Case"]:::business
        PUse["Profiles Use Case"]:::business
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        PServ["Prisma Service"]:::data
        URepo["Users Repository"]:::data
        PRepo["Profiles Repository"]:::data
        DB["Database (Prisma)"]:::database
    end

    %% Core & Configuration
    subgraph "Core & Config"
        Env["Configuration (env)"]:::core
        Fw["Framework Utilities"]:::core
    end

    %% Connections
    %% Request flow: HTTP -> Middleware -> Controllers -> Use Cases -> Repositories -> Prisma -> Database
    A -->|"applies"| M
    M -->|"forwards"| UCtrl
    M -->|"forwards"| PCtrl
    UCtrl -->|"calls"| UUse
    PCtrl -->|"calls"| PUse
    UUse -->|"invokes"| URepo
    PUse -->|"invokes"| PRepo
    URepo -->|"queries"| PServ
    PRepo -->|"queries"| PServ
    PServ -->|"connects"| DB

    %% Core & Config influence on components
    Env -->|"configures"| A
    Fw -->|"injects into"| UCtrl
    Fw -->|"injects into"| PCtrl
    Fw -->|"injects into"| UUse
    Fw -->|"injects into"| PUse
    Fw -->|"injects into"| URepo
    Fw -->|"injects into"| PRepo

    %% Styles
    classDef http fill:#AED6F1,stroke:#2E86C1,stroke-width:2px;
    classDef controller fill:#A9DFBF,stroke:#27AE60,stroke-width:2px;
    classDef business fill:#F9E79F,stroke:#F4D03F,stroke-width:2px;
    classDef data fill:#D7BDE2,stroke:#8E44AD,stroke-width:2px;
    classDef core fill:#FADBD8,stroke:#E74C3C,stroke-width:2px;
    classDef middleware fill:#F5B7B1,stroke:#CD6155,stroke-width:2px;
    classDef database fill:#D1F2EB,stroke:#1ABC9C,stroke-width:2px;

    %% Click Events
    click A "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/server/fastify/fastify.service.ts"
    click UCtrl "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/modules/users/users.controller.ts"
    click PCtrl "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/modules/profiles/profiles.controller.ts"
    click UUse "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/modules/users/users.usercase.ts"
    click PUse "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/modules/profiles/profiles.usecase.ts"
    click PServ "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/database/prisma/prisma.service.ts"
    click URepo "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/repositories/prisma/users.repository.ts"
    click PRepo "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/repositories/prisma/profiles.repository.ts"
    click Env "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/config/env.ts"
    click Fw "https://github.com/rcasachi/fastify-ts-boilerplate/tree/main/src/core/framework/"
    click M "https://github.com/rcasachi/fastify-ts-boilerplate/blob/main/src/core/middlewares/log.middleware.ts"
```
