Forms: How would you go about updating relations from the frontend?

(long-ish post) I'm currently building an admin dashboard, where you can manage users for example. A user can be edited by going to /dashboard/users/<userid>, and there I fetch the whole user object including relations (role object, permissions object).
model User {
id String @id @default(nanoid(14))
displayName String
image String
role UserRole @relation(fields: [roleId], references: [id])
roleId String
permissions UserPermission[]
createdAt String
updatedAt String

@@index([roleId])
}

model UserRole {
id String @id @default(nanoid(14))
name String @unique
displayName String
description String?
user User[]
}

model UserPermission {
id String @id @default(nanoid(18))
name String @unique
displayName String
description String?
user User[]
}
model User {
id String @id @default(nanoid(14))
displayName String
image String
role UserRole @relation(fields: [roleId], references: [id])
roleId String
permissions UserPermission[]
createdAt String
updatedAt String

@@index([roleId])
}

model UserRole {
id String @id @default(nanoid(14))
name String @unique
displayName String
description String?
user User[]
}

model UserPermission {
id String @id @default(nanoid(18))
name String @unique
displayName String
description String?
user User[]
}
I'm using Formik to handle my user editing form and I'm a bit uncertain about how to tackle updating all of the relations - especially the permissions. I want these to be toggleable via checkboxes. One way of doing this is to take the initial permissions object with all its nested permissions and create a copy of it (Formik does this for you). Then I can remove individual permissions from that object, or add a permissions unique name to it. Finally, I can compare that to the initial values and see which permissions got removed or which got added, and do multiple DB queries based on that (connect/disconnect user from permission). Maybe create endpoints like /addUserPermission and /removeUserPermission? Is this a good approach? Or is there maybe a better way that requires fewer queries?
1 Reply
Scot
Scot2y ago
Is this a good approach? Or is there maybe a better way that requires fewer queries?
make it work first, then worry about optimizing, have a think to yourself about your constraints and do some napkin math: 1. How many requests do i expect per unit of time? 2. How much of a payload can I have on those requests? (ie is there anything stoppingme?) 3. How much data do i expect to be stored in the db?
Want results from more Discord servers?
Add your server