fix: inject eventEmitter and read triggred eventName

This commit is contained in:
yassinedorbozgithub 2025-03-24 01:48:22 +01:00
parent 80f7fdf8f5
commit 3b1738caf0
2 changed files with 22 additions and 6 deletions

View File

@ -6,6 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { Inject } from '@nestjs/common';
import { import {
EventEmitter2, EventEmitter2,
IHookEntities, IHookEntities,
@ -79,7 +80,8 @@ export abstract class BaseRepository<
private readonly leanOpts = { virtuals: true, defaults: true, getters: true }; private readonly leanOpts = { virtuals: true, defaults: true, getters: true };
eventEmitter: EventEmitter2; @Inject(EventEmitter2)
readonly eventEmitter: EventEmitter2;
constructor( constructor(
readonly model: Model<T>, readonly model: Model<T>,
@ -87,7 +89,6 @@ export abstract class BaseRepository<
protected readonly populate: P[] = [], protected readonly populate: P[] = [],
protected readonly clsPopulate?: new () => TFull, protected readonly clsPopulate?: new () => TFull,
) { ) {
this.eventEmitter = new EventEmitter2();
this.registerLifeCycleHooks(); this.registerLifeCycleHooks();
} }
@ -170,15 +171,21 @@ export abstract class BaseRepository<
const query = this as Query<DeleteResult, D, unknown, T, 'deleteMany'>; const query = this as Query<DeleteResult, D, unknown, T, 'deleteMany'>;
const criteria = query.getQuery(); const criteria = query.getQuery();
await repository.preDelete(query, criteria); await repository.preDelete(query, criteria);
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preDelete),
query,
criteria,
);
}); });
hooks.deleteMany.post.execute(async function (result: DeleteResult) { hooks.deleteMany.post.execute(async function (result: DeleteResult) {
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
result,
);
const query = this as Query<DeleteResult, D, unknown, T, 'deleteMany'>; const query = this as Query<DeleteResult, D, unknown, T, 'deleteMany'>;
await repository.postDelete(query, result); await repository.postDelete(query, result);
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
query,
result,
);
}); });
hooks.findOneAndUpdate.pre.execute(async function () { hooks.findOneAndUpdate.pre.execute(async function () {

View File

@ -330,11 +330,18 @@ declare module '@nestjs/event-emitter' {
: `hook:${G}:${TNormalizedOrCustomized<G>}` : `hook:${G}:${TNormalizedOrCustomized<G>}`
: never; : never;
type TEventName = Exclude<
customEvent<EventNamespaces | ConstrainedString>,
`${string}:*`
>;
interface ListenerFn<G extends EventNamespaces | ConstrainedString> { interface ListenerFn<G extends EventNamespaces | ConstrainedString> {
(value: EventValueOf<G>, ...values: any[]): void; (value: EventValueOf<G>, ...values: any[]): void;
} }
class EventEmitter2 { class EventEmitter2 {
constructor(options?: ConstructorOptions);
emit<G extends EventNamespaces | ConstrainedString, H extends G>( emit<G extends EventNamespaces | ConstrainedString, H extends G>(
customEvent: customEvent<G>, customEvent: customEvent<G>,
value: EventValueOf<H>, value: EventValueOf<H>,
@ -396,6 +403,8 @@ declare module '@nestjs/event-emitter' {
customEvent: customEvent<G>, customEvent: customEvent<G>,
listener: ListenerFn<H>, listener: ListenerFn<H>,
): this; ): this;
public readonly event: TEventName;
} }
declare type OnEventMethodDecorator< declare type OnEventMethodDecorator<