# leashmt/db-management architecture

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

- Interactive diagram: https://gitdiagram.com/leashmt/db-management
- Repository: https://github.com/leashmt/db-management

## Overview

##### Main Components

A. Client Layer
- External clients/applications that consume the REST API
- Supported HTTP methods (GET, POST, PUT, DELETE)

B. API Layer
- Express.js server (src/app.js as main entry point)
- Route handlers (organized in src/routes/)
- Different endpoints:
- Complex routes (from complexRoutes.js)
- CRUD operations (from get.js, getAll.js, post.js, put.js, delete.js)

C. Business Logic Layer
- Data validation (Joi)
- Business rules and operations

D. Data Access Layer
- MySQL database connection
- SQL scripts (from src/scripts/)
- Initialization scripts (init.sql)
- Data population scripts (add.sql)

E. Infrastructure Layer
- Environment configuration (.env)
- Database server

**[External Clients] (Top)**
- ↓
**[API Gateway/Express Server]**
- ↓
**[Route Handlers]**
- ↓
**[Business Logic]**
- ↓
**[Data Access Layer]**
- ↓
**[MySQL Database] (Bottom)**

##### Specific Elements

A. API Routes Section:
- Routes by functionality:
- Product management
- Order management
- Supplier management
- HTTP methods supported by each route

B. Database Section:
- Main database tables (Products, Orders, Suppliers)
- Relationships between tables
- Initialization and data population scripts

C. Configuration Section:
- Environment variables management
- Server configuration

##### Additional Details

- A section showing the npm scripts workflow:
- init → add → start sequence
- order and admin scripts interaction
- stats generation process

##### The relationship between different components

This system design should provide a clear understanding of how the application handles requests from initial HTTP call through to database operations and back, while showing the supporting infrastructure and configuration components.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        C1[External Clients]:::client
    end

    subgraph "API Gateway"
        API[Express Server]:::api
        click API "https://github.com/leashmt/db-management/blob/main/src/app.js"
    end

    subgraph "Route Handlers"
        direction TB
        RH[Route Handler Layer]:::route
        click RH "https://github.com/leashmt/db-management/tree/main/src/routes/"
        
        subgraph "CRUD Operations"
            direction TB
            POST[Create Routes]:::route
            GET[Read Routes]:::route
            PUT[Update Routes]:::route
            DEL[Delete Routes]:::route
            click POST "https://github.com/leashmt/db-management/blob/main/src/routes/post.js"
            click GET "https://github.com/leashmt/db-management/blob/main/src/routes/get.js"
            click PUT "https://github.com/leashmt/db-management/blob/main/src/routes/put.js"
            click DEL "https://github.com/leashmt/db-management/blob/main/src/routes/delete.js"
        end
        
        COMPLEX[Complex Routes]:::route
        click COMPLEX "https://github.com/leashmt/db-management/blob/main/src/routes/complexRoutes.js"
    end

    subgraph "Business Logic"
        BL[Data Validation & Business Rules]:::logic
    end

    subgraph "Data Access Layer"
        DAL[Database Operations]:::data
        
        subgraph "Database Scripts"
            INIT[Initialization Scripts]:::script
            ADD[Data Population Scripts]:::script
            click INIT "https://github.com/leashmt/db-management/blob/main/src/scripts/init.sql"
            click ADD "https://github.com/leashmt/db-management/blob/main/src/scripts/add.sql"
        end
        
        INIT_HANDLER[Init Handler]:::handler
        ADD_HANDLER[Add Handler]:::handler
        click INIT_HANDLER "https://github.com/leashmt/db-management/blob/main/src/scripts/init.js"
        click ADD_HANDLER "https://github.com/leashmt/db-management/blob/main/src/scripts/add.js"
    end

    subgraph "Infrastructure"
        DB[(MySQL Database)]:::database
        ENV[Environment Config]:::config
        SERVER[Server]:::server
        click SERVER "https://github.com/leashmt/db-management/blob/main/src/start.js"
    end

    C1 --> API
    API --> RH
    RH --> POST & GET & PUT & DEL & COMPLEX
    POST & GET & PUT & DEL & COMPLEX --> BL
    BL --> DAL
    DAL --> INIT & ADD
    DAL --> INIT_HANDLER & ADD_HANDLER
    INIT_HANDLER & ADD_HANDLER --> DB
    ENV -.- API
    ENV -.- DB

    classDef client fill:#2374ab,stroke:#000,stroke-width:2px,color:#fff
    classDef api fill:#48a9a6,stroke:#000,stroke-width:2px,color:#fff
    classDef route fill:#4ad66d,stroke:#000,stroke-width:2px,color:#000
    classDef logic fill:#e4cc37,stroke:#000,stroke-width:2px,color:#000
    classDef data fill:#d96a4f,stroke:#000,stroke-width:2px,color:#fff
    classDef script fill:#8b687f,stroke:#000,stroke-width:2px,color:#fff
    classDef handler fill:#7b435b,stroke:#000,stroke-width:2px,color:#fff
    classDef database fill:#9c6644,stroke:#000,stroke-width:2px,color:#fff
    classDef config fill:#d1495b,stroke:#000,stroke-width:2px,color:#fff
    classDef server fill:#234d5b,stroke:#000,stroke-width:2px,color:#fff

    subgraph Legend
        L1[Client Components]:::client
        L2[API Components]:::api
        L3[Route Components]:::route
        L4[Logic Components]:::logic
        L5[Data Components]:::data
        L6[Database]:::database
        L7[Infrastructure]:::server
    end
```
