Skip to content

Commit

Permalink
Merge pull request #269 from Hexastack/268-issue-base-repositoryts-ha…
Browse files Browse the repository at this point in the history
…s-typing-issues

fix(api): enhance base-repository.ts typing
  • Loading branch information
marrouchi authored Oct 25, 2024
2 parents a83b104 + 8c0c1c9 commit 6b387b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 7 additions & 1 deletion api/src/eventemitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ declare module '@nestjs/event-emitter' {
>;
}

/* entities hooks */
/* hooks */
interface IHookEntityOperationMap extends IHookOperationMap {
stats: TDefinition<BotStats, { entry: string }>;
attachment: TDefinition<Attachment>;
Expand Down Expand Up @@ -177,6 +177,12 @@ declare module '@nestjs/event-emitter' {
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.
*/
Expand Down
19 changes: 12 additions & 7 deletions api/src/utils/generics/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/

import { EventEmitter2 } from '@nestjs/event-emitter';
import {
EventEmitter2,
IHookEntities,
TNormalizedEvents,
} from '@nestjs/event-emitter';
import { ClassTransformOptions, plainToClass } from 'class-transformer';
import {
Document,
Expand Down Expand Up @@ -66,8 +70,9 @@ export abstract class BaseRepository<
return this.populate;
}

getEventName(suffix: EHook): any {
return `hook:${this.cls.name.toLocaleLowerCase()}:${suffix}`;
getEventName(suffix: EHook) {
const entity = this.cls.name.toLocaleLowerCase();
return `hook:${entity}:${suffix}` as `hook:${IHookEntities}:${TNormalizedEvents}`;
}

private registerLifeCycleHooks() {
Expand Down Expand Up @@ -108,7 +113,7 @@ export abstract class BaseRepository<
await repository.preDelete(query, criteria);
repository.emitter.emit(
repository.getEventName(EHook.preDelete),
query as any,
query,
criteria,
);
});
Expand All @@ -118,7 +123,7 @@ export abstract class BaseRepository<
await repository.postDelete(query, result);
repository.emitter.emit(
repository.getEventName(EHook.postDelete),
query as any,
query,
result,
);
});
Expand All @@ -132,7 +137,7 @@ export abstract class BaseRepository<
hooks?.deleteMany.post.execute(async function (result: DeleteResult) {
repository.emitter.emit(
repository.getEventName(EHook.postDelete),
result as any,
result,
);
const query = this as Query<DeleteResult, D, unknown, T, 'deleteMany'>;
await repository.postDelete(query, result);
Expand All @@ -146,7 +151,7 @@ export abstract class BaseRepository<
await repository.preUpdate(query, criteria, updates);
repository.emitter.emit(
repository.getEventName(EHook.preUpdate),
criteria as any,
criteria,
updates?.['$set'],
);
});
Expand Down

0 comments on commit 6b387b8

Please sign in to comment.