Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for entity independent hooks/subscribers #516

Closed
carmas123 opened this issue Apr 27, 2020 · 2 comments
Closed

Add support for entity independent hooks/subscribers #516

carmas123 opened this issue Apr 27, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@carmas123
Copy link

Hi, I want to migrate my app from TypeORM to MikroORM. Now I've a problem with event listening.
In TypeORM I've a EntitySubscriberInterface injectable by @subscriber() that hook all events afterUpdate, beforeUpdate or other into an external handler and not directly into the entity. This is very useful to manage some events from external.

There is an alternative into MikroORM?

@carmas123 carmas123 added the enhancement New feature or request label Apr 27, 2020
@B4nan
Copy link
Member

B4nan commented Apr 27, 2020

As discussed on slack, you could use base entity and define the hooks there, so they will be fired for all entities that extend this base entity.

I am planning to add support for something similar to EntitySubscriber in v4 (not necessarily in 4.0 though).

@B4nan B4nan changed the title Migration from TypeORM Add support for entity independent hooks/subscribers May 7, 2020
@B4nan B4nan added this to the 4.0 milestone May 7, 2020
@B4nan B4nan mentioned this issue May 7, 2020
46 tasks
@B4nan B4nan self-assigned this Jun 20, 2020
B4nan added a commit that referenced this issue Jun 21, 2020
Use `EventSubscriber` to hook to multiple entities or if you do not want to pollute
the entity prototype. All methods are optional, if you omit the `getSubscribedEntities()`
method, it means you are subscribing to all entities.

```typescript
@subscriber()
export class EverythingSubscriber implements EventSubscriber {
  async afterCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  onInit<T>(args: EventArgs<T>): void { ... }
}
```

Closes #516
B4nan added a commit that referenced this issue Jun 21, 2020
Use `EventSubscriber` to hook to multiple entities or if you do not want to pollute
the entity prototype. All methods are optional, if you omit the `getSubscribedEntities()`
method, it means you are subscribing to all entities.

```typescript
@subscriber()
export class EverythingSubscriber implements EventSubscriber {
  async afterCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  onInit<T>(args: EventArgs<T>): void { ... }
}
```

Closes #516
@B4nan
Copy link
Member

B4nan commented Jun 21, 2020

Closing as implemented in v4 via #614, subscribe to #527 for updates (this will be part of 4.0.0-alpha.4).

@B4nan B4nan closed this as completed Jun 21, 2020
B4nan added a commit that referenced this issue Aug 2, 2020
Use `EventSubscriber` to hook to multiple entities or if you do not want to pollute
the entity prototype. All methods are optional, if you omit the `getSubscribedEntities()`
method, it means you are subscribing to all entities.

```typescript
@subscriber()
export class EverythingSubscriber implements EventSubscriber {
  async afterCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  onInit<T>(args: EventArgs<T>): void { ... }
}
```

Closes #516
B4nan added a commit that referenced this issue Aug 9, 2020
Use `EventSubscriber` to hook to multiple entities or if you do not want to pollute
the entity prototype. All methods are optional, if you omit the `getSubscribedEntities()`
method, it means you are subscribing to all entities.

```typescript
@subscriber()
export class EverythingSubscriber implements EventSubscriber {
  async afterCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async afterUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeCreate<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeDelete<T>(args: EventArgs<T>): Promise<void> { ... }
  async beforeUpdate<T>(args: EventArgs<T>): Promise<void> { ... }
  onInit<T>(args: EventArgs<T>): void { ... }
}
```

Closes #516
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants