# gnauqoa/bee-farm-be architecture

> Architecture diagram of the GitHub repository gnauqoa/bee-farm-be, 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/gnauqoa/bee-farm-be
- Repository: https://github.com/gnauqoa/bee-farm-be

## Overview

##### Project Type & Purpose
- This backend is part of an IoT full-stack application for beekeeping farm management. It is built using NestJS and employs a REST API approach with @nestjsx/crud, TypeORM for data management with PostgreSQL, and incorporates a range of features from real-time monitoring (via sensor data) to actuator controls.

##### Analyzing the File Structure
- The directory tree shows a well-organized NestJS project split into multiple modules. Key modules include:
- "src/auth": managing user authentication, email-based login, password resets, etc.
- "src/users": handling user operations, CRUD services and persistence with relational databases.
- "src/devices": managing device-related logic (likely for sensor/actuator data), along with guards and domain logic.
- "src/database": configuration, migrations, and seeds for setting up PostgreSQL (all handled via TypeORM).
- "src/files": handling file uploads using different infrastructure strategies (local, AWS S3, etc.).
- "src/mail" and "src/mailer": modules for sending activation, password reset emails, etc.
- Additional modules for i18n, socket-io (for real-time communications), MQTT (for IoT messaging), roles & sessions management.
- The presence of multiple “hygen” directories indicates that code generation templates are used to streamline creating new resources (e.g., for CRUD operations with relational persistence).

a. Main Components:
- API Gateway / HTTP Layer: The NestJS REST API endpoint (controllers, middleware, interceptors).
- Domain/Business Logic: Modules for auth, users, devices, files, mail, sessions, roles, etc.
- Database Layer: PostgreSQL database accessed via TypeORM (including migrations and seeders).
- Real-Time Communication: Socket.io gateway and MQTT service for sensor updates and actuator commands.
- External Services & Integrations: Mail service (via nodemailer), file upload systems (local and AWS S3), and the external hardware repositories (gateway board & driver board).

b. Relationships and Interactions:
- Frontend (not in this repo but referenced) communicates via REST APIs to the NestJS backend.
- Devices/sensors send real-time data to the backend via MQTT and/or web sockets.
- The Backend communicates with PostgreSQL for persistence and uses TypeORM, with migrations and seeders ensuring data integrity.
- Authentication flows use dedicated modules (auth/users) and integrate email services to manage sign in, sign up, and password resets.
- Inter-module communications happen through service injections and shared domain models.

c. Architectural Patterns & Design Principles:
- Modularization: Each domain is isolated into its own module.
- Layered Architecture: Clear separation of controllers (presentation), services (business logic), and repositories (data access).
- Code Generation: Use of hygen templates for consistent and scalable CRUD operations.
- Containerization & Deployment: Docker and Docker Compose for environment consistency.
- Real-Time Data Flow: Integration of socket.io and MQTT for asynchronous, real-time updates.

d. Technologies, Frameworks, and Libraries:
- NestJS as the core framework.
- TypeORM for database interactions.
- @nestjs/config for environment configuration.
- nodemailer for email functionalities.
- nestjs-i18n for internationalization.
- Swagger for API documentation.
- Various Docker configurations for different deployment environments.
- Socket.io and MQTT for real-time communications.
- Testing frameworks (E2E and unit tests) and linting (eslint, prettierrc).

##### Final Assembly
- Drill down into the backend structure by decomposing it into logical modules (auth, users, devices, etc.), highlighting persistence layers and inter-module communications.
- Be detailed in showing how repositories, services, controllers, and additional support utilities (e.g., internationalization, file uploading) work together.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Clients
    subgraph "External Clients"
        FE("Frontend Client"):::external
        IoT("IoT Devices(Sensors/Actuators)"):::external
        HW("Hardware Integration"):::external
    end

    %% NestJS Backend
    subgraph "NestJS Backend"
        APIG("API Gateway/HTTP Layer"):::nestjs
        subgraph "Modules"
            Auth("Auth Module"):::nestjs
            Users("Users Module"):::nestjs
            Devices("Devices Module"):::nestjs
            Files("Files Module"):::nestjs
            MailM("Mail Module"):::nestjs
            Mailer("Mailer Module"):::nestjs
            Roles("Roles Module"):::nestjs
            Sessions("Sessions Module"):::nestjs
        end
        Socket("Socket.io Communication"):::realtime
        MQTT("MQTT Service"):::realtime
    end

    %% Database and Deployment
    DB("PostgreSQL Database"):::database
    Docker("Docker & Deployments"):::container

    %% Connections from External Clients to Backend
    FE -->|"REST_API"| APIG
    HW -->|"Integration"| APIG
    IoT -->|"MQTT"| MQTT
    IoT -->|"WebSocket"| Socket

    %% API Gateway routes to Modules
    APIG -->|"routes_to"| Auth
    APIG -->|"routes_to"| Users
    APIG -->|"routes_to"| Devices
    APIG -->|"routes_to"| Files
    APIG -->|"routes_to"| MailM
    APIG -->|"routes_to"| Mailer
    APIG -->|"routes_to"| Roles
    APIG -->|"routes_to"| Sessions

    %% Modules interactions with Database
    Auth -->|"uses_DB"| DB
    Users -->|"uses_DB"| DB
    Devices -->|"uses_DB"| DB
    Files -->|"uses_DB"| DB
    MailM -->|"uses_DB"| DB
    Mailer -->|"uses_DB"| DB
    Roles -->|"uses_DB"| DB
    Sessions -->|"uses_DB"| DB

    %% Real-Time communications
    MQTT -->|"real_time_data"| Devices
    Socket -->|"notifications"| Auth

    %% Deployment containerization links
    APIG --- Docker
    Socket --- Docker
    MQTT --- Docker
    DB --- Docker

    
    %% Click Events
    click APIG "https://github.com/gnauqoa/bee-farm-be/blob/main/src/main.ts"
    click APIG "https://github.com/gnauqoa/bee-farm-be/blob/main/src/app.module.ts"
    click Auth "https://github.com/gnauqoa/bee-farm-be/tree/main/src/auth"
    click Users "https://github.com/gnauqoa/bee-farm-be/tree/main/src/users"
    click Devices "https://github.com/gnauqoa/bee-farm-be/tree/main/src/devices"
    click DB "https://github.com/gnauqoa/bee-farm-be/tree/main/src/database"
    click Files "https://github.com/gnauqoa/bee-farm-be/tree/main/src/files"
    click MailM "https://github.com/gnauqoa/bee-farm-be/tree/main/src/mail"
    click Mailer "https://github.com/gnauqoa/bee-farm-be/tree/main/src/mailer"
    click Socket "https://github.com/gnauqoa/bee-farm-be/tree/main/src/socket-io"
    click MQTT "https://github.com/gnauqoa/bee-farm-be/tree/main/src/mqtt"
    click Roles "https://github.com/gnauqoa/bee-farm-be/tree/main/src/roles"
    click Sessions "https://github.com/gnauqoa/bee-farm-be/tree/main/src/session"

    %% Styles for classes
    classDef nestjs fill:#cce5ff,stroke:#1f78b4,stroke-width:2px;
    classDef database fill:#d0f0c0,stroke:#2ca02c,stroke-width:2px;
    classDef external fill:#fdfd96,stroke:#f1c40f,stroke-width:2px;
    classDef realtime fill:#ffb347,stroke:#ff7f0e,stroke-width:2px;
    classDef container fill:#e6e6fa,stroke:#d62728,stroke-width:2px;
```
