How rafaelcetina/cars-api works
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.