How macchiitaka/tasks-api-express works
Project Type and Purpose
• The project is a full-stack back-end API that offers task management functionalities via a REST API.
• It uses Express as the web framework, TypeScript for type safety, and Prisma as an ORM connecting to a PostgreSQL database.
• The architecture is inspired by Clean Architecture, which separates application concerns into distinct layers (Domain, Application, Infrastructure, and Interfaces).
Analyzing the File Structure – Key Directories
• src/application:
• Contains use cases (e.g., create-task, get-task, list-tasks, remove-task, replace-task) and repository interfaces that abstract how the domain accesses data.
• src/domain:
• Contains domain models (e.g., task-model.ts) which represent the core business entities.
• src/infrastructure:
• Contains controllers, handlers, the router, and the server setup. This layer acts as the delivery mechanism for HTTP requests.
• src/interfaces:
• Provides adapters for external systems:
• Controllers testing and implementation.
• Database adapter using Prisma (prisma.ts).
• Repository implementations bridging the domain with the database.
• Serializers to format the data for outgoing API responses.
• prisma:
• Consists of the schema and migration files that define the database structure and transformation history.
• Other configuration and tooling files (e.g., .eslintrc.js, prettier config, GitHub workflows) help in building, testing, linting, and deploying the project.
Main Components
a. External Clients & HTTP Requests:
• External API consumers making HTTP requests.
b. API Layer (Express Server):
• Entry point (server.ts) that initializes the Express app, applies middleware (e.g., Helmet, CORS), and sets up the routing.
• Router (router.ts) that forwards specific task-related endpoints to the corresponding controllers.
c. Controllers & Handlers:
• Handlers (in infrastructure/handlers) responsible for executing task-related operations.
d. Application Layer:
• Use Cases (e.g., create-task, get-task) that encapsulate the business logic.
• Repository Interfaces to abstract data access, thus decoupling the application logic from persistence concerns.
e. Domain Layer:
• Domain models (e.g., task-model.ts) representing the fundamental business entities.
• That these models are independent of external frameworks.
f. Data Persistence Layer:
• Adapters using Prisma (prisma/schema.prisma, prisma/migrations) to interact with a PostgreSQL database.
• Repository implementations (e.g., task-repository.ts) placed in interfaces connect the domain to the database through Prisma.
g. External Services and Tools:
• Deployment tools (e.g., Heroku as depicted by Procfile).
• Utility tools such as GitHub workflows for CI/CD, linters, and formatters, though these are not directly part of the runtime flow, they ensure code quality and smooth deployment.
Relationships and Interactions
• From the server/router, show how requests are forwarded to controllers/handlers.
• Controllers then invoke use cases from the application layer, which in turn leverage domain models to execute business logic.
• The application layer interacts with repository interfaces; these interfaces are implemented by adapter classes in the interfaces directory.
• Flow from these repository implementations to the persistence layer (Prisma & PostgreSQL).
• Optionally, include feedback flow for error handling and logging by referencing utility modules (from src/utils).
Architectural Patterns and Design Principles
• Use of Clean Architecture: the system is structured so that the domain and application layers are independent of external details.
• Use dependency inversion to show that higher-level modules (use cases) do not depend on lower-level modules (database implementations) directly.
• Clearly separate concerns between layers to facilitate testing and future scalability.