Daniel Olavio Ferreira
08/05/2021, 8:17 PMconst commentParser = (comments: Comment[]): Comment[] => {
const commentMap = {};
comments.forEach((comment) => {
commentMap[comment.id] = comment;
commentMap[comment.id].children = [];
});
comments.forEach((comment) => {
if (comment.parentId !== null) {
const parent = commentMap[comment.parentId];
parent.children.push(comment);
}
});
return comments.filter((comment) => comment.parentId === null);
};
The income data looks like this:
[
{
id: 'ckryupa4i0014sdyazioetibq',
text: 'Teste',
createdAt: '05/08/2021',
parentId: 'ckryqxk8s02772zyagi476b6x'
},
{
id: 'ckryvgpd80063sdyadwo9gyck',
text: 'Teste 2',
createdAt: '05/08/2021',
parentId: 'ckryupa4i0014sdyazioetibq'
},
{
id: 'ckryr0tgh03352zya03qkogtx',
text: 'You are welcome',
createdAt: '05/08/2021',
parentId: 'ckryqw59k02432zyaet0j395g'
},
{
id: 'ckryqxk8s02772zyagi476b6x',
text: 'dasdasd',
createdAt: '05/08/2021',
parentId: null
},
{
id: 'ckryqw59k02432zyaet0j395g',
text: 'Thanks!',
createdAt: '05/08/2021',
parentId: 'ckryp56ga00492zya3yq6kgbm'
},
{
id: 'ckryp56ga00492zya3yq6kgbm',
text: 'great post!',
createdAt: '05/08/2021',
parentId: null
}
]
Here is my Comment model:
model Comment {
id String @id @default(cuid())
text String
createdAt DateTime @default(now()) @map("created_at")
author User @relation(fields: [authorId], references: [id])
authorId String @map("author_id")
post Post @relation(fields: [postId], references: [id])
postId String @map("post_id")
children Comment[] @relation("CommentToComment")
parent Comment? @relation("CommentToComment", fields: [parentId], references: [id])
parentId String?
}
Tim Mensch
08/05/2021, 8:40 PMparent: { equals: null }
and then include the children
relation, yes?Daniel Olavio Ferreira
08/05/2021, 10:54 PMTim Mensch
08/06/2021, 12:39 AMRyan
08/06/2021, 5:53 AMDaniel Olavio Ferreira
08/06/2021, 6:33 AM