# rafaelcetina/cars-api architecture

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

- Interactive diagram: https://gitdiagram.com/rafaelcetina/cars-api
- Repository: https://github.com/rafaelcetina/cars-api

## Overview

##### Project Type and Purpose
- The project is a RESTful API for managing car brands and models. It uses Express, NodeJS, and TypeScript.
- The project implements Clean Architecture, separating concerns into distinct layers (modules for brands and models) and facilitating maintainability and testing.
- The API communicates with a PostgreSQL database, and the project includes database schema (database.sql) and dump scripts (dump.js) for data seeding.
- Deployment is done via Render.com.

##### Analyzing the File Structure
- At the root level, you see configuration files (.env.example, tsconfig.json, package.json, etc.), scripts (dump.js), and database scripts (database.sql, models.json). This indicates clear separation of configuration, setup, and execution.
- The src directory holds all application code. Under src:
- The controllers directory acts as an interface with Express, mapping routes to endpoints and handling requests (e.g., BrandController.ts, ModelController.ts).
- The routes directory defines Express routes (brands.ts, models.ts) and directs them to their appropriate controllers.
- The modules directory contains subdirectories for each domain resource (brands and models) further divided by Clean Architecture layers:
- application: Contains the use cases (e.g., BrandsCreator, BrandsGetter, ModelsCreator, ModelsGetter, etc.) that capture the business logic.
- domain: Holds domain entities (Brand.ts, Model.ts) and interfaces (BrandRepository, ModelRepository) that define contracts for repository implementations.
- infrastructure: Contains the adapting layers for the database (e.g., BrandContainer, DbBrandRepository, ModelContainer, DbModelRepository) and any infrastructure-related concerns.
- The shared directory includes cross-cutting components like the PostgreSQL client (PgClient) for database connectivity.

##### Key Components
a. Express Server:
- Express server setup found in src/index.ts.
- How routes (from src/routes) connect to controllers (from src/controllers) and invoke application layer use cases.
b. Use Cases (Application Layer):
- Use case classes (e.g., BrandsCreator, BrandsGetter, ModelsCreator, ModelsGetter) that encapsulate business logic.
c. Domain Layer:
- Core domain entities (Brand, Model) and their associated interfaces (BrandRepository, ModelRepository) that define the contracts.
d. Infrastructure Layer:
- Actual repository implementations (DbBrandRepository, DbModelRepository) and container management components.
- Mention the shared PostgreSQL client (PgClient) which is used by these repositories.
e. Database:
- PostgreSQL as a separate component interacting with the infrastructure layer.
f. External Deployment:
- Render.com as the production deployment platform.

##### Defining Relationships and Architectural Patterns
- Data flow from the HTTP request at the Express layer to the controllers. Controllers call the application layer (use cases), which then interact with the domain layer via defined interfaces.
- From Routes → Controllers → Application Use Cases → Domain Interfaces, with the Infrastructure layer fulfilling these contracts by interacting with PostgreSQL.
- Adherence to Clean Architecture, displaying separation between layers and dependency inversion (domain interfaces have their implementations in infrastructure).

- The Express server (HTTP Entry Point) → Routes layer (src/routes) → Controllers (src/controllers)
- Continue by expanding on:
- The Application Layer (use cases under src/modules/.../application) which encapsulate business logic.
- The Domain Layer (entities and interfaces under src/modules/.../domain) that abstract business entities and repository contracts.
- The Infrastructure Layer (repository implementations under src/modules/.../infrastructure, leveraging the PgClient from src/modules/shared/domain).
- PostgreSQL as a separate component that interacts with the Infrastructure layer.
- Optionally, include a note on deployment (Render.com) as the hosting environment.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External and Deployment
    Consumer("External Consumer (HTTP Request)"):::external
    Deploy("Render.com (Deployment)"):::external

    %% Presentation Layer
    subgraph "Presentation Layer"
        ES("Express Server (src/index.ts)"):::express
        subgraph "Routes"
            RT1("Brands Route (brands.ts)"):::routes
            RT2("Models Route (models.ts)"):::routes
        end
        subgraph "Controllers"
            CT1("Brand Controller (BrandController.ts)"):::controllers
            CT2("Model Controller (ModelController.ts)"):::controllers
        end
    end

    %% Application Layer (Use Cases)
    subgraph "Application Layer (Use Cases)"
        subgraph "Brands Use Cases"
            BUC("BrandsCreator (BrandsCreator.ts)"):::usecase
            BUG("BrandsGetter (BrandsGetter.ts)"):::usecase
        end
        subgraph "Models Use Cases"
            MUC("ModelsCreator (ModelsCreator.ts)"):::usecase
            MUG("ModelsGetter (ModelsGetter.ts)"):::usecase
        end
    end

    %% Domain Layer (Entities & Interfaces)
    subgraph "Domain Layer (Entities & Interfaces)"
        subgraph "Brands Domain"
            BE("Brand Entity (Brand.ts)"):::domain
            BR("BrandRepository Interface (BrandRepository.ts)"):::domain
        end
        subgraph "Models Domain"
            ME("Model Entity (Model.ts)"):::domain
            MR("ModelRepository Interface (ModelRepository.ts)"):::domain
        end
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        subgraph "Brands Infrastructure"
            BC("BrandContainer (BrandContainer.ts)"):::infrastructure
            DBR("DbBrandRepository (DbBrandRepository.ts)"):::infrastructure
        end
        subgraph "Models Infrastructure"
            MC("ModelContainer (ModelContainer.ts)"):::infrastructure
            DMR("DbModelRepository (DbModelRepository.ts)"):::infrastructure
        end
        PG("PostgreSQL Client (PgClient.ts)"):::infrastructure
    end

    %% Database
    subgraph "Database"
        DB("PostgreSQL Database"):::database
        DS("Database Schema (database.sql)"):::database
        DS2("Dump/Seed Script (dump.js)"):::database
    end

    %% Connections
    Deploy --> ES
    Consumer --> ES

    ES --> RT1
    ES --> RT2

    RT1 --> CT1
    RT2 --> CT2

    CT1 --> BUC
    CT1 --> BUG
    CT2 --> MUC
    CT2 --> MUG

    BUC --> BR
    BUG --> BR
    MUC --> MR
    MUG --> MR

    BR --> DBR
    MR --> DMR

    BC --> DBR
    MC --> DMR

    DBR --> PG
    DMR --> PG
    PG --> DB

    DS --- DB
    DS2 --- DB

    %% Styles
    classDef external fill:#fef6e4,stroke:#333,stroke-width:2px;
    classDef express fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef routes fill:#d1f0d1,stroke:#333,stroke-width:2px;
    classDef controllers fill:#d1f0ff,stroke:#333,stroke-width:2px;
    classDef usecase fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef domain fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef database fill:#d6d8db,stroke:#333,stroke-width:2px;

    %% Click Events
    click ES "https://github.com/rafaelcetina/cars-api/blob/main/src/index.ts"
    click RT1 "https://github.com/rafaelcetina/cars-api/blob/main/src/routes/brands.ts"
    click RT2 "https://github.com/rafaelcetina/cars-api/blob/main/src/routes/models.ts"
    click CT1 "https://github.com/rafaelcetina/cars-api/blob/main/src/controllers/BrandController.ts"
    click CT2 "https://github.com/rafaelcetina/cars-api/blob/main/src/controllers/ModelController.ts"
    click BUC "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/application/BrandsCreator.ts"
    click BUG "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/application/BrandsGetter.ts"
    click MUC "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/application/ModelsCreator.ts"
    click MUG "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/application/ModelsGetter.ts"
    click BE "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/domain/entities/Brand.ts"
    click BR "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/domain/interfaces/BrandRepository.ts"
    click ME "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/domain/entities/Model.ts"
    click MR "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/domain/interfaces/ModelRepository.ts"
    click BC "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/infrastructure/BrandContainer.ts"
    click DBR "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/brands/infrastructure/repositories/DbBrandRepository.ts"
    click MC "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/infrastructure/ModelContainer.ts"
    click DMR "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/models/infrastructure/repositories/DbModelRepository.ts"
    click PG "https://github.com/rafaelcetina/cars-api/blob/main/src/modules/shared/domain/PgClient.ts"
    click DS "https://github.com/rafaelcetina/cars-api/blob/main/database.sql"
    click DS2 "https://github.com/rafaelcetina/cars-api/blob/main/dump.js"
```
