I am currently working on a project using nextjs 13 and prisma ORM with MongoDB. My task involves fetching roles along with their permissions to create an admin role matrix. Here is the schema for the Role model.
model Role {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
userIDs String[] @db.ObjectId
users User[] @relation(fields: [userIDs], references: [id])
permissions String[]
@@map("roles")
}
When I fetch the records, I want to manipulate them slightly. Below is the query result I get.
const roles = await prisma.role.findMany({
select: {
name: true,
permissions: true,
}
})
console.log(roles);
[
{ name: 'User', permissions: [ 'permissions.user.view.dashboard' ] },
{
name: 'Admin',
permissions: [
'permissions.admin.view.dashboard',
'permissions.user.view.dashboard'
]
}
]
I aim to combine the role name with each permission into one object as follows:
{
'User.permissions.user.view.dashboard',
'Admin.permissions.user.view.dashboard',
'Admin.permissions.admin.view.dashboard'
}
Can this be achieved directly in prisma? If not, how can it be done in JavaScript?