-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.zmodel
153 lines (128 loc) · 4.36 KB
/
schema.zmodel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = 'postgresql'
url = env('DATABASE_URL')
directUrl = env('DATABASE_DIRECT_URL')
}
/*
* Enum for user's role in a space
*/
enum SpaceUserRole {
USER
ADMIN
}
plugin hooks {
provider = '@zenstackhq/tanstack-query'
output = 'src/lib/hooks'
target = 'svelte'
}
/*
* Model for a space in which users can collaborate on Lists and Todos
*/
model Space {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String @default(auth().id)
name String @length(4, 50)
slug String @unique @regex('^[0-9a-zA-Z\-_]{4,16}$')
members SpaceUser[]
lists List[]
// require login
@@deny('all', auth() == null)
// everyone can create a space
@@allow('create', true)
// any user in the space can read the space
@@allow('read', members?[user == auth()])
// space admin can update and delete
@@allow('update,delete', members?[user == auth() && role == ADMIN])
}
/*
* Model representing membership of a user in a space
*/
model SpaceUser {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
role SpaceUserRole
@@unique([userId, spaceId])
// require login
@@deny('all', auth() == null)
// space owner can add any one
@@allow('create', space.owner == auth())
// space admin can add anyone but not himself
@@allow('create', auth() != user && space.members?[user == auth() && role == ADMIN])
// space admin can update/delete
@@allow('update,delete', space.members?[user == auth() && role == ADMIN])
// user can read entries for spaces which he's a member of
@@allow('read', space.members?[user == auth()])
}
/*
* User model
*/
model User {
id String @id @default(cuid())
email String @unique @email
password String @password @omit @length(6, 32)
name String?
ownedSpaces Space[]
memberships SpaceUser[]
todos Todo[]
lists List[]
@@allow('create,read', true)
@@allow('all', auth() == this)
}
/*
* Model for a Todo list
*/
model List {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String @default(auth().id)
title String @length(1, 100)
private Boolean @default(false)
todos Todo[]
// require login
@@deny('all', auth() == null)
// can be read by owner or space members (only if not private)
@@allow('read', owner == auth() || (space.members?[user == auth()] && !private))
// when create, owner must be set to current user, and user must be in the space
@@allow('create', owner == auth() && space.members?[user == auth()])
// when create, owner must be set to current user, and user must be in the space
// update is not allowed to change owner
@@allow('update', owner == auth() && space.members?[user == auth()] && future().owner == owner)
// can be deleted by owner
@@allow('delete', owner == auth())
}
/*
* Model for a single Todo
*/
model Todo {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String @default(auth().id)
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
listId String
title String @length(1, 100)
completedAt DateTime?
// require login
@@deny('all', auth() == null)
// owner has full access, also space members have full access (if the parent List is not private)
@@allow('all', list.owner == auth())
@@allow('all', list.space.members?[user == auth()] && !list.private)
// update cannot change owner
@@deny('update', future().owner != owner)
}