# masatotezuka/clean-architecture-sample-app architecture

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

- Interactive diagram: https://gitdiagram.com/masatotezuka/clean-architecture-sample-app
- Repository: https://github.com/masatotezuka/clean-architecture-sample-app

## Overview

##### Main Components
- Docker Environment: Represent the Docker infrastructure elements such as the Dockerfile, docker-compose.yaml, and the dedicated Docker folder for MySQL initialization (Docker/db/init.sql). This indicates that the application is containerized and runs in a set-up that includes a MySQL database.
- Express Server Application: The core backend server is built using Express. The entry point (src/index.ts) will be the starting point where the server is initialized.
- Application Layers (Clean Architecture):
a. Controllers (src/controller): These handle the incoming HTTP requests and coordinate input validations and responses.
b. Use Cases (src/use-case): This layer encapsulates the business logic. Files like create-user.ts, delete-user.ts, get-users.ts, update-user.ts belong here.
c. Domain (src/domain): This contains domain entities (e.g., user.entity.ts) representing the core business model.
d. Repositories (src/repositories): This handles data persistence (e.g., user.repository.ts) where the business data is interfaced with the database through Prisma.
e. Middlewares (src/middleware): Includes error handling and request validation functionalities.
- External Services:
a. Database (MySQL): Managed by Docker and configured through Prisma (prisma/schema.prisma and prisma/index.ts).
b. Orchestration / Dependency Injection: While not emphasized heavily, tools such as tsyringe are mentioned.

##### Relationships and Data Flow
- Incoming HTTP requests are received by the Express controllers.
- The controllers pass the request data to the appropriate use case, which represents the application’s business rules.
- The use cases interact with repositories to manage data, where Prisma serves as the ORM interfacing with MySQL.
- Middleware (such as input validation and error handling) intercepts requests and responses, ensuring data integrity and robust error management.
- Docker and Docker Compose set up the environment by orchestrating both the application service and the MySQL database container.

##### Architectural Patterns & Design Principles
- Clean (Onion) Architecture: Demonstrates a clear separation between different layers (controllers, use cases, domain, and repositories).
- Dependency Injection: Indicated by the use of tsyringe, which enforces loose coupling.
- Modular Design: Separation of concerns across distinct directories (e.g., src/domain vs. src/use-case).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Docker Compose Environment
    subgraph "Docker Compose Environment"
        DC1("Dockerfile"):::docker
        DC2("docker-compose.yaml"):::docker
        DC3("Docker/db/init.sql"):::docker

        subgraph "Containers"
            %% Application Container
            subgraph "Application Container"
                entry("Express Server (src/index.ts)"):::app
                subgraph "Application Layers"
                    ctrl("Controller (src/controller/index.ts)"):::controller
                    mid("Middleware (src/middleware)"):::middleware
                    use("Use Cases (src/use-case)"):::usecase
                    dom("Domain Entities (src/domain)"):::domain
                    rep("Repositories (src/repositories)"):::domain
                end
                entry -->|"starts"| ctrl
                ctrl -->|"validates"| mid
                mid -->|"processes"| use
                use -->|"appliesBusiness"| dom
                use -->|"callsRepo"| rep
                rep -->|"dataAccess"| prismaNode("Prisma ORM (prisma)"):::prisma
            end
            %% Database Container
            subgraph "Database Container"
                mysqlNode("MySQL Database"):::mysql
            end
            prismaNode -->|"connectsTo"| mysqlNode
        end
    end

    %% External Dependencies & HTTP Request
    req("HTTP Request"):::external
    expLib("Express"):::app
    prismaLib("Prisma"):::prisma
    mysqlLib("MySQL"):::mysql
    dockerLib("Docker"):::docker
    di("tsyringe (DI)"):::external
    validation("zod (Validation)"):::external

    req -->|"calls"| entry
    expLib --- entry
    prismaLib --- prismaNode
    mysqlLib --- mysqlNode
    di --- use
    validation --- mid

    %% Click Events
    click DC1 "https://github.com/masatotezuka/clean-architecture-sample-app/tree/main/Dockerfile"
    click DC2 "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/docker-compose.yaml"
    click DC3 "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/Docker/db/init.sql"
    click entry "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/src/index.ts"
    click ctrl "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/src/controller/index.ts"
    click mid "https://github.com/masatotezuka/clean-architecture-sample-app/tree/main/src/middleware"
    click use "https://github.com/masatotezuka/clean-architecture-sample-app/tree/main/src/use-case"
    click dom "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/src/domain/user.entity.ts"
    click rep "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/src/repositories/user.repository.ts"
    click prismaNode "https://github.com/masatotezuka/clean-architecture-sample-app/blob/main/prisma/index.ts"

    %% Styles
    classDef docker fill:#f6d186,stroke:#333,stroke-width:2px;
    classDef app fill:#bbdefb,stroke:#333,stroke-width:2px;
    classDef controller fill:#81d4fa,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ffcc80,stroke:#333,stroke-width:2px;
    classDef usecase fill:#ffe082,stroke:#333,stroke-width:2px;
    classDef domain fill:#d1c4e9,stroke:#333,stroke-width:2px;
    classDef prisma fill:#a5d6a7,stroke:#333,stroke-width:2px;
    classDef mysql fill:#80cbc4,stroke:#333,stroke-width:2px;
    classDef external fill:#c8e6c9,stroke:#333,stroke-width:2px;
```
