cqrs icon indicating copy to clipboard operation
cqrs copied to clipboard

feat: add support for using factory providers for `QueryHandler` and `CommandHandler`

Open JoshVee opened this issue 4 months ago • 0 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [x] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • [ ] Bugfix
  • [x] Feature
  • [ ] Code style update (formatting, local variables)
  • [ ] Refactoring (no functional changes, no api changes)
  • [ ] Build related changes
  • [ ] CI related changes
  • [ ] Other... Please describe:

What is the current behavior?

Currently, all @QueryHandler and @CommandHandler implementations need to be class providers. If a factory provider is used, a TypeError is thrown when trying to reflect the command \ query metadata, eg:

TypeError
    at Reflect.getMetadata (/Users/work/code/apps/node_modules/reflect-metadata/Reflect.js:367:23)
    at QueryBus.reflectQueryId (/Users/work/code/apps/node_modules/@nestjs/cqrs/dist/query-bus.js:114:39)

What is the new behavior?

This PR modifies the InstanceWrapper reflection code within CommandBus and QueryBus to support factory providers.

The implementation reflects that used in EventBus.

Does this PR introduce a breaking change?

  • [ ] Yes
  • [x] No

Other information

JoshVee avatar Aug 20 '25 23:08 JoshVee