Merge pull request #269 from Hexastack/268-issue-base-repositoryts-has-typing-issues

fix(api): enhance base-repository.ts typing
This commit is contained in:
Med Marrouchi 2024-10-25 19:19:45 +01:00 committed by GitHub
commit 6b387b826b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 8 deletions

View File

@ -132,7 +132,7 @@ declare module '@nestjs/event-emitter' {
>; >;
} }
/* entities hooks */ /* hooks */
interface IHookEntityOperationMap extends IHookOperationMap { interface IHookEntityOperationMap extends IHookOperationMap {
stats: TDefinition<BotStats, { entry: string }>; stats: TDefinition<BotStats, { entry: string }>;
attachment: TDefinition<Attachment>; attachment: TDefinition<Attachment>;
@ -177,6 +177,12 @@ declare module '@nestjs/event-emitter' {
user: TDefinition<User, { lastvisit: Subscriber }>; user: TDefinition<User, { lastvisit: Subscriber }>;
} }
/* entities hooks having schemas */
type IHookEntities = keyof Omit<
IHookEntityOperationMap,
keyof IHookOperationMap
>;
/** /**
* @description A constrained string type that allows specific string values while preserving type safety. * @description A constrained string type that allows specific string values while preserving type safety.
*/ */

View File

@ -6,7 +6,11 @@
* 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 { EventEmitter2 } from '@nestjs/event-emitter'; import {
EventEmitter2,
IHookEntities,
TNormalizedEvents,
} from '@nestjs/event-emitter';
import { ClassTransformOptions, plainToClass } from 'class-transformer'; import { ClassTransformOptions, plainToClass } from 'class-transformer';
import { import {
Document, Document,
@ -66,8 +70,9 @@ export abstract class BaseRepository<
return this.populate; return this.populate;
} }
getEventName(suffix: EHook): any { getEventName(suffix: EHook) {
return `hook:${this.cls.name.toLocaleLowerCase()}:${suffix}`; const entity = this.cls.name.toLocaleLowerCase();
return `hook:${entity}:${suffix}` as `hook:${IHookEntities}:${TNormalizedEvents}`;
} }
private registerLifeCycleHooks() { private registerLifeCycleHooks() {
@ -108,7 +113,7 @@ export abstract class BaseRepository<
await repository.preDelete(query, criteria); await repository.preDelete(query, criteria);
repository.emitter.emit( repository.emitter.emit(
repository.getEventName(EHook.preDelete), repository.getEventName(EHook.preDelete),
query as any, query,
criteria, criteria,
); );
}); });
@ -118,7 +123,7 @@ export abstract class BaseRepository<
await repository.postDelete(query, result); await repository.postDelete(query, result);
repository.emitter.emit( repository.emitter.emit(
repository.getEventName(EHook.postDelete), repository.getEventName(EHook.postDelete),
query as any, query,
result, result,
); );
}); });
@ -132,7 +137,7 @@ export abstract class BaseRepository<
hooks?.deleteMany.post.execute(async function (result: DeleteResult) { hooks?.deleteMany.post.execute(async function (result: DeleteResult) {
repository.emitter.emit( repository.emitter.emit(
repository.getEventName(EHook.postDelete), repository.getEventName(EHook.postDelete),
result as any, 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);
@ -146,7 +151,7 @@ export abstract class BaseRepository<
await repository.preUpdate(query, criteria, updates); await repository.preUpdate(query, criteria, updates);
repository.emitter.emit( repository.emitter.emit(
repository.getEventName(EHook.preUpdate), repository.getEventName(EHook.preUpdate),
criteria as any, criteria,
updates?.['$set'], updates?.['$set'],
); );
}); });