You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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!
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:
For reasons unclear to me the
entityId
andentityType
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
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.
The text was updated successfully, but these errors were encountered: