diff --git a/docs/module/orm.md b/docs/module/orm.md index 07aa250..2776099 100644 --- a/docs/module/orm.md +++ b/docs/module/orm.md @@ -137,39 +137,6 @@ upsertOne(): Promise; commit(): Promise; ``` -### Creating an Subscriber - -If you would like to create hooks when triggering certain operations, it is possible by defining an injectable subscriber: - -```ts -import { Injectable, LoggerService } from '@bechara/nestjs-core'; -import { EntityManager, OrmSubscriber, OrmSubscriberParams } from '@bechara/nestjs-orm'; - -import { User } from './user.entity'; - -@Injectable() -export class UserSubscriber implements OrmSubscriber { - - public constructor( - private readonly entityManager: EntityManager, - private readonly loggerService: LoggerService, - ) { - entityManager.getEventManager().registerSubscriber(this); - } - - /** - * Before update hook example. - * @param params - */ - public beforeUpdate(params: OrmSubscriberParams): Promise { - const { changeSet } = params; - this.loggerService.warning('beforeUpdate: changeSet', changeSet); - return; - } - -} -``` - ### Creating an Entity Controller diff --git a/source/orm/orm.interface.ts b/source/orm/orm.interface.ts index 82abc21..a3f87da 100644 --- a/source/orm/orm.interface.ts +++ b/source/orm/orm.interface.ts @@ -1,4 +1,4 @@ -import { EntityData, EventArgs, FilterQuery, FindOptions } from '@mikro-orm/core'; +import { EntityData, FilterQuery, FindOptions } from '@mikro-orm/core'; import { AutoPath } from '@mikro-orm/core/typings'; import { EntityName, MikroOrmModuleOptions } from '@mikro-orm/nestjs'; import { ModuleMetadata } from '@nestjs/common'; @@ -12,8 +12,6 @@ export type OrmReadParams = FilterQuery; export type OrmReadPaginatedParams = FilterQuery & OrmPageReadDto; -export type OrmSubscriberParams = EventArgs; - export interface OrmAsyncModuleOptions extends Pick { disableEntityScan?: boolean; entities?: any[]; @@ -38,15 +36,6 @@ export interface OrmExceptionHandlerParams { error: Error; } -export interface OrmSubscriberChangeset { - before?: EntityData; - after: EntityData; -} - -export interface OrmSubscriberOptions { - entities: any; -} - export interface OrmUpdateParams { entity: Entity; data: EntityData; diff --git a/source/orm/orm.repository.spec.ts b/source/orm/orm.repository.spec.ts index a5d5bfa..5e4cc7d 100644 --- a/source/orm/orm.repository.spec.ts +++ b/source/orm/orm.repository.spec.ts @@ -97,18 +97,6 @@ export const OrmRepositorySpec = ({ type, port, user }): void => { }); }); - describe('OrmSubscriber', () => { - it('should apply beforeCreate hook', async () => { - await userRepository.create({ - name: 'After Create Hook', - age: 10, - }); - - const [ user ] = await userRepository.readBy({ name: 'After Hook' }); - expect(user.name).toBe('After Hook'); - }); - }); - describe('OrmBaseRepository', () => { it('should build an entity and persist only after committing', async () => { const user = userRepository.buildOne({ diff --git a/source/orm/orm.subscriber.ts b/source/orm/orm.subscriber.ts deleted file mode 100644 index f0e9923..0000000 --- a/source/orm/orm.subscriber.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { EntityManager, EntityName, EventSubscriber } from '@mikro-orm/core'; - -import { OrmSubscriberChangeset, OrmSubscriberOptions, OrmSubscriberParams } from './orm.interface'; - -export abstract class OrmSubscriber implements EventSubscriber { - - public constructor( - protected readonly entityManager: EntityManager, - protected readonly ormSubscriberOptions: OrmSubscriberOptions, - ) { - entityManager.getEventManager().registerSubscriber(this); - } - - /** - * Filter target entities to subscribe. - */ - public getSubscribedEntities(): EntityName[] { - const { entities } = this.ormSubscriberOptions; - return Array.isArray(entities) ? entities : [ entities ]; - } - - /** - * Get entity changeset, useful for 'update' hooks. - * @param params - */ - protected getChangeset(params: OrmSubscriberParams): OrmSubscriberChangeset { - const { changeSet } = params; - const { entity, originalEntity } = changeSet; - - return { - before: originalEntity, - after: entity, - }; - } - -} diff --git a/test/user/user.module.ts b/test/user/user.module.ts index e31364b..7953f3f 100644 --- a/test/user/user.module.ts +++ b/test/user/user.module.ts @@ -1,14 +1,10 @@ import { Module } from '@nestjs/common'; import { UserController } from './user.controller'; -import { UserSubscriber } from './user.subscriber'; @Module({ controllers: [ UserController, ], - providers: [ - UserSubscriber, - ], }) export class UserModule { } diff --git a/test/user/user.subscriber.ts b/test/user/user.subscriber.ts deleted file mode 100644 index 5d41e61..0000000 --- a/test/user/user.subscriber.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { EntityManager } from '@mikro-orm/core'; -import { Injectable } from '@nestjs/common'; - -import { OrmSubscriberParams } from '../../source/orm/orm.interface'; -import { OrmSubscriber } from '../../source/orm/orm.subscriber'; -import { User } from './user.entity'; - -@Injectable() -export class UserSubscriber extends OrmSubscriber { - - public constructor( - protected readonly entityManager: EntityManager, - ) { - super(entityManager, { - entities: User, - }); - } - - /** - * Truncate name if greater than 3 words. - * @param params - */ - public beforeCreate(params: OrmSubscriberParams): void { - const { entity } = params; - const { name } = entity; - const names = name.split(' '); - - if (names.length > 2) { - entity.name = `${names[0]} ${names.at(-1)}`; - } - } - -}