Skip to content

Commit

Permalink
fix(orm): drop unreliable subscriber support
Browse files Browse the repository at this point in the history
  • Loading branch information
etienne-bechara committed Nov 21, 2024
1 parent edb1579 commit 98c0323
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 130 deletions.
33 changes: 0 additions & 33 deletions docs/module/orm.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,6 @@ upsertOne(): Promise<Entity>;
commit(): Promise<void>;
```

### 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<User> {

public constructor(
private readonly entityManager: EntityManager,
private readonly loggerService: LoggerService,
) {
entityManager.getEventManager().registerSubscriber(this);
}

/**
* Before update hook example.
* @param params
*/
public beforeUpdate(params: OrmSubscriberParams<User>): Promise<void> {
const { changeSet } = params;
this.loggerService.warning('beforeUpdate: changeSet', changeSet);
return;
}

}
```


### Creating an Entity Controller

Expand Down
13 changes: 1 addition & 12 deletions source/orm/orm.interface.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -12,8 +12,6 @@ export type OrmReadParams<T> = FilterQuery<T>;

export type OrmReadPaginatedParams<T> = FilterQuery<T> & OrmPageReadDto;

export type OrmSubscriberParams<Entity> = EventArgs<Entity>;

export interface OrmAsyncModuleOptions extends Pick<ModuleMetadata, 'imports'> {
disableEntityScan?: boolean;
entities?: any[];
Expand All @@ -38,15 +36,6 @@ export interface OrmExceptionHandlerParams {
error: Error;
}

export interface OrmSubscriberChangeset<Entity> {
before?: EntityData<Entity>;
after: EntityData<Entity>;
}

export interface OrmSubscriberOptions {
entities: any;
}

export interface OrmUpdateParams<Entity> {
entity: Entity;
data: EntityData<Entity>;
Expand Down
12 changes: 0 additions & 12 deletions source/orm/orm.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
36 changes: 0 additions & 36 deletions source/orm/orm.subscriber.ts

This file was deleted.

4 changes: 0 additions & 4 deletions test/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -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 { }
33 changes: 0 additions & 33 deletions test/user/user.subscriber.ts

This file was deleted.

0 comments on commit 98c0323

Please sign in to comment.