NestJS schematics for generating CQRS commands and queries
npm install nest-cqrs-schematicsA custom schematic collection to generate Command and Query files (along with their handlers) for NestJS projects using the CQRS pattern.
> ā”ļø Built to save time and reduce boilerplate when working with @nestjs/cqrs.
---
- š§© Generate Command and CommandHandler
- š Generate Query and QueryHandler
- š Quick scaffolding using Nest CLI
- āļø Easily customizable boilerplate after generation
---
Install the package as a dev dependency:
``bash`
pnpm add -D nest-cqrs-schematics
> You can also use npm or yarn if preferred.
---
Add the following scripts to your package.json to simplify command execution:
`json`
{
"scripts": {
"gen:cmd": "nest g -c nest-cqrs-schematics command",
"gen:query": "nest g -c nest-cqrs-schematics query"
}
}
Run the following commands using the NestJS CLI:
`bash`
npm run gen:cmd create-user modules/features/users
Generates:
``
src/
āāā modules/
āāā features/
āāā users/
āāā commands/
āāā impl/
ā āāā create-user.command.ts
āāā handlers/
āāā create-user.handler.ts
---
`bash`
npm run gen:query get-user modules/features/users
Generates:
``
src/
āāā modules/
āāā features/
āāā users/
āāā queries/
āāā impl/
ā āāā get-user.query.ts
āāā handlers/
āāā get-user.handler.ts
---
`ts`
export class CreateUserCommand {
constructor(public readonly name: string, public readonly email: string) {}
}
`ts
import { CommandHandler, ICommandHandler } from "@nestjs/cqrs";
import { CreateUserCommand } from "../impl/create-user.command";
@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler
async execute(command: CreateUserCommand): Promise
const { name, email } = command;
// TODO: Implement business logic
}
}
`
---
Planned for future versions:
- š£ Support for Events and Event Handlers
- š Support for Sagas
- šļø Custom configuration options (e.g., naming, file structure)
---
- This package only works with NestJS projects.
- Ensure you have the @nestjs/cqrs` module installed in your project.
- Generated code is fully editable and meant to be a starting point.
---
If you find this tool helpful, feel free to share your feedback, report issues, or contribute improvements!
Maintained with ā¤ļø by Rameez Hasan ā always open to collaboration and community input.
---