NestJS CQRS Hexagonal Architecture Schematic
June 19, 2025 ยท View on GitHub
A powerful Angular CLI schematic for generating CQRS (Command Query Responsibility Segregation) modules following hexagonal architecture principles in NestJS applications.
Features
- ๐๏ธ Complete CQRS Structure: Generates commands, queries, handlers, and controllers
- ๐ท Hexagonal Architecture: Follows clean architecture principles with clear separation of concerns
- ๐๏ธ TypeORM Integration: Includes repository pattern with TypeORM entities
- ๐ TypeScript Support: Fully typed with proper interfaces and DTOs
- ๐ง Module Registration: Automatically registers the generated module in the parent module
- โ Validation: Input validation using Zod schemas
- ๐ฏ Best Practices: Follows NestJS and clean architecture best practices
Installation
npm install -D @lcasass3/nest-schematics
Usage
Basic Usage
# Generate a CQRS module for a "user" entity
nest g -c @lcasass3/nest-schematics cqrs-module user
# Generate in a specific path
nest g -c @lcasass3/nest-schematics cqrs-module user --path=src/modules
# Specify parent module for registration
nest g -c @lcasass3/nest-schematics cqrs-module user --module=src/app.module.ts
Options
| Option | Type | Default | Description |
|---|---|---|---|
name | string | - | Name of the module (required) |
path | string | src | Path where the module will be generated |
module | string | auto-detected | Path to the parent module file |
Generated Structure
When you run the schematic with schematics @lcasass3/nest-schematics:cqrs-module user, it
generates:
src/user/
โโโ user.module.ts # Main module file
โโโ application/
โ โโโ commands/
โ โ โโโ handlers/
โ โ โ โโโ create-user.handler.ts # Create command handler
โ โ โ โโโ update-user.handler.ts # Update command handler
โ โ โ โโโ delete-user.handler.ts # Delete command handler
โ โ โ โโโ index.ts # Barrel export
โ โ โโโ impl/
โ โ โโโ create-user.command.ts # Create command
โ โ โโโ update-user.command.ts # Update command
โ โ โโโ delete-user.command.ts # Delete command
โ โ โโโ index.ts # Barrel export
โ โโโ queries/
โ โโโ handlers/
โ โ โโโ all-users.handler.ts # Get all query handler
โ โ โโโ find-user-by-id.handler.ts # Find by ID query handler
โ โ โโโ index.ts # Barrel export
โ โโโ impl/
โ โโโ all-users.query.ts # Get all query
โ โโโ find-user-by-id.query.ts # Find by ID query
โ โโโ index.ts # Barrel export
โโโ domain/
โ โโโ entities/
โ โ โโโ user.entity.ts # Domain entity
โ โ โโโ index.ts # Barrel export
โ โโโ repositories/
โ โ โโโ users.repository.ts # Repository interface
โ โ โโโ index.ts # Barrel export
โ โโโ dtos/
โ โโโ create-user.dto.ts # Create DTO with validation
โ โโโ update-user.dto.ts # Update DTO with validation
โ โโโ index.ts # Barrel export
โโโ infrastructure/
โโโ users.controller.ts # REST controller
โโโ persistance/
โโโ entities/
โ โโโ user.typeorm-entity.ts # TypeORM entity
โ โโโ index.ts # Barrel export
โโโ repositories/
โ โโโ users.typeorm-repository.ts # TypeORM repository
โ โโโ index.ts # Barrel export
โโโ modules/
โโโ typeorm-user.module.ts # TypeORM module
โโโ index.ts # Barrel export
Architecture Overview
Hexagonal Architecture Layers
- Domain Layer (
domain/): Contains business logic, entities, and repository interfaces - Application Layer (
application/): Contains use cases (commands, queries, and handlers) - Infrastructure Layer (
infrastructure/): Contains external concerns (controllers, database, etc.)
CQRS Pattern
- Commands: Handle write operations (create, update, delete)
- Queries: Handle read operations (find, get all)
- Handlers: Process commands and queries
- DTOs: Data transfer objects with validation
Dependencies
The schematic will check for and warn about missing dependencies:
@nestjs/common@nestjs/core@nestjs/cqrs@nestjs/typeormtypeorm
Development
Building the Schematic
# Install dependencies
npm install
# Build
npm run build
# Build in watch mode
npm run build:watch
Code Quality Features
- โ Validation: Comprehensive input validation and error handling
- ๐ Smart Module Detection: Automatically finds the appropriate parent module
- ๐ซ Duplicate Prevention: Prevents duplicate imports and module registrations
- ๐ Clear Logging: Informative console output for debugging
- ๐๏ธ Modular Design: Well-organized utility classes for maintainability
License
MIT
Support
For issues and questions, please open an issue on the GitHub repository.