prismanested-lists

Prisma nested queries


I Have the Following Prisma Model For Product Categories

model ProductCategory {
  categoryId    Int               @id @default(autoincrement())
  categoryName String            @db.VarChar(50) 
  slug         String            @db.VarChar(50) 
  parent        ProductCategory?  @relation("Subcategories", fields: [parentId], references: [categoryId])
  parentId      Int?
  subcategories ProductCategory[] @relation("Subcategories")
 
}

and this is How i Select The Categories

  const data = await prisma.productCategory.findMany({
      select: {
        categoryId: true,
        categoryName: true,     
        slug: true,
        subcategories: {
          select: {
            categoryId: true,
            categoryName: true,           
            slug: true,
            subcategories: {
              select: {
                categoryId: true,
                categoryName: true,               
                slug: true,
              },
            },
          },
        },
      },          
      
    });

And this is the Result

{
        "categoryId": 10,
        "categoryName": "Main Category",        
        "slug": "main-category",
        "subcategories": [
            {
                "categoryId": 11,
                "categoryName": "Sub Category",
                "slug": "sub-category",
                "subcategories": [
                    {
                        "categoryId": 12,
                        "categoryName": "Sub Category 1",
                        "slug": "sub-category-1"
                    },
                    {
                        "categoryId": 13,
                        "categoryName": "Sub Category 2",  
                        "slug": "sub-category-2"
                    }
                ]
            }
        ]
    },
    {
        "categoryId": 11,
        "categoryName": "Sub Category",
        "slug": "sub-category",
        "subcategories": [
          {
                   
                        "categoryId": 12,
                        "categoryName": "Sub Category 1",
                        "slug": "sub-category-1"
                    },
                    {
                        "categoryId": 13,
                        "categoryName": "Sub Category 2",  
                        "slug": "sub-category-2"
                    }
        ]
    },
    {
        "categoryId": 12,
        "categoryNameE": "Sub Category 1",
        "categoryNameD": "Sub Category 1",
        "categoryNameP": "Sub Category 1",
        "slugE": "sub-category-1",
        "subcategories": []
    },
    {
        "categoryId": 13,
        "categoryNameE": "Sub Category 2",
        "categoryNameD": "Sub Category 2",
        "categoryNameP": "Sub Category 2",
        "slugE": "sub-category-2",
        "subcategories": []
    }

I Want the Result Like in the First Row and not want to have the other Rows because its again display the Sub Category Inside {Sub Category 1 ,Sub Category 2} and at the end Each Sub Category 1 and Sub Category 2 separately I want Result Like this

{
        "categoryId": 10,
        "categoryName": "Main Category",        
        "slug": "main-category",
        "subcategories": [
            {
                "categoryId": 11,
                "categoryName": "Sub Category",
                "slug": "sub-category",
                "subcategories": [
                    {
                        "categoryId": 12,
                        "categoryName": "Sub Category 1",
                        "slug": "sub-category-1"
                    },
                    {
                        "categoryId": 13,
                        "categoryName": "Sub Category 2",  
                        "slug": "sub-category-2"
                    }
                ]
            }
        ]
    },


Solution

  • Maybe something like this:

      const data = await prisma.productCategory.findMany({
          where: {
            parentId: null // Not 100% sure, but something similar to this if it doesn't work
          },
          select: {
             //...
          },          
        });
    

    basically, you want to fetch all categories that do not have a parent ID, or in other words, only the TOP level categories.