feat: centrelize eventEmitter

This commit is contained in:
yassinedorbozgithub
2025-03-23 16:37:40 +01:00
parent ac770154f5
commit 80f7fdf8f5
36 changed files with 105 additions and 202 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -23,7 +23,11 @@ export abstract class BaseController<
TFull extends Omit<T, P> = never,
Dto extends DtoConfig = object,
> {
constructor(protected readonly service: BaseService<T, P, TFull, Dto>) {}
eventEmitter: typeof this.service.eventEmitter;
constructor(protected readonly service: BaseService<T, P, TFull, Dto>) {
this.eventEmitter = service.eventEmitter;
}
/**
* Checks if the given populate fields are allowed based on the allowed fields list.

View File

@@ -79,13 +79,15 @@ export abstract class BaseRepository<
private readonly leanOpts = { virtuals: true, defaults: true, getters: true };
eventEmitter: EventEmitter2;
constructor(
private readonly emitter: EventEmitter2,
readonly model: Model<T>,
private readonly cls: new () => T,
protected readonly populate: P[] = [],
protected readonly clsPopulate?: new () => TFull,
) {
this.eventEmitter = new EventEmitter2();
this.registerLifeCycleHooks();
}
@@ -112,7 +114,7 @@ export abstract class BaseRepository<
hooks.validate.pre.execute(async function () {
const doc = this as HydratedDocument<T>;
await repository.preCreateValidate(doc);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preCreateValidate),
doc,
);
@@ -120,7 +122,7 @@ export abstract class BaseRepository<
hooks.validate.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreateValidate(created);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postCreateValidate),
created,
);
@@ -129,7 +131,7 @@ export abstract class BaseRepository<
hooks.save.pre.execute(async function () {
const doc = this as HydratedDocument<T>;
await repository.preCreate(doc);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preCreate),
doc,
);
@@ -137,7 +139,7 @@ export abstract class BaseRepository<
hooks.save.post.execute(async function (created: HydratedDocument<T>) {
await repository.postCreate(created);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postCreate),
created,
);
@@ -147,7 +149,7 @@ export abstract class BaseRepository<
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
const criteria = query.getQuery();
await repository.preDelete(query, criteria);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preDelete),
query,
criteria,
@@ -157,7 +159,7 @@ export abstract class BaseRepository<
hooks?.deleteOne.post.execute(async function (result: DeleteResult) {
const query = this as Query<DeleteResult, D, unknown, T, 'deleteOne'>;
await repository.postDelete(query, result);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
query,
result,
@@ -171,7 +173,7 @@ export abstract class BaseRepository<
});
hooks.deleteMany.post.execute(async function (result: DeleteResult) {
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postDelete),
result,
);
@@ -187,7 +189,7 @@ export abstract class BaseRepository<
throw new Error('Unable to run findOneAndUpdate pre hook');
}
await repository.preUpdate(query, criteria, updates);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preUpdate),
criteria,
updates?.['$set'],
@@ -202,7 +204,7 @@ export abstract class BaseRepository<
throw new Error('Unable to execute updateMany() pre-hook');
}
await repository.preUpdateMany(query, criteria, updates);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.preUpdateMany),
criteria,
updates?.['$set'],
@@ -212,7 +214,7 @@ export abstract class BaseRepository<
hooks.updateMany.post.execute(async function (updated: any) {
const query = this as Query<D, D, unknown, T, 'updateMany'>;
await repository.postUpdateMany(query, updated);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postUpdateMany),
updated,
);
@@ -227,7 +229,7 @@ export abstract class BaseRepository<
query,
plainToClass(repository.cls, updated, repository.transformOpts),
);
await repository.emitter.emitAsync(
await repository.eventEmitter.emitAsync(
repository.getEventName(EHook.postUpdate),
updated,
);
@@ -503,14 +505,14 @@ export abstract class BaseRepository<
}
await this.preUpdateValidate(filterCriteria, queryUpdates);
await this.emitter.emitAsync(
await this.eventEmitter.emitAsync(
this.getEventName(EHook.preUpdateValidate),
filterCriteria,
queryUpdates,
);
await this.postUpdateValidate(filterCriteria, queryUpdates);
await this.emitter.emitAsync(
await this.eventEmitter.emitAsync(
this.getEventName(EHook.postUpdateValidate),
filterCriteria,
queryUpdates,

View File

@@ -26,9 +26,11 @@ export abstract class BaseService<
Dto extends DtoConfig = object,
U extends Omit<T, keyof BaseSchema> = Omit<T, keyof BaseSchema>,
> {
constructor(
protected readonly repository: BaseRepository<T, P, TFull, Dto>,
) {}
eventEmitter: typeof this.repository.eventEmitter;
constructor(protected readonly repository: BaseRepository<T, P, TFull, Dto>) {
this.eventEmitter = repository.eventEmitter;
}
getRepository() {
return this.repository;

View File

@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -7,7 +7,6 @@
*/
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@@ -17,10 +16,7 @@ import { Dummy } from '../schemas/dummy.schema';
@Injectable()
export class DummyRepository extends BaseRepository<Dummy> {
constructor(
readonly eventEmitter: EventEmitter2,
@InjectModel(Dummy.name) readonly model: Model<Dummy>,
) {
super(eventEmitter, model, Dummy);
constructor(@InjectModel(Dummy.name) readonly model: Model<Dummy>) {
super(model, Dummy);
}
}