Get a Quote Right Now

Edit Template

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.

Note: Do not make any database calls within a listener, opt for subscribers instead.

@AfterLoad

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

@Entity()
export class Post {
    @AfterLoad()
    updateCounters() {
        if (this.likesCount === undefined) this.likesCount = 0
    }
}

 @BeforeInsert

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

@Entity()
export class Post {
    @BeforeInsert()
    updateDates() {
        this.createdDate = new Date()
    }
}

@AfterInsert

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

@Entity()
export class Post {
    @AfterInsert()
    resetCounters() {
        this.counters = 0
    }
}

@BeforeUpdate

You can define a method with any name in the entity and mark it with @BeforeUpdate and 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:

@Entity()
export class Post {
    @BeforeUpdate()
    updateDates() {
        this.updatedDate = new Date()
    }
}

@AfterUpdate

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

@Entity()
export class Post {
    @AfterUpdate()
    updateCounters() {
        this.counter = 0
    }
}

@BeforeRemove

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

@Entity()
export class Post {
    @BeforeRemove()
    updateStatus() {
        this.status = "removed"
    }
}

@AfterRemove

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

@Entity()
export class Post {
    @AfterRemove()
    updateStatus() {
        this.status = "removed"
    }
}

 @BeforeSoftRemove

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

@Entity()
export class Post {
    @BeforeSoftRemove()
    updateStatus() {
        this.status = "soft-removed"
    }
}

@AfterSoftRemove

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

@Entity()
export class Post {
    @AfterSoftRemove()
    updateStatus() {
        this.status = "soft-removed"
    }
}

@BeforeRecover

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

@Entity()
export class Post {
    @BeforeRecover()
    updateStatus() {
        this.status = "recovered"
    }
}

@AfterRecover

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

@Entity()
export class Post {
    @AfterRecover()
    updateStatus() {
        this.status = "recovered"
    }
}

Share

Leave a Reply

Your email address will not be published. Required fields are marked *