# viniciuscestarii/modular-pets architecture

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

- Interactive diagram: https://gitdiagram.com/viniciuscestarii/modular-pets
- Repository: https://github.com/viniciuscestarii/modular-pets

## Overview

##### High-Level Architecture Overview

a) Core System Components:
- API Layer (Express/Node.js with Bun runtime)
- Database Layer (SQL database, indicated by Drizzle ORM usage)
- Module Layer (containing all business modules)

##### Module Organization

Create distinct sections for each major module, following the modular architecture visible in the source code:

a) Pet Management Module:
- Pets
- Species
- Breeds
- Relationships between these entities

b) Health Module:
- Patients
- Medical Records

##### Detailed Component Structure

For each module, represent the following layers (as seen in the file structure):
- Controllers (API endpoints)
- Use Cases (business logic)
- Repositories (data access)
- Entities (domain models)
- Schemas (data validation)

##### Design Elements

Use the following visual elements:

- Rectangles: Modules
- Hexagons: External Services
- Rounded Rectangles: Controllers/APIs

- Blue: Core System Components
- Green: Pet Module Components
- Orange: Health Module Components
- Gray: Shared Utilities
- Purple: Database Layer

##### Specific Implementation Details

Key architectural patterns visible in the codebase:

a) Repository Pattern:
- Both DrizzleRepository and InMemoryRepository implementations
- Interface segregation

b) Factory Pattern:
- Demonstrate the creation flow using factories

c) Error Handling:
- Error handling components and custom errors

##### Infrastructure Components

Infrastructure elements:
- Docker container
- Environment configuration
- Database migrations (Drizzle)
- Testing infrastructure

**[Frontend Clients] (Future)**
- ↓
**[API Gateway Layer]**
- ↓
**[Module Layer]**
- [Pet Module]
- Pets
- Species
- Breeds

- [Health Module]
- Patients
- Medical Records

- [Shared Utilities]
- Schema Validation
- Event Emitter
- Telemetry

**[Database Layer]**
- [SQL Database]
- Drizzle ORM

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "API Gateway Layer"
        API[API Gateway]
    end

    subgraph "Module Layer"
        subgraph "Pet Management Module"
            PM["Pet Module"]:::module
            click PM "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/pet/"
            
            Pets["Pets"]:::component
            click Pets "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/pet/pets/"
            Species["Species"]:::component
            click Species "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/pet/species/"
            Breeds["Breeds"]:::component
            click Breeds "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/pet/breeds/"
            
            PM --> Pets
            PM --> Species
            PM --> Breeds
        end

        subgraph "Health Module"
            HM["Health Module"]:::module
            click HM "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/health/"
            
            Patients["Patients"]:::component
            click Patients "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/health/patients/"
            MedRecords["Medical Records"]:::component
            
            HM --> Patients
            HM --> MedRecords
        end

        subgraph "Shared Utilities"
            Schema["Schema Validation"]:::utility
            click Schema "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/modules/shared/schema/"
            Events["Event Emitter"]:::utility
            click Events "https://github.com/ViniciusCestarii/modular-pets/blob/main/src/modules/shared/utilities/events/event-emmiter.ts"
            Telemetry["Telemetry"]:::utility
            click Telemetry "https://github.com/ViniciusCestarii/modular-pets/blob/main/src/modules/shared/utilities/telemetry.ts"
        end
    end

    subgraph "Database Layer"
        DB[(SQL Database)]:::database
        click DB "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/db/"
        
        subgraph "Repository Implementations"
            DR["Drizzle Repositories"]:::repository
            IMR["In-Memory Repositories"]:::repository
            
            click DR "https://github.com/ViniciusCestarii/modular-pets/blob/main/src/modules/pet/pets/repositories/drizzle-repository.ts"
            click IMR "https://github.com/ViniciusCestarii/modular-pets/blob/main/src/modules/pet/pets/repositories/in-memory-repository.ts"
        end
        
        Migrations["Database Migrations"]:::infra
        click Migrations "https://github.com/ViniciusCestarii/modular-pets/tree/main/drizzle/"
    end

    subgraph "Infrastructure"
        Docker["Docker Container"]:::infra
        click Docker "https://github.com/ViniciusCestarii/modular-pets/tree/main/Dockerfile"
        EnvConfig["Environment Config"]:::infra
        click EnvConfig "https://github.com/ViniciusCestarii/modular-pets/tree/main/src/env/"
    end

    %% Connections
    API --> PM
    API --> HM
    
    PM -.-> Schema
    PM -.-> Events
    PM -.-> Telemetry
    
    HM -.-> Schema
    HM -.-> Events
    HM -.-> Telemetry
    
    Pets --> DR
    Species --> DR
    Breeds --> DR
    Patients --> DR
    
    Pets --> IMR
    Species --> IMR
    Breeds --> IMR
    Patients --> IMR
    
    DR --> DB
    Migrations --> DB

    %% Styles
    classDef module fill:#2ecc71,stroke:#27ae60,stroke-width:2px
    classDef component fill:#3498db,stroke:#2980b9,stroke-width:2px
    classDef utility fill:#95a5a6,stroke:#7f8c8d,stroke-width:2px
    classDef database fill:#9b59b6,stroke:#8e44ad,stroke-width:2px
    classDef repository fill:#e67e22,stroke:#d35400,stroke-width:2px
    classDef infra fill:#34495e,stroke:#2c3e50,stroke-width:2px

    %% Legend
    subgraph Legend
        M1["Module"]:::module
        C1["Component"]:::component
        U1["Utility"]:::utility
        D1["Database"]:::database
        R1["Repository"]:::repository
        I1["Infrastructure"]:::infra
    end
```
