adamjkb / bark

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

Return children recursively in nested array. #67

Open ashoktamang opened 9 months ago

ashoktamang commented 9 months ago

I have a model named Page which can have multiple children, which in return can have its own children (and so on). I would like to have a functiion like findChildren that would return (given a node/parent) children and its grandchildren in a nested array.

For example:

{
  name: 'parent',
  children: [
    {
      name: 'child1',
    },
    {
      name: 'child2',
      children: [
        {
          name: 'grandchild1',
          children: [*so on*],
        }
      }
    ]
}
adamjkb commented 9 months ago

findDescendants is the function that will give you the result you are looking for but in a flat array.

That said I like the idea of creating a nested array from a flat array. I imagine it as a utility function that take the result of any function call and format it into a nested array tree.