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

Possible bug: polymorphic entity creation not working #14

Closed
nmokkenstorm opened this issue Jun 10, 2021 · 1 comment
Closed

Possible bug: polymorphic entity creation not working #14

nmokkenstorm opened this issue Jun 10, 2021 · 1 comment

Comments

@nmokkenstorm
Copy link

nmokkenstorm commented Jun 10, 2021

Hey, first of all massive thanks for creating this extension. I've grown up with PHP and made Laravel my superpower, so having this Eloquent-inspired feature available in Typeorm has been a great experience so far.

I'm currently having an issue with creating entities using the polymorphic relationships provided by this package, but I've only been doing NestJS/Typeorm for about half a year now, so it's very possible that it is due to my lack of experience with the tooling that I'm running into issues. I'd be more than willing to look into fixing the issue, but I'd like confirmation that it's an actual issue first.

I have the following files/classes available, slightly edited for brevity:


@EntityRepository(Notification)
export class NotificationRepository extends AbstractPolymorphicRepository<Notification> {}

export type Notifiable = Foo | Bar | Baz

@Entity('notifications')
export class Notification  extends TimestampedBaseEntity implements PolymorphicChildInterface {

  @Column()
  entityId: string

  @Column()
  entityType: string

  // TODO: use Notifiable here too
  @PolymorphicParent(() => [Foo, Bar, Baz])
  notifiable: Notifiable

  @ManyToOne(() => User)
  @JoinColumn({ name: 'user_id' })
  @Expose()  user: User
}

@Module({
  imports: [
    TypeOrmModule.forFeature([Notification, User, Foo, Bar, Baz]),
    // specifically not using  NotificationRepository in TypeOrm.forFeature here, that seems to be a documentation error?
    UserModule
  ],
  controllers: [NotificationsController],
  providers: [NotificationsService],
  exports: [NotificationsService]
})
export class NotificationsModule {}

@Injectable()
export class NotificationsService {

  constructor(private readonly notificationsRepository: NotificationRepository) {}
  
  notify(user: User, notifiable: Notifiable): Promise<Notification> {
    return this.notificationsRepository.create({
      user,
      // notifiable, HACK: we shouldn't need this
      entityId: notifiable.id,
      entityType: notifiable.constructor.name
    }).save()
  }
}

For reasons unclear to me the entityId and entityType here are not set automatically when I add the notifiable directly, manually specifying them does work. I tried several permutations of setting the notifiable here, including manually instantiating the entity instead of using the repository, but no dice. I more or less stole this approach from here. As an aside, the readme.md suggests importing the Repository from Typeorm, but that gave me errors. For completeness sake I added it to the sample code.

For what it's worth, I'm using

"@nestjs/typeorm": "^7.1.5"
 "typeorm": "^0.2.29"
"typescript": "^4.0.5"

I'd love to look into fixing this issue, but as mentioned I'm unsure if this is something that's on our end or something that needs to be fixed here.

@bashleigh
Copy link
Owner

I would try this instead

return this.notificationsRepository.save(this.notificationsRepository.create({
      user,
    }))

calling create().save() is using the entity's save method and I don't think typeorm returns the correct repository from the entity manager? This is purely a guess but I've had no issues using the above method!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants