ฉันมีโมเดลดังต่อไปนี้:
User
, Customer
,Comment
ผู้ใช้สามารถแสดงความคิดเห็นในCustomer
ผู้ใช้สามารถตอบกลับความคิดเห็นของผู้ใช้คนอื่นไม่ จำกัด ซ้ำ
ฉันได้ทำสิ่งนี้แล้ว แต่ จำกัด ไว้เพียงคำตอบเดียวและฉันต้องการได้รับคำตอบทั้งหมด NESTED:
public async getCommentsForCustomerId(customerId: string): Promise<CustomerComment[]> {
return this.find({where: {customer: {id: customerId}, parentComment: null}, relations: ['childComments']});
}
อย่างไรก็ตามคำตอบที่ฉันได้รับนั้นซ้อนกันในระดับเดียวเท่านั้น:
[
{
"id": "7b5b654a-efb0-4afa-82ee-c00c38725072",
"content": "test",
"created_at": "2019-12-03T15:14:48.000Z",
"updated_at": "2019-12-03T15:14:49.000Z",
"childComments": [
{
"id": "7b5b654a-efb0-4afa-82ee-c00c38725073",
"content": "test reply",
"created_at": "2019-12-03T15:14:48.000Z",
"updated_at": "2019-12-03T15:14:49.000Z",
"parentCommentId": "7b5b654a-efb0-4afa-82ee-c00c38725072"
}
]
}
]
ฉันจะสร้างเคียวรีเพื่อวางซ้อนเคียวรีในรูปแบบทั้งหมดได้อย่างไร?
ข้อกำหนดเอนทิตี(ลูกค้าหมายเหตุเปลี่ยนชื่อเป็นลูกค้าเป้าหมาย) :
@Entity('leads_comments')
export class LeadComment {
@PrimaryGeneratedColumn('uuid')
id: string;
@ManyToOne(type => LeadComment, comment => comment.childComments, {nullable: true})
parentComment: LeadComment;
@OneToMany(type => LeadComment, comment => comment.parentComment)
@JoinColumn({name: 'parentCommentId'})
childComments: LeadComment[];
@RelationId((comment: LeadComment) => comment.parentComment)
parentCommentId: string;
@ManyToOne(type => User, {cascade: true})
user: User | string;
@RelationId((comment: LeadComment) => comment.user, )
userId: string;
@ManyToOne(type => Lead, lead => lead.comments, {cascade: true})
lead: Lead | string;
@RelationId((comment: LeadComment) => comment.lead)
leadId: string;
@Column('varchar')
content: string;
@CreateDateColumn()
created_at: Date;
@UpdateDateColumn()
updated_at: Date;
}