question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polymorphic Relationship

See original GitHub issue

Issue type:

[ ] question [ ] bug report [x ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ x] mysql / mariadb [ ] oracle [ x] postgres [ ] cockroachdb [x ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

8reactions
onichandamecommented, Aug 15, 2021

It is great that a 3rd party package solves the problem. But polymorphic relation is close to the core functions of an ORM in my opinion, would it make sense to merge this function into typeorm itself?

8reactions
bashleighcommented, Mar 13, 2020

I’ve been implementing a repository which handles polymorphic children and polymorphic parents. I was going to open source it but if I’m honest, I would rather implement it into typeorm!

@Entity()
export class PhotoEntity implements PolymorphicChildInterface {

  @PolymorphicParent(() => [UserEntity, MerchantEntity])
  owner: UserEntity | MerchantEntity;

  @Column()
  entityId: number; // required for PolymorphicChildInterface

  @Column()
  entityType: string; // required for PolymorphicChildInterface

}

@Entity()
export class UserEntity {

  @PolymorphicChildren(() => PhotoEntity, {
    eager: false,
  })
  photos: PhotoEntity[];

}

@Entity('merchants')
export class MerchantEntity extends AbstractEntity {
  @ManyToMany(t => UserEntity, user => user.merchants)
  users: UserEntity[];

  @PolymorphicChildren(() => PhotoEntity, {
    eager: false,
  })
  photos: PhotoEntity[];
}

These are the available options for the above decorators

export interface PolymorphicChildrenDecoratorOptionsInterface {
  primaryColumn?: string;
  hasMany?: boolean;
  cascade?: boolean;
  eager?: boolean;
  deleteBeforeUpdate?: boolean;
  entityTypeColumn?: string;
  entityTypeId?: string;
}

export interface PolymorphicParentDecoratorOptionsInterface {
  deleteBeforeUpdate?: boolean;
  primaryColumn?: string;
  hasMany?: boolean;
  cascade?: boolean;
  eager?: boolean;
  entityTypeColumn?: string;
  entityTypeId?: string;
}

A downside is I’m not currently using a reverse relationship check?

I’ve implemented this in a way outside of typeORM so I’ve just made an abstract repository that handles hydrating on {findOne, find} and saving etc. I’d love to implement this for typeORM! When I’m 100% happy with it, I’ll create a repo with it on github 😃

I did however start writing a medium post about it https://medium.com/p/96ad63cb8d0b/edit I’ve since reimplemented this entirely although some of the basic logic is the same!

I’ve implemented this so you can really customise it! You can have many different parents, many different children AND! Singular + array returns so you can have a parent of 2 types yet return one and the reverse for children. There is no overall difference for how the decorators are handled other than the default values for the options.

There’s only one thing holding me back at the moment and that’s whether to implement a race promise for singular relations or find all and determine which to return as we’d only require one entity? Not sure what decision to make! Not sure if to throw an exception if 2 entities are found? Or even just bypass the typing and return an array instead of 1 entity but I don’t want to do that!

If you’d like to see it as is I can upload it now 😃

I’m also considering creating a PolymorphicHasManyThrough type relationship for where my user can have many photos through a merchant.

Another blocker is I have created an interface:

export interface PolymorphicChildInterface {
  entityId: number;
  entityType: string;
}

But I’ve also implemented the ability (not that I’m using it currently) to change the child column names. So I’m trying to think of a possible way I could change the key names? The default for entityTypeColumn is entityType and entityIdColumn is entityId.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Polymorphic Relationships - DevDojo
Before explaining polymorphism, let's do a quick review of the three common relationships: One-to-One; One-to-Many; Many-to-Many. One-to-One.
Read more >
Polymorphic relationships in Laravel and their use cases
The general concept behind using polymorphic relationships revolves around identifying similarities between what two or more models might need ...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
Many To Many (Polymorphic). Defining Relationships. Eloquent relationships are defined as methods on your Eloquent model classes. Since relationships also serve ...
Read more >
Re-Introducing Eloquent's Polymorphic Relationships - SitePoint
A polymorphic relationship is where a model can belong to more than one other model on a single association. To clarify this, let's...
Read more >
Polymorphic association - Wikipedia
Polymorphic association is a term used in discussions of Object-Relational Mapping with respect to the problem of representing in the relational database ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found