stuart Lynn
09/12/2022, 3:41 PMenum ResourceType {
App
Dataset
}
model App {
id String @id @default(cuid())
spec Json
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
name String
description String
public Boolean
ownerId String
noViews Int @default(0)
noForks Int @default(0)
owner User @relation(fields: [ownerId], references: [id])
collaborators Collaborator[] @relation("AppColaborator")
@@map("apps")
}
model Dataset {
id String @id @default(cuid())
createdAt DateTime @default(now())
name String
description String
public Boolean
path String
ownerId String
owner User @relation(fields: [ownerId], references: [id])
collaborators Collaborator[] @relation("DatasetColaborator")
@@map("datasets")
}
model User {
id String @id @default(cuid())
name String?
apps App[]
collaborations Collaborator[]
Datasets Dataset[]
@@map("users")
}
model Collaborator {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String
appId String?
datasetId String?
resourceType ResourceType
view Boolean
edit Boolean
manage Boolean
app App? @relation("AppColaborator", fields: [appId], references: [id], map: "app_colaboration_id")
dataset Dataset? @relation("DatasetColaborator", fields: [datasetId], references: [id], map: "dataset_colaboration_id")
@@unique([userId, appId, datasetId])
@@map("collaborators")
}
The issue with this is if I try and create or upsert a Collaborator object, I cant because one of the appId or datasetId needs to be undefined which prisma rejects.
The other approach I have tried is to have a single resourceId with a resourceType that defines what kind of resource this is and define two relationships like so
model Collaborator {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String
resourceId String
resourceType ResourceType
view Boolean
edit Boolean
manage Boolean
app App? @relation("AppColaborator", fields: [resourceId], references: [id], map: "app_colaboration_id")
dataset Dataset? @relation("DatasetColaborator", fields: [resourceId], references: [id], map: "dataset_colaboration_id")
@@unique([userId, resourceId])
@@map("collaborators")
}
However that fails when I try to create a new collaborator because both foreign key constraints for datasetId and appId cant both be satisfied.
Is there a good model or example for doing this kind of thing? A little stumped herestuart Lynn
09/12/2022, 3:44 PMstuart Lynn
09/12/2022, 7:38 PMRaphael Etim
09/14/2022, 6:18 AMstuart Lynn
09/14/2022, 8:58 PMRaphael Etim
09/15/2022, 6:38 AM