javascriptmysqlprismaprisma-graphql

Prisma query nested list with explicit table


I would like to kindly ask anyone for help. I am struggling with querying my db, where I have 3 tables: Permission, Group, PermissionInGroup.

model Permission {
    @@map(name: "permission")
    id      Int                 @default(autoincrement()) @id
    name    String              @db.VarChar(255)
    groups   PermissionInGroup[]
}

model Group {
    @@map(name: "group")
    id              Int                 @default(autoincrement()) @id
    name            String?
    permissions     PermissionInGroup[]
    users           UserInGroup[]
    domain          Domain              @relation(fields: [domainId], references:[id])
    domainId        Int
}

model PermissionInGroup {
    @@map(name: "permission_in_group")
    id                  Int         @id @default(autoincrement())
    permission          Permission  @relation(fields: [permissionId], references:[id])
    permissionId        Int
    group               Group       @relation(fields: [groupId], references: [id])
    groupId             Int
}

What I want is to query one specific permission (by ID) and I want it to return it with all the groups. My code for prisma is

prisma.permission.findUnique({
                where: {
                    id: id
                },
                include: {
                    groups: {
                        include: {
                            group:true
                        }
                    }
                }
            }

and the return is

{
  id: 1,
  name: 'Test permission',
  groups: [ 
    { id: 1, permissionId: 1, groupId: 1, group: [Object] } 
    { id: 2, permissionId: 1, groupId: 2, group: [Object] } 
  ]
}

My expectation is to get this:

{
  id: 1,
  name: 'Test permission',
  groups: [ 
     {id: 1, name: "Test group", domain: 1},
     {id: 2, name: "Test group 2", domain: 1} 
  ]
}

Is there any simple way to do it? I mean I can do it with for loop but its another step that I would like to avoid if possible

Thank you very much guys


Solution

  • Currently this isn't possible without updating it in your application after fetching the data.

    You can follow this request for more updates as this could solve your issue.