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

OptionTypeDefaultDescription
namestring-Name of the module (required)
pathstringsrcPath where the module will be generated
modulestringauto-detectedPath 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

  1. Domain Layer (domain/): Contains business logic, entities, and repository interfaces
  2. Application Layer (application/): Contains use cases (commands, queries, and handlers)
  3. 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/typeorm
  • typeorm

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.