Entity Listeners and Subscribers

What is an Entity Listener

Any of your entities can have methods with custom logic that listen to specific entity events.You must mark those methods with special decorators depending on what event you want to listen to.

@AfterLoad

You can define a method with any name in entity and mark it with @AfterLoadand TypeORM will call it each time the entityis loaded using QueryBuilder or repository/manager find methods.Example:

  1. @Entity()
  2. export class Post {
  3. @AfterLoad()
  4. updateCounters() {
  5. if (this.likesCount === undefined)
  6. this.likesCount = 0;
  7. }
  8. }

@BeforeInsert

You can define a method with any name in entity and mark it with @BeforeInsertand TypeORM will call it before the entity is inserted using repository/manager save.Example:

  1. @Entity()
  2. export class Post {
  3. @BeforeInsert()
  4. updateDates() {
  5. this.createdDate = new Date();
  6. }
  7. }

@AfterInsert

You can define a method with any name in entity and mark it with @AfterInsertand TypeORM will call it after the entity is inserted using repository/manager save.Example:

  1. @Entity()
  2. export class Post {
  3. @AfterInsert()
  4. resetCounters() {
  5. this.counters = 0;
  6. }
  7. }

@BeforeUpdate

You can define a method with any name in the entity and mark it with @BeforeUpdateand TypeORM will call it before an existing entity is updated using repository/manager save. Keep in mind, however, that this will occur only when information is changed in the model. If you run save without modifying anything from the model, @BeforeUpdate and @AfterUpdate will not run.Example:

  1. @Entity()
  2. export class Post {
  3. @BeforeUpdate()
  4. updateDates() {
  5. this.updatedDate = new Date();
  6. }
  7. }

@AfterUpdate

You can define a method with any name in the entity and mark it with @AfterUpdateand TypeORM will call it after an existing entity is updated using repository/manager save.Example:

  1. @Entity()
  2. export class Post {
  3. @AfterUpdate()
  4. updateCounters() {
  5. this.counter = 0;
  6. }
  7. }

@BeforeRemove

You can define a method with any name in the entity and mark it with @BeforeRemoveand TypeORM will call it before a entity is removed using repository/manager remove.Example:

  1. @Entity()
  2. export class Post {
  3. @BeforeRemove()
  4. updateStatus() {
  5. this.status = "removed";
  6. }
  7. }

@AfterRemove

You can define a method with any name in the entity and mark it with @AfterRemoveand TypeORM will call it after the entity is removed using repository/manager remove.Example:

  1. @Entity()
  2. export class Post {
  3. @AfterRemove()
  4. updateStatus() {
  5. this.status = "removed";
  6. }
  7. }

What is a Subscriber

Marks a class as an event subscriber which can listen to specific entity events or any entity events.Events are firing using QueryBuilder and repository/manager methods.Example:

  1. @EventSubscriber()
  2. export class PostSubscriber implements EntitySubscriberInterface<Post> {
  3. /**
  4. * Indicates that this subscriber only listen to Post events.
  5. */
  6. listenTo() {
  7. return Post;
  8. }
  9. /**
  10. * Called before post insertion.
  11. */
  12. beforeInsert(event: InsertEvent<Post>) {
  13. console.log(`BEFORE POST INSERTED: `, event.entity);
  14. }
  15. }

You can implement any method from EntitySubscriberInterface.To listen to any entity you just omit listenTo method and use any:

  1. @EventSubscriber()
  2. export class PostSubscriber implements EntitySubscriberInterface {
  3. /**
  4. * Called before entity insertion.
  5. */
  6. beforeInsert(event: InsertEvent<any>) {
  7. console.log(`BEFORE ENTITY INSERTED: `, event.entity);
  8. }
  9. }