adamjkb / bark

Materialized path extension for Prisma
https://prisma-extension-bark.gitbook.io
MIT License
33 stars 5 forks source link

Get at least one children #76

Closed VadimZP closed 4 months ago

VadimZP commented 4 months ago

Hello! I have a comments section and I request "root" comments with findSiblings.

How can I get at least one or two children of each sibling? Reddit's comments section is a good example of what I am trying to achieve 🙂

adamjkb commented 4 months ago

If those comments are at the root level it might be worth it to just use Prisma's findMany. You could write something like this to get all the roots + 1 level of their children:

await prisma.node.findMany({
  where: { OR: [
    { depth: { equals: 1 } },
    { depth: { equals: 2 } }]
  }
})

Otherwise if you want to limit how many children you got back you might need to do findSiblings and then findChildren for each sibling with a take argument. This operation can even be wrapped up in a $transaction in case you were thinking of using Promise.all. (I'm suggesting because there used to some issues with Promise.all and SQLite: https://github.com/adamjkb/bark/pull/54/commits/740aecfad496deaf1a34cf699372b742428f899d)

VadimZP commented 4 months ago

Thank you!