Skip to content

manoel-lopes/template-node-api

Repository files navigation

Clean Architecture API

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.

🛠️ Technologies

🏛️ Architecture

Clean Architecture

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.

Clean Architecture Diagram

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.

Domain-Driven Design (DDD)

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.

🏗️ Design Patterns

  • 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.

🧪 Test Patterns

  • 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.

📂 Project Structure

├── 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

🚀 Setup & Installation

Prerequisites

  • Node.js (v18 or higher recommended)
  • pnpm
  • Docker and Docker Compose

Installation Steps

  1. Clone the repository.
  2. Install dependencies using pnpm install.
  3. Set up environment variables by copying .env.example to .env.development.
  4. Start the database and Redis using the provided pnpm scripts (e.g., pnpm db:up:dev).
  5. Run database migrations using the provided pnpm scripts (e.g., pnpm migrate:dev).

Running the Application

  • Development Mode: pnpm start:dev
  • Production Mode: pnpm build and then pnpm start

🧪 Testing

  • 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

Swagger Documentation

This project uses Swagger for interactive API documentation. Once the application is running, you can access the Swagger UI at:

http://localhost:3000/docs

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •