Search code examples
angulartypescriptnestjsbackendtypeorm

afterUpdate event typeORM - NestJS


I would like to insert an entry in another table after each update of my data (log table) I've created an EntitySubscriberInterface to do this. The event is correctly triggered but my entity array don't include the id which is updated.

 async afterUpdate(event: UpdateEvent<any>) {
    console.log(event.entity);
    const book = event.entity;// entity should be a book
    const authorId = book.authorId;
    console.log(newAuthorId);       
// this.myService.insertHistory(authorId , event.entity);

}

If anybody have an idea. Thanks


Solution

  • With afterInsert method you have this information. With afterUpdate you have to query the databse to get your entity.

    const lot = await event.connection
          .getRepository(book)
          .findOne(event.entity.id);