This project is a robust RESTful API built with Node.js, Fastify, and TypeScript. It follows the principles of Clean Architecture and Domain-Driven Design to create a scalable, maintainable, and testable codebase.
This project is structured following the principles of Clean Architecture by Robert C. Martin. This architectural style emphasizes the separation of concerns, creating a system that is independent of frameworks, UI, and databases.
Image courtesy of Robert C. Martin (Uncle Bob)
The core of the application is built around the Domain and Application layers, which contain the business logic and are independent of any external frameworks. The outer layers, Presentation and Infrastructure, handle details like HTTP requests, database interactions, and other external services.
It uses concepts from Domain-Driven Design to model the business domain of the forum.
- Entities: Core objects of the domain with a unique identifier.
- Value Objects: Objects that represent a descriptive aspect of the domain without a conceptual identifier.
- Repositories: Provide an abstraction over data persistence, allowing the application layer to remain independent of the database technology.
- Repository: Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
- Factory: Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
- Adapter: Allows objects with incompatible interfaces to collaborate.
- Strategy: Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
- In-Memory Database: For unit and integration tests, it uses in-memory repositories to replace the actual database. This provides a fast and isolated test environment.
- Stubs: It uses stubs to replace real implementations of certain modules (like use cases) with a controlled, predictable behavior during tests.
- Spies/Mocks: To verify interactions between different parts of the code, it uses spies and mocks to observe function calls and their arguments.
- Fakes: It uses factory functions to generate fake data for testing, ensuring consistent and repeatable test scenarios.
├── prisma/ # Prisma schema, migrations, and seed scripts
├── src/
│ ├── application/ # Application-specific business rules (Use Cases)
│ ├── domain/ # Enterprise-wide business rules (Entities, Value Objects)
│ ├── infra/ # Frameworks, drivers, and external dependencies (DB, HTTP, etc.)
│ ├── main/ # Composition root, where everything is wired up
│ └── presentation/ # Controllers to handle HTTP requests and responses
├── .env.example # Example environment variables
└── package.json # Project dependencies and scripts
- Node.js (v18 or higher recommended)
- pnpm
- Docker and Docker Compose
- Clone the repository.
- Install dependencies using
pnpm install
. - Set up environment variables by copying
.env.example
to.env.development
. - Start the database and Redis using the provided
pnpm
scripts (e.g.,pnpm db:up:dev
). - Run database migrations using the provided
pnpm
scripts (e.g.,pnpm migrate:dev
).
- Development Mode:
pnpm start:dev
- Production Mode:
pnpm build
and thenpnpm start
-
Run unit tests:
pnpm test:unit
-
Run integration tests:
pnpm test:int
-
Run E2E tests:
pnpm test:e2e
-
Generate test coverage report:
pnpm test:ci
This project uses Swagger for interactive API documentation. Once the application is running, you can access the Swagger UI at: