Hello everyone, does anyone know if there is anywa...
# orm-help
d
Hello everyone, does anyone know if there is anyway to make a model with a relation included in the query by default. The use case is: I have a table of comments, where each comment is can have a parent comment and children comments. I want to query only the parents, and that the children be included for every single one, not only the top level ones. Right now I implemented it with a simple algorithm, but it would be great if prisma had support for something like such. Here is the algorithm:
Copy code
const 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:
Copy code
[
  {
    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:
Copy code
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?
}
t
Looks like you'd want to filter by
parent: { equals: null }
and then include the
children
relation, yes?
d
Yes, but for every children's children too
t
Ahh, recursively. Of course. I don't know how to do that in Prisma off the top of my head. Sorry.
r
@Daniel Olavio Ferreira ๐Ÿ‘‹ Currently you need to specify exactly how many levels you need in the self-relation. Prisma doesnโ€™t support fetching recursive relations but we have a couple of requests for the same so it would be great if you could add a ๐Ÿ‘ to these ๐Ÿ™‚ https://github.com/prisma/prisma/issues/4562 https://github.com/prisma/prisma/issues/3725
d
Thanks @Ryan I assumed that would be the case, I left my ๐Ÿ‘ there, and my possible solution as well.
๐Ÿ‘ 1