# edvan-lima/nlw-connect-node architecture

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

- Interactive diagram: https://gitdiagram.com/edvan-lima/nlw-connect-node
- Repository: https://github.com/edvan-lima/nlw-connect-node

## Overview

##### Main Components
- API Server: Represented by the server initialization code in src/server.ts. This is the entry point where Fastify is configured.
- Environment Configuration: The src/env.ts file handles environment variable validation. Include the .env configuration (and .env.example) as part of the configuration module.
- Routing Layer: The src/routes directory contains endpoints that map incoming requests to their respective route-handling functions.
- Business Logic / Functions: The src/functions directory holds the actual processing logic (e.g., subscribe-to-event, get-ranking, etc.) that is being invoked by the routes.
- Redis Cache Module: The src/redis directory contains Redis client configuration and integration, indicating a cache layer.
- Auxiliary Components: Documentation (docs) and build/configuration files (docker-compose, tsup.config.ts, etc.) support deployment, development, and testing.

##### Relationships and Interactions
- Data Flow: Illustrate the flow from client requests hitting the Fastify API server. Show that the request goes through route handlers (from the routes directory) which then invoke functions (business logic).
- Database Interaction: Show that these business logic functions utilize Drizzle ORM for database operations. Mark the PostgreSQL database as a component that interacts with the ORM layer.
- Cache Interaction: Optionally, demonstrate how cached data is managed by the Redis module to optimize request handling.
- Environment and Configuration Management: Demonstrate that the server, routes, and ORM components all rely on shared environment configuration (validated in src/env.ts).

##### Architectural Patterns and Design Principles
- Clearly separate concerns: the project isolates API routing, business logic, and data access into distinct modules.
- Mention the use of Fastify’s modular architecture, which promotes separation of concerns and scalable route handling.
- That the project uses TypeScript, which enforces type safety across the application layers.

##### Including Relevant Technologies and Libraries
- Fastify as the web framework for handling HTTP requests.
- Zod for schema validation (especially for environment variables and request payloads).
- Drizzle ORM for database interaction.
- PostgreSQL, which is the primary data store (along with migrations and schema definitions managed in src/drizzle).
- @fastify/jwt for authentication (if applicable based on the dependencies).
- Redis, which is used optionally as a caching layer.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    ExternalClient["External Client"]:::external

    %% Configuration Subgraph
    subgraph "Configuration"
        ENV_CONFIG["Environment Config (src/env.ts)"]:::config
        ENV_FILE["Env File (.env)"]:::config
        ENV_EXAMPLE["Env Example (.env.example)"]:::config
    end

    %% API Server Subgraph
    subgraph "API Layer"
        APIServer["API Server / Fastify (src/server.ts)"]:::api
    end

    %% Routing Layer Subgraph
    subgraph "Routing Layer"
        AccessInviteRoute["Access Invite Route (src/routes/access-invite-link-route.ts)"]:::routes
        GetRankingRoute["Get Ranking Route (src/routes/get-ranking-route.ts)"]:::routes
    end

    %% Business Logic Subgraph
    subgraph "Business Logic / Functions"
        SubscribeEvent["Subscribe-to-Event (src/functions/subscribe-to-event.ts)"]:::business
        GetRanking["Get Ranking (src/functions/get-ranking.ts)"]:::business
    end

    %% Database Layer Subgraph
    subgraph "Database Layer"
        DrizzleClient["Drizzle ORM Client (src/drizzle/client.ts)"]:::db
        Migrations["Migrations (src/drizzle/migrations)"]:::db
        Schema["Schema (src/drizzle/schema)"]:::db
        PostgresDB["PostgreSQL Database"]:::db
    end

    %% Redis Cache Subgraph
    subgraph "Cache Layer"
        RedisClient["Redis Client (src/redis/client.ts)"]:::redis
    end

    %% Auxiliary Components Subgraph
    subgraph "Auxiliary Components"
        DOCDocker["DOCKER.md (docs/DOCKER.md)"]:::aux
        DOCDrizzle["DRIZZLE.md (docs/DRIZZLE.md)"]:::aux
        DOCNodejs["NODEJS.md (docs/NODEJS.md)"]:::aux
        DockerCompose["docker-compose.yml"]:::aux
        TsupConfig["tsup.config.ts"]:::aux
    end

    %% Data Flow Connections
    ExternalClient -->|"HTTP"| APIServer
    ENV_CONFIG --- APIServer
    ENV_FILE --- APIServer
    ENV_EXAMPLE --- APIServer

    APIServer -->|"routes"| AccessInviteRoute
    APIServer -->|"routes"| GetRankingRoute

    AccessInviteRoute -->|"calls"| SubscribeEvent
    GetRankingRoute -->|"calls"| GetRanking

    SubscribeEvent -->|"ORM"| DrizzleClient
    SubscribeEvent -->|"cache"| RedisClient
    GetRanking -->|"ORM"| DrizzleClient
    GetRanking -->|"cache"| RedisClient

    DrizzleClient --> Migrations
    DrizzleClient --> Schema
    DrizzleClient -->|"DB operations"| PostgresDB

    ENV_CONFIG --- SubscribeEvent
    ENV_CONFIG --- GetRanking
    ENV_CONFIG --- DrizzleClient

    %% Click Events
    click APIServer "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/server.ts"
    click ENV_CONFIG "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/env.ts"
    click ENV_FILE "https://github.com/edvan-lima/nlw-connect-node/blob/main/.env"
    click ENV_EXAMPLE "https://github.com/edvan-lima/nlw-connect-node/blob/main/.env.example"
    click AccessInviteRoute "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/routes/access-invite-link-route.ts"
    click GetRankingRoute "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/routes/get-ranking-route.ts"
    click SubscribeEvent "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/functions/subscribe-to-event.ts"
    click GetRanking "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/functions/get-ranking.ts"
    click DrizzleClient "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/drizzle/client.ts"
    click Migrations "https://github.com/edvan-lima/nlw-connect-node/tree/main/src/drizzle/migrations"
    click Schema "https://github.com/edvan-lima/nlw-connect-node/tree/main/src/drizzle/schema"
    click RedisClient "https://github.com/edvan-lima/nlw-connect-node/blob/main/src/redis/client.ts"
    click DOCDocker "https://github.com/edvan-lima/nlw-connect-node/blob/main/docs/DOCKER.md"
    click DOCDrizzle "https://github.com/edvan-lima/nlw-connect-node/blob/main/docs/DRIZZLE.md"
    click DOCNodejs "https://github.com/edvan-lima/nlw-connect-node/blob/main/docs/NODEJS.md"
    click DockerCompose "https://github.com/edvan-lima/nlw-connect-node/blob/main/docker-compose.yml"
    click TsupConfig "https://github.com/edvan-lima/nlw-connect-node/blob/main/tsup.config.ts"

    %% Styles
    classDef external fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef config fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef api fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
    classDef routes fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef business fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef db fill:#fde2e2,stroke:#a71d2a,stroke-width:2px;
    classDef redis fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
    classDef aux fill:#e0f7fa,stroke:#006064,stroke-width:2px;
```
