Union type in typeorm relation for nested comments

Viewed 2886

I am trying to create kind of CRUD-blog with posts, users and comments. And would like to have 2-level comments like ones on Youtube: top-level comments are attached to video/post and replies are attached to top-level comments. So top-level comments have post as their parent and replies have top-level comment as their parent.

Does it meant that I have to use different entity classes(and thus different tables) for top-level comments and replies? Can typeorm handle union type as parent?

@Entity()
export class Comment extends CreatableEntity {

  @Column({ type: 'text' })
  content: string

  @ManyToOne(type => User, user => user.comments, {
    nullable: false,
  })
  author: User

  @ManyToOne(type => Post | Comment, parent => parentcomments, {
    nullable: false,
  })
  parent: Post | Comment

0 Answers
Related