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

feat(logs): Log duplicate handlers #1438

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/command-bus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Type } from '@nestjs/common';
import { Injectable, Logger, Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import 'reflect-metadata';
import {
Expand Down Expand Up @@ -26,6 +26,7 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
{
private handlers = new Map<string, ICommandHandler<CommandBase>>();
private _publisher: ICommandPublisher<CommandBase>;
private readonly _logger = new Logger(CommandBus.name);

constructor(private readonly moduleRef: ModuleRef) {
super();
Expand Down Expand Up @@ -64,7 +65,17 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
return handler.execute(command);
}

bind<T extends CommandBase>(handler: ICommandHandler<T>, id: string) {
bind<T extends CommandBase>(
handler: ICommandHandler<T>,
{ id, name }: CommandMetadata,
) {
if (this.handlers.has(id)) {
const previousHandlerName = this.handlers.get(id).constructor.name;
const handlerName = handler.constructor.name;
this._logger.warn(
`Multiple handlers for command ${name}. Repleacing ${previousHandlerName} with ${handlerName}.`,
);
}
this.handlers.set(id, handler);
}

Expand All @@ -77,11 +88,11 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
if (!instance) {
return;
}
const target = this.reflectCommandId(handler);
if (!target) {
const commandMetadata = this.reflectCommandMetadata(handler);
if (!commandMetadata) {
throw new InvalidCommandHandlerException();
}
this.bind(instance as ICommandHandler<CommandBase>, target);
this.bind(instance as ICommandHandler<CommandBase>, commandMetadata);
}

private getCommandId(command: CommandBase): string {
Expand All @@ -97,16 +108,12 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
return commandMetadata.id;
}

private reflectCommandId(handler: CommandHandlerType): string | undefined {
private reflectCommandMetadata(handler: CommandHandlerType): CommandMetadata {
const command: Type<ICommand> = Reflect.getMetadata(
COMMAND_HANDLER_METADATA,
handler,
);
const commandMetadata: CommandMetadata = Reflect.getMetadata(
COMMAND_METADATA,
command,
);
return commandMetadata.id;
return Reflect.getMetadata(COMMAND_METADATA, command);
}

private useDefaultPublisher() {
Expand Down
9 changes: 7 additions & 2 deletions src/decorators/command-handler.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import { ICommand } from '../index';
import { COMMAND_HANDLER_METADATA, COMMAND_METADATA } from './constants';
import { v4 } from 'uuid';
import { Type } from '@nestjs/common';

/**
* Decorator that marks a class as a Nest command handler. A command handler
Expand All @@ -13,10 +14,14 @@ import { v4 } from 'uuid';
*
* @see https://docs.nestjs.com/recipes/cqrs#commands
*/
export const CommandHandler = (command: ICommand | (new (...args: any[]) => ICommand)): ClassDecorator => {
export const CommandHandler = (command: Type<ICommand>): ClassDecorator => {
return (target: object) => {
if (!Reflect.hasOwnMetadata(COMMAND_METADATA, command)) {
Reflect.defineMetadata(COMMAND_METADATA, { id: v4() }, command);
Reflect.defineMetadata(
COMMAND_METADATA,
{ id: v4(), name: command.name },
command,
);
}
Reflect.defineMetadata(COMMAND_HANDLER_METADATA, command, target);
};
Expand Down
9 changes: 7 additions & 2 deletions src/decorators/query-handler.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import { IQuery } from '../interfaces';
import { QUERY_HANDLER_METADATA, QUERY_METADATA } from './constants';
import { v4 } from 'uuid';
import { Type } from '@nestjs/common';

/**
* Decorator that marks a class as a Nest query handler. A query handler
Expand All @@ -13,10 +14,14 @@ import { v4 } from 'uuid';
*
* @see https://docs.nestjs.com/recipes/cqrs#queries
*/
export const QueryHandler = (query: IQuery): ClassDecorator => {
export const QueryHandler = (query: Type<IQuery>): ClassDecorator => {
return (target: object) => {
if (!Reflect.hasOwnMetadata(QUERY_METADATA, query)) {
Reflect.defineMetadata(QUERY_METADATA, { id: v4() }, query);
Reflect.defineMetadata(
QUERY_METADATA,
{ id: v4(), name: query.name },
query,
);
}
Reflect.defineMetadata(QUERY_HANDLER_METADATA, query, target);
};
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/commands/command-metadata.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface CommandMetadata {
id: string;
}
name: string;
}
3 changes: 2 additions & 1 deletion src/interfaces/queries/query-metadata.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface QueryMetadata {
id: string;
}
name: string;
}
33 changes: 20 additions & 13 deletions src/query-bus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable, Type } from '@nestjs/common';
import { Injectable, Logger, Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import 'reflect-metadata';
import { QUERY_HANDLER_METADATA, QUERY_METADATA } from './decorators/constants';
Expand Down Expand Up @@ -27,6 +27,7 @@ export class QueryBus<QueryBase extends IQuery = IQuery>
{
private handlers = new Map<string, IQueryHandler<QueryBase, IQueryResult>>();
private _publisher: IQueryPublisher<QueryBase>;
private readonly _logger = new Logger(QueryBus.name);

constructor(private readonly moduleRef: ModuleRef) {
super();
Expand Down Expand Up @@ -69,9 +70,16 @@ export class QueryBus<QueryBase extends IQuery = IQuery>

bind<T extends QueryBase, TResult = any>(
handler: IQueryHandler<T, TResult>,
queryId: string,
{ id, name }: QueryMetadata,
) {
this.handlers.set(queryId, handler);
if (this.handlers.has(id)) {
const previousHandlerName = this.handlers.get(id).constructor.name;
const handlerName = handler.constructor.name;
this._logger.warn(
`Multiple handlers for query ${name}. Repleacing ${previousHandlerName} with ${handlerName}.`,
);
}
this.handlers.set(id, handler);
}

register(handlers: QueryHandlerType<QueryBase>[] = []) {
Expand All @@ -83,11 +91,14 @@ export class QueryBus<QueryBase extends IQuery = IQuery>
if (!instance) {
return;
}
const target = this.reflectQueryId(handler);
if (!target) {
const queryMetadata = this.reflectQueryMetadata(handler);
if (!queryMetadata) {
throw new InvalidQueryHandlerException();
}
this.bind(instance as IQueryHandler<QueryBase, IQueryResult>, target);
this.bind(
instance as IQueryHandler<QueryBase, IQueryResult>,
queryMetadata,
);
}

private getQueryId(query: QueryBase): string {
Expand All @@ -103,18 +114,14 @@ export class QueryBus<QueryBase extends IQuery = IQuery>
return queryMetadata.id;
}

private reflectQueryId(
private reflectQueryMetadata(
handler: QueryHandlerType<QueryBase>,
): string | undefined {
): QueryMetadata {
const query: Type<QueryBase> = Reflect.getMetadata(
QUERY_HANDLER_METADATA,
handler,
);
const queryMetadata: QueryMetadata = Reflect.getMetadata(
QUERY_METADATA,
query,
);
return queryMetadata.id;
return Reflect.getMetadata(QUERY_METADATA, query);
}

private useDefaultPublisher() {
Expand Down