Skip to content

maalayat/node-typescript-ddd

Repository files navigation

Node TypeScript DDD πŸ”·

A comprehensive Domain-Driven Design (DDD) implementation in TypeScript and Node.js, following hexagonal architecture principles and CQRS patterns.

Overview

This project demonstrates advanced DDD concepts including:

  • Domain-Driven Design: Clear separation of domain, application, and infrastructure layers
  • CQRS: Command Query Responsibility Segregation with separate buses
  • Event-Driven Architecture: Domain events with RabbitMQ support
  • Hexagonal Architecture: Clean architecture with dependency inversion
  • Dependency Injection: IoC container with YAML configuration
  • Testing Strategy: Unit tests with Jest and BDD with Cucumber

Tech Stack

Prerequisites

  • Node.js >= 14.0.0
  • npm >= 6.14.0
  • Docker and Docker Compose (for MongoDB and RabbitMQ)

Installation

  1. Clone the repository

    git clone https://github.com/mayalaat/node-typescript-ddd.git
    cd node-typescript-ddd
  2. Install dependencies

    npm install
  3. Start infrastructure services

    docker-compose up -d

    This starts:

    • MongoDB on port 27017
    • RabbitMQ on port 5672 (management UI on 15672)
  4. Configure RabbitMQ

    npm run command:mooc:rabbitmq

Usage

Development Server

npm run dev:mooc:backend

The API will be available at http://localhost:3000

Available Endpoints

Create Course

PUT /courses
Content-Type: application/json

{
  "id": "course-uuid",
  "name": "Course Name",
  "duration": "10 hours"
}

Get Courses Count

GET /courses_counter

Health Check

GET /status

Development

Build

npm run build          # Full build (clean + compile + copy assets)
npm run build:tsc      # TypeScript compilation only
npm run build:clean    # Clean dist directory

Testing

npm test                        # Run all tests
npm run test:unit              # Unit tests only (Jest)
npm run test:features          # Feature tests only (Cucumber)
npm run test:mooc:backend:features  # Backend feature tests

Code Quality

npm run lint           # Format code with Prettier

Architecture

Project Structure

src/
β”œβ”€β”€ Contexts/                 # Bounded Contexts
β”‚   β”œβ”€β”€ Mooc/                # Main business context
β”‚   β”‚   β”œβ”€β”€ Courses/         # Course aggregate
β”‚   β”‚   └── CoursesCounter/  # Counter aggregate
β”‚   └── Shared/              # Shared kernel
β”‚       β”œβ”€β”€ domain/          # Base classes and interfaces
β”‚       └── infrastructure/  # Shared infrastructure
└── apps/
    └── mooc/
        └── backend/         # Express application
            β”œβ”€β”€ controllers/ # HTTP controllers
            β”œβ”€β”€ routes/      # Route definitions
            └── config/      # Application configuration

Key Patterns Implemented

CQRS (Command Query Responsibility Segregation)

  • Commands: Modify state through CommandBus
  • Queries: Read data through QueryBus
  • Separate handlers for each command/query

Domain Events

  • Aggregates extend AggregateRoot
  • Events are recorded and published after successful operations
  • Supports both in-memory and RabbitMQ event buses

Value Objects

  • Encapsulate business rules and validation
  • Extend base classes: StringValueObject, IntValueObject, Uuid

Repository Pattern

  • Abstract repositories in domain layer
  • Concrete implementations in infrastructure
  • Support for MongoDB and file-based storage

Dependency Injection

The application uses YAML configuration for dependency injection:

# Environment-specific configuration files
β”œβ”€β”€ application_dev.yaml      # Development environment
β”œβ”€β”€ application_production.yaml  # Production environment
└── application_test.yaml    # Test environment

Services are organized by bounded context and automatically wired based on configuration.

Testing Strategy

Unit Tests (Jest)

  • Domain logic testing with mocks
  • Test doubles follow *Mock.ts and *Mother.ts patterns
  • Located in tests/ directory mirroring source structure

Feature Tests (Cucumber)

  • BDD scenarios in Gherkin syntax
  • End-to-end API testing
  • Located in tests/apps/mooc/backend/features/

Running Specific Tests

# Run tests for specific bounded context
npm run test:unit -- --testPathPattern=Courses

# Run single feature file
npx cucumber-js tests/apps/mooc/backend/features/courses/create-course.feature

Environment Configuration

The application uses environment-specific configuration:

  • NODE_ENV=dev - Development (default)
  • NODE_ENV=test - Testing
  • NODE_ENV=production - Production

Configuration is managed through convict with JSON files in /config/.

Docker Support

Use the provided docker-compose.yml for local development:

docker-compose up -d     # Start services in background
docker-compose down      # Stop and remove containers
docker-compose logs -f   # Follow logs

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes following the existing patterns
  4. Ensure tests pass: npm test
  5. Format code: npm run lint
  6. Submit a pull request

The project uses Husky pre-commit hooks to ensure code quality.

License

This project is part of a Domain-Driven Design course and is for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published