I have a entity called "Comments", the idea is having replies for these comments. I can have also replies for replies. Example as following:
-Comment1
---Reply 1 Of Comment 1
-----Reply Of Reply 1
---Reply 2 Of Comment 1
-----Reply1 Of Reply 2
-------Reply1 Of Reply 1 of Reply 2
-Comment2
The entity is this one:
@Entity('comments') {
@PrimaryGenerated()
id: string;
@Column()
message: string;
@Column()
date: Date;
}
The approach I was thinking of is to have a "self relationship" where I could create another field maybe called "parent_id" and where I have this field filled, I consider as a reply. Any ideas of how I can do this "self relationship" or ideas of another approach?
This is an example of self referencing relation:
@Entity('comments')
export class Comment {
@PrimaryGenerated()
id: string;
@Column()
message: string;
@Column()
date: Date;
@Column()
parentId: number;
@ManyToOne(type => Comment, comment => comment.children)
@JoinColumn({ name: "parentId" })
parent: Comment;
@OneToMany(type => Category, comment => comment.parent)
children: Comment[];
}
Also you can look at Tree Entities as another approach.