Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom command publisher #1668

Open
1 task done
KostyaZgara opened this issue Mar 26, 2024 · 0 comments
Open
1 task done

Use custom command publisher #1668

KostyaZgara opened this issue Mar 26, 2024 · 0 comments
Labels

Comments

@KostyaZgara
Copy link

KostyaZgara commented Mar 26, 2024

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I can't use just custom publisher, when I call commandBus.execute() it fails with the error CommandHandlerNotFoundException. So, it forces me to have some kind of mock for the command handler with empty execute method to bypass the previous logic and publish my command to external service

Describe the solution you'd like

I think we can refactor CommandBus.execute method

execute<T extends CommandBase, R = any>(command: T): Promise<R> {
in a way like it is implemented in EventBus.publish
publish<TEvent extends EventBase, TContext = unknown>(
, so move all current logic placed in execute method to default command publisher, and in execute method just call this._publisher.publish(command)

execute<T extends CommandBase, R = any>(command: T): Promise<R> {
    return this._publisher.publish(command);
  }

Teachability, documentation, adoption, migration strategy

The API design won't be changed, the existing users will still use prior logic without any breaking changes, since all current logic will be moved to default command.

Here is my idea how it can be used. User will define command with custom decorators to reflect destination of command

@Queue('tests', 'create_test')
class CreateTestCommand {}

Then the existing commandBus API will be used, but custom publisher will reflect all required metadata from passed command

await commandBus.execute(new CreateTestCommand())

The user can define any shape of their handler according to publisher, as an example we can use @nestjs/microservices package

@Controller()
class TestHandler {
  @EventPattern('create_test')
  public createTest() {
  }
}

That's all, under the hood, the user might opt-in any kind queue service, such as SQS, RabbitMQ, etc

What is the motivation / use case for changing the behavior?

I want to use custom publisher for command bus and skip default logic of command handlers with their execute method. As a custom publisher it could be whatever third party service such as bullmq, rabbitmq, etc. But before start implementation of suggested idea, I'd like to hear the idea behind the current implementation for command publisher. From my perspective it has no point where the custom command publisher might be useful. And if I want to fully migrate all my commands to external service, the current implementation forces me to follow only their design. Maybe I didn't catch the idea and considering command publisher in the wrong way, otherwise how I can use external service such as RabbitMQ for my commands?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant