英文:
Designing Google Docs-Like Permissions in Prisma?
问题
我正在构建一个使用 Prisma 的应用程序,并需要设计数据库,使其具有类似于 Google Docs 的权限架构。
所谓 "类似于 Google Docs",是指该应用程序允许用户访问多个 "项目",要么是因为他们是这些项目的创建者/作者,要么是因为其他用户授权他们访问他们创建的项目。
如果作为用户你可以访问另一个用户的项目,那么你将拥有一定级别的权限,这些权限由该用户授予你。这是应用程序与 Google Docs 模型不同的地方。三种权限级别分别是编辑者、审阅者和查看者。
以下是我 Prisma 模式的一部分...
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
projects Project[]
}
model Project {
id String @id @default(cuid())
name String?
things Thing[]
User User @relation(fields: [userId], references: [id])
userId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
我无法弄清楚 User
和 Project
之间的正确关系,以实现拥有一个 Project
、成为一个 Project
的协作者,或两者混合的情况。
英文:
I'm building an application that uses Prisma and need to design the database to have a Google Docs-like permissions architecture.
By "Google Docs-like" I mean that the application allows a user to have access to multiple "Projects", either by being the creator/author of those projects, or by another user giving them access to their created projects.
If as a user you have access to another user's projects, then you have a certain level of permissions granted to you by that user. This is where the application diverges from the Google Docs model. The three permissions levels are Editor, Reviewer and Viewer.
Here's part of my Prisma schema so far...
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
projects Project[]
}
model Project {
id String @id @default(cuid())
name String?
things Thing[]
User User @relation(fields: [userId], references: [id])
userId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
What I can't figure out is the correct relation between User
and Project
to achieve either owning a Project
, being a collaborator on a Project
or a mix of both.
答案1
得分: 1
你可以使用明确的多对多关系来建模这种关系:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
userProjects UserProject[]
}
model Project {
id String @id @default(cuid())
name String?
things Thing[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userProjects UserProject[]
}
model UserProject {
user User @relation(fields: [userId], references: [id])
userId String
project Project @relation(fields: [projectId], references: [id])
projectId String
role Role
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@id([userId, projectId])
}
enum Role {
EDITOR
REVIEWER
VIEWER
}
User
和 Project
通过 UserProject
模型相互关联。这个模型代表了用户和项目之间的多对多关系。每个 UserProject
代表了用户与项目的关联,它包括一个可以是 EDITOR
、REVIEWER
或 VIEWER
的角色字段。这个模式允许用户成为项目的所有者、协作者或同时担任两者。
英文:
You can use explicit many-to-many relations to model this relationship:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
userProjects UserProject[]
}
model Project {
id String @id @default(cuid())
name String?
things Thing[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
userProjects UserProject[]
}
model UserProject {
user User @relation(fields: [userId], references: [id])
userId String
project Project @relation(fields: [projectId], references: [id])
projectId String
role Role
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@id([userId, projectId])
}
enum Role {
EDITOR
REVIEWER
VIEWER
}
User
and Project
are connected through the UserProject model. This model represents the many-to-many relationship between users and projects. Each UserProject represents a user's relationship to a project. It includes a role field that can be EDITOR
, REVIEWER
, or VIEWER
. This schema allows for a user to be the owner of a project, a collaborator on a project, or both.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论