# dreamographer/zensync-backend architecture

> Architecture diagram of the GitHub repository dreamographer/zensync-backend, 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/dreamographer/zensync-backend
- Repository: https://github.com/dreamographer/zensync-backend

## Overview

##### Main Components
- Presentation Layer: This encompasses the controllers, routes, middleware, validators, and public assets found under src/presentation. It is responsible for handling incoming HTTP requests (or possibly web socket connections, as indicated by middleware named socketIo and socketService) and returning appropriate responses.
- Service Layer: Found in src/services, this layer contains business logic (e.g., fileService, folderService, userService, workspaceService, and socketService) which orchestrates domain logic and coordinates operations.
- Database Layer: Divided into several parts:
- Database Connections and Models: src/database/connection.ts and subdirectories for models (File, Folder, User, Workspace).
- Repositories: The repository pattern is applied here (e.g., UserRepository, fileRepository, folderRepository, workspaceRepository) to abstract and encapsulate all database access.
- Domain Entities: These are defined under src/entities. They represent the core data objects used throughout the application.
- External Libraries & Integrations: Under src/external-libraries, you have token management, bcrypt for encryption, mailer for email services, and passport strategies for GitHub and Google authentication. These provide specialized functionality and integrate with third-party services.

##### Relationships and Interactions
- Requests are initially handled by the Presentation Layer—routes direct requests to controllers.
- Controllers then call appropriate Service Layer methods where business rules are enforced.
- Service methods make use of repository interfaces (declared in src/interfaces) to interact with the database, abstracting the data persistence logic.
- The external libraries are used by the services or the controllers (for example, authentication with Passport, token generation, encryption with Bcrypt, and sending emails with Mailer) to handle specific functionalities.
- The database models and entities ensure that the application domain is strongly defined, and the database connection encapsulated in src/database/connection.ts supports data operations.

##### Key Architectural Patterns & Principles
- Layered/Modular Architecture: Clear segregation between presentation, business logic, data access, and external integrations.
- Repository Pattern: Used in the database layer to abstract data persistence.
- Dependency Injection (implicit via interface definitions in src/interfaces) that allows for flexibility and testability.
- Integration of external libraries for authentication, encryption, and mailing emphasizes extensibility.

##### Instructing the Principal Software Engineer on Diagramming
- Presentation Layer (Controllers, Routes, Middleware, Validators)
- Service Layer (e.g., File Service, Folder Service, User Service, Workspace Service, Socket Service)
- Database Layer, separated into:
▪ Database Connection
▪ Models and Entities (File, Folder, User, Workspace)
▪ Repositories (UserRepository, fileRepository…)
- External Libraries (Token, Bcrypt, Mailer, Passport Strategies)
- Infrastructure/DevOps (Docker, GitHub Actions for CI/CD)
- From incoming requests to the Controllers and then to Services.
- From Services to the Repositories, showing dependency for data persistence.
- From Services to External Libraries where required.
- To capture the error handling mechanisms and middleware chains (like errorHandler and request/validation middleware) which indicate how the system deals with runtime issues and input validation.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Presentation Layer
    subgraph "Presentation Layer"
        PL["Presentation (Controllers,Routes,Middleware,Validators,Public Assets)"]:::presentation
    end

    %% Service Layer
    subgraph "Service Layer"
        FileService["File Service"]:::service
        FolderService["Folder Service"]:::service
        UserService["User Service"]:::service
        WorkspaceService["Workspace Service"]:::service
        SocketService["Socket Service"]:::service
    end

    %% Domain Entities
    subgraph "Domain Entities"
        FileEntity["File Entity"]:::entity
        FolderEntity["Folder Entity"]:::entity
        UserEntity["User Entity"]:::entity
        WorkspaceEntity["Workspace Entity"]:::entity
    end

    %% Database Layer
    subgraph "Database Layer"
        DBConn["Database Connection"]:::database
        subgraph "Models"
            FileModel["File Model"]:::database
            FolderModel["Folder Model"]:::database
            UserModel["User Model"]:::database
            WorkspaceModel["Workspace Model"]:::database
        end
        subgraph "Repositories"
            UserRepo["User Repository"]:::repository
            FileRepo["File Repository"]:::repository
            FolderRepo["Folder Repository"]:::repository
            WorkspaceRepo["Workspace Repository"]:::repository
        end
    end

    %% External Libraries & Integrations
    subgraph "External Libraries"
        TokenMgmt["Token Management"]:::external
        Bcrypt["Bcrypt"]:::external
        Mailer["Mailer"]:::external
        PassportGitHub["Passport-GitHub"]:::external
        PassportGoogle["Passport-Google"]:::external
    end

    %% Infrastructure/DevOps
    subgraph "Infrastructure/DevOps"
        DockerConfig["Docker Config"]:::devops
        CICD["CI/CD Pipeline"]:::devops
    end

    %% Connections from Presentation Layer to Service Layer
    PL -->|"handles"| FileService
    PL -->|"handles"| FolderService
    PL -->|"handles"| UserService
    PL -->|"handles"| WorkspaceService
    PL -->|"handles"| SocketService

    %% Service Layer to Database Repositories (DB Calls)
    UserService -->|"db"| UserRepo
    FileService -->|"db"| FileRepo
    FolderService -->|"db"| FolderRepo
    WorkspaceService -->|"db"| WorkspaceRepo

    %% Service Layer to External Libraries
    UserService -->|"encrypt"| Bcrypt
    UserService -->|"token"| TokenMgmt
    UserService -->|"auth"| PassportGitHub
    UserService -->|"auth"| PassportGoogle
    WorkspaceService -->|"email"| Mailer

    %% Repositories use Database Connection
    UserRepo -->|"connects"| DBConn
    FileRepo -->|"connects"| DBConn
    FolderRepo -->|"connects"| DBConn
    WorkspaceRepo -->|"connects"| DBConn

    %% Repositories map to Database Models
    UserRepo -->|"maps"| UserModel
    FileRepo -->|"maps"| FileModel
    FolderRepo -->|"maps"| FolderModel
    WorkspaceRepo -->|"maps"| WorkspaceModel

    %% Database Models represent Domain Entities
    UserModel -->|"represents"| UserEntity
    FileModel -->|"represents"| FileEntity
    FolderModel -->|"represents"| FolderEntity
    WorkspaceModel -->|"represents"| WorkspaceEntity

    %% Service Layer Deployment to Infrastructure
    SocketService -->|"deployed"| DockerConfig
    DockerConfig -->|"build"| CICD

    %% Class Definitions (Styles)
    classDef presentation fill:#bbdefb,stroke:#0d47a1,stroke-width:2px;
    classDef service fill:#c8e6c9,stroke:#1b5e20,stroke-width:2px;
    classDef repository fill:#ffe0b2,stroke:#e65100,stroke-width:2px;
    classDef database fill:#fff9c4,stroke:#bf360c,stroke-width:2px;
    classDef external fill:#e1bee7,stroke:#6a1b9a,stroke-width:2px;
    classDef entity fill:#f8bbd0,stroke:#880e4f,stroke-width:2px;
    classDef devops fill:#cfd8dc,stroke:#37474f,stroke-width:2px;

    %% Click Events
    click PL "https://github.com/dreamographer/zensync-backend/tree/main/src/presentation"
    click FileService "https://github.com/dreamographer/zensync-backend/blob/main/src/services/fileService.ts"
    click FolderService "https://github.com/dreamographer/zensync-backend/blob/main/src/services/folderService.ts"
    click UserService "https://github.com/dreamographer/zensync-backend/blob/main/src/services/userService.ts"
    click WorkspaceService "https://github.com/dreamographer/zensync-backend/blob/main/src/services/workspaceService.ts"
    click SocketService "https://github.com/dreamographer/zensync-backend/blob/main/src/services/socketService.ts"
    click DBConn "https://github.com/dreamographer/zensync-backend/blob/main/src/database/connection.ts"
    click FileModel "https://github.com/dreamographer/zensync-backend/blob/main/src/database/models/File.ts"
    click FolderModel "https://github.com/dreamographer/zensync-backend/blob/main/src/database/models/Folder.ts"
    click UserModel "https://github.com/dreamographer/zensync-backend/blob/main/src/database/models/User.ts"
    click WorkspaceModel "https://github.com/dreamographer/zensync-backend/blob/main/src/database/models/workspace.ts"
    click UserRepo "https://github.com/dreamographer/zensync-backend/blob/main/src/database/repository/UserRepository.ts"
    click FileRepo "https://github.com/dreamographer/zensync-backend/blob/main/src/database/repository/fileRepository.ts"
    click FolderRepo "https://github.com/dreamographer/zensync-backend/blob/main/src/database/repository/folderRepository.ts"
    click WorkspaceRepo "https://github.com/dreamographer/zensync-backend/blob/main/src/database/repository/workspaceRepository.ts"
    click FileEntity "https://github.com/dreamographer/zensync-backend/blob/main/src/entities/File.ts"
    click FolderEntity "https://github.com/dreamographer/zensync-backend/blob/main/src/entities/Folder.ts"
    click UserEntity "https://github.com/dreamographer/zensync-backend/blob/main/src/entities/User.ts"
    click WorkspaceEntity "https://github.com/dreamographer/zensync-backend/blob/main/src/entities/Workspace.ts"
    click TokenMgmt "https://github.com/dreamographer/zensync-backend/blob/main/src/external-libraries/Token.ts"
    click Bcrypt "https://github.com/dreamographer/zensync-backend/blob/main/src/external-libraries/bcrypt.ts"
    click Mailer "https://github.com/dreamographer/zensync-backend/blob/main/src/external-libraries/mailer.ts"
    click PassportGitHub "https://github.com/dreamographer/zensync-backend/blob/main/src/external-libraries/passport-github-auth.ts"
    click PassportGoogle "https://github.com/dreamographer/zensync-backend/blob/main/src/external-libraries/passport-google-auth.ts"
    click DockerConfig "https://github.com/dreamographer/zensync-backend/tree/main/dockerfile"
    click CICD "https://github.com/dreamographer/zensync-backend/blob/main/.github/workflows/cicd.yml"
```
