TypeORM eager relations in afterInsert and afterUpdate

In TypeORM, I have a subscriber (class implementing EntitySubscriberInterface) which listens on afterInsert and afterUpdate events. Here is a minimal entity to reproduce my problem:

@Entity()
export class MainEntity {
  @ManyToOne(type => Target, { eager: true })
  @JoinColumn({ name: 'targetId' })
  target: Target;
  @Column()
  targetId: string;
}

The eager relation works (loads) when using mainEntity.find(), but if I use:

   mainEntity.save({targetId: 'valid_id'}).then((result) => console.log(result))

The relation for .target is not resolved, neither in the then() callback or in the afterInsert() event listener. The targetId field has the correct value, but target is undefined.

Is there a way to tell .save() to load eager relations or do I need to get back the entity from the database using its ID in order to get them?

I also tried to use insert() instead of save() just in case it was a problem with save(), to no avail.

Thanks!



Read more here: https://stackoverflow.com/questions/64014249/typeorm-eager-relations-in-afterinsert-and-afterupdate

Content Attribution

This content was originally published by alexandre_okidoo at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: