This is an advanced boilerplate project implementing Domain-Driven Design (DDD), Clean Architecture, CQRS (Command Query Responsibility Segregation), Event Sourcing and MongoDB with NestJS. It provides a robust foundation for building scalable and maintainable enterprise-level applications with proper separation of concerns and clean dependency direction.
If you want more documentation about NestJS, click here Nest
📝 Note: This version uses MongoDB with Mongoose. If you prefer the PostgreSQL version with TypeORM, you can find it at the original repository: https://github.com/CollatzConjecture/nestjs-clean-architecture-postgres
A quick introduction to clean architecture
bcrypt with salt rounds.git clone https://github.com/CollatzConjecture/nestjs-clean-architecture
cd nestjs-clean-architecture
.
├── doc/
│ ├── common.http # Common API requests
│ └── users.http # User-specific API requests
├── src/
│ ├── api/ # API Layer (HTTP Controllers & DTOs)
│ │ ├── controllers/
│ │ │ └── *.controller.ts # HTTP endpoints (auth, profile, hello)
│ │ ├── dto/
│ │ │ ├── auth/ # Authentication DTOs
│ │ │ │ └── *.dto.ts # Login & register DTOs
│ │ │ └── *.dto.ts # Profile management DTOs
│ │ └── api.module.ts # API module configuration
│ ├── application/ # Application Layer (Business Orchestration)
│ │ ├── __test__/
│ │ │ └── *.spec.ts # Application layer tests
│ │ ├── auth/
│ │ │ ├── command/ # Auth commands & handlers
│ │ │ │ ├── *.command.ts # Create/delete auth user commands
│ │ │ │ └── handler/
│ │ │ │ └── *.handler.ts # Command handlers
│ │ │ ├── events/ # Auth domain events
│ │ │ │ └── *.event.ts # User created/deleted events
│ │ │ ├── sagas/
│ │ │ │ └── *.saga.ts # Registration flow orchestration
│ │ │ ├── decorators/
│ │ │ │ └── *.decorator.ts # Custom decorators (roles)
│ │ │ ├── guards/
│ │ │ │ └── *.guard.ts # Authentication & authorization guards
│ │ │ ├── *.strategy.ts # Auth strategies (JWT, local, Google OAuth)
│ │ │ └── auth.module.ts # Auth module configuration
│ │ ├── decorators/
│ │ │ └── *.decorator.ts # Global decorators (current user)
│ │ ├── interfaces/
│ │ │ └── *.interface.ts # Application interfaces
│ │ ├── interceptors/
│ │ │ └── *.interceptor.ts # Request logging interceptors
│ │ ├── middlewere/
│ │ │ └── *.middleware.ts # HTTP middleware (logging)
│ │ ├── services/
│ │ │ └── *.service.ts # Application services (auth, profile, logger)
│ │ ├── profile/
│ │ │ ├── command/ # Profile commands & handlers
│ │ │ │ ├── *.command.ts # Profile commands
│ │ │ │ └── handler/
│ │ │ │ └── *.handler.ts # Command handlers
│ │ │ ├── events/ # Profile domain events
│ │ │ │ └── *.event.ts # Profile events
│ │ │ └── profile.module.ts # Profile module configuration
│ │ └── application.module.ts # Application module aggregator
│ ├── domain/ # Domain Layer (Pure Business Logic)
│ │ ├── __test__/
│ │ │ └── *.spec.ts # Domain layer tests
│ │ ├── aggregates/ # Domain aggregates
│ │ ├── entities/
│ │ │ ├── *.ts # Pure domain entities (Auth, Profile)
│ │ │ └── enums/ # Domain enums
│ │ │ └── *.enum.ts # Role enums, etc.
│ │ ├── interfaces/
│ │ │ └── repositories/ # Repository contracts defined by domain
│ │ │ └── *.interface.ts # Repository interfaces
│ │ └── services/
│ │ └── *.service.ts # Pure business logic services
│ ├── infrastructure/ # Infrastructure Layer (External Concerns)
│ │ ├── database/
│ │ │ ├── database.module.ts # Database configuration
│ │ │ └── database.providers.ts # Database providers
│ │ ├── health/
│ │ │ └── *.check.ts # Health check configurations
│ │ ├── logger/
│ │ │ └── logger.module.ts # Global logger module
│ │ ├── models/
│ │ │ ├── *.model.ts # MongoDB models (auth, profile)
│ │ │ └── index.ts # Model exports
│ │ └── repository/
│ │ └── *.repository.ts # Repository implementations
│ ├── main.ts # Application entry point
│ ├── app.module.ts # Root application module
│ └── constants.ts # Application constants
├── test/
│ ├── *.e2e-spec.ts # End-to-end tests
│ ├── jest-e2e.json # E2E test configuration
│ └── setup-e2e.ts # E2E test setup
├── prometheus/
│ └── prometheus.yml # Prometheus configuration
├── docker-compose*.yml # Docker Compose configurations (dev, prod)
└── Dockerfile # Container definition
This project follows a strict 4-layer architecture:
src/api/): HTTP controllers, DTOs, and request/response handlingsrc/application/): Business orchestration, CQRS coordination, and application servicessrc/domain/): Pure business logic, entities, and domain servicessrc/infrastructure/): Database, external services, and technical implementationssrc/application/*/command.src/application/*/query.API Controller → Application Service → Domain Service (validation) →
RegisterCommand → CreateAuthUser → AuthUserCreated Event →
RegistrationSaga → CreateProfile → ProfileCreated
API Controller → Application Service → Domain Service (email validation) →
LoginCommand → ValidateUser → JWT Token Generation
/auth/google → Google OAuth → /auth/google/redirect →
Domain Service (validation) → FindOrCreateUser → JWT Token Generation
ProfileCreationFailed Event → RegistrationSaga →
DeleteAuthUser (Compensating Transaction)@Global() decoratorThe project is configured to run seamlessly with Docker. Use the pnpm scripts from package.json for convenience.
# Build and start containers in detached mode for development
$ pnpm run docker:dev
# Build and start containers for production
$ pnpm run docker:prod
# View logs for the API service
$ pnpm run docker:logs
# Stop all running containers
$ pnpm run docker:down
# Restart the development environment
$ pnpm run docker:restart
$ pnpm install
# Development
$ pnpm run start
# Watch mode (recommended for development)
$ pnpm run start:dev
# Production mode
$ pnpm run start:prod
# Debug mode
$ pnpm run start:debug
# Unit tests
$ pnpm run test
# E2E tests
$ pnpm run test:e2e
# Test coverage
$ pnpm run test:cov
# Watch mode
$ pnpm run test:watch
# Check code style
$ pnpm run lint
# Auto-fix issues where possible
$ pnpm run lint:fix
You can import this Postman collection to test the API endpoints.
The collection includes:
NestJS CA-DDD.postman_collection.json into Postmanlocalhost: http://localhost (or your host)port: 4000 (or your configured port)Authorization: Bearer <your-jwt-token> (set after login)POST /auth/register # User registration
POST /auth/login # User login
POST /auth/logout # User logout (Protected)
POST /auth/refresh-token # Token refresh (Protected)
GET /auth/google # Initiate Google OAuth login
GET /auth/google/redirect # Google OAuth callback
GET /auth/:id # Get user by auth ID (Protected)
DELETE /auth/:id # Delete user by auth ID (Protected)
```http GET /profile/all # Get all user profiles (Admin only) GET /profile/admins # Get all admin users (Admin only) GET /p
$ claude mcp add nestjs-clean-architecture \
-- python -m otcore.mcp_server <graph>