-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
schema.prisma
441 lines (395 loc) · 15.6 KB
/
schema.prisma
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-3.0.x", "linux-arm64-openssl-3.0.x"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("MIGRATE_DATABASE_URL")
}
enum Role {
READ_ONLY
USER
MODERATOR
ADMIN
}
model GlobalOptions {
id Int @id @default(autoincrement())
// Site
siteName String @default("ZTNET")
// Registration
enableRegistration Boolean @default(true)
firstUserRegistration Boolean @default(true) // not in use, will be removed at a later stage
// Email configuration
smtpHost String?
smtpPort String @default("587")
smtpEmail String?
smtpUsername String?
smtpPassword String?
smtpUseSSL Boolean @default(false)
smtpSecure Boolean @default(false)
smtpRequireTLS Boolean @default(false)
smtpIgnoreTLS Boolean @default(false)
inviteUserTemplate Json?
inviteAdminTemplate Json?
inviteOrganizationTemplate Json?
forgotPasswordTemplate Json?
verifyEmailTemplate Json?
notificationTemplate Json?
newDeviceNotificationTemplate Json?
deviceIpChangeNotificationTemplate Json?
// Notifications
userRegistrationNotification Boolean @default(false)
// Welcome message
welcomeMessageEnabled Boolean @default(false)
welcomeMessageTitle String?
welcomeMessageBody String?
// mkworld
customPlanetUsed Boolean @default(false)
planetId Int? @unique
planet Planet? @relation(fields: [planetId], references: [id])
}
// ROOT SERVERS
model Planet {
id Int @id @default(autoincrement())
plID BigInt @default(0)
plBirth BigInt @default(0)
plRecommend Boolean @default(false)
rootNodes RootNodes[]
globalOptions GlobalOptions?
}
model RootNodes {
id Int @id @default(autoincrement())
Planet Planet @relation(fields: [PlanetId], references: [id], onDelete: Cascade)
PlanetId Int
comments String?
identity String
endpoints Json
}
model network_members {
nodeid Int @id @default(autoincrement())
id String
nwid_ref network @relation(fields: [nwid], references: [nwid], onDelete: Cascade)
nwid String
lastSeen DateTime?
physicalAddress String?
online Boolean? @default(false)
deleted Boolean? @default(false)
name String?
address String? @default("")
creationTime DateTime
notations NetworkMemberNotation[]
@@unique([id, nwid])
}
model network {
nwid String @id
name String?
description String?
creationTime DateTime?
lastModifiedTime DateTime?
flowRule String?
autoAssignIp Boolean? @default(true)
nw_userid User? @relation(fields: [authorId], references: [id], onDelete: Cascade)
authorId String?
tagsByName Json?
capabilitiesByName Json?
// Relationships
organizationId String?
organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
networkMembers network_members[]
notations Notation[]
routes Json?
}
model Notation {
id Int @id @default(autoincrement())
name String
color String?
description String?
creationTime DateTime @default(now())
updatedTime DateTime @updatedAt
isActive Boolean @default(true)
nwid String
network network @relation(fields: [nwid], references: [nwid], onDelete: Cascade)
networkMembers NetworkMemberNotation[]
icon String?
orderIndex Int?
visibility String?
@@unique([name, nwid])
}
model NetworkMemberNotation {
notationId Int
nodeid Int
label Notation @relation(fields: [notationId], references: [id])
member network_members @relation(fields: [nodeid], references: [nodeid], onDelete: Cascade)
@@id([notationId, nodeid])
}
// Necessary for Next auth
model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
expires_at Int?
refresh_expires_in Int?
token_type String?
ext_expires_in Int?
expires_in Int?
scope String?
id_token String? @db.Text
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
}
model Session {
id String @id @default(cuid())
sessionToken String @unique
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
model UserOptions {
id Int @id @default(autoincrement())
userId String @unique
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
//networks
useNotationColorAsBg Boolean? @default(false)
showNotationMarkerInTableRow Boolean? @default(true)
//zt central
ztCentralApiKey String? @default("")
ztCentralApiUrl String? @default("https://api.zerotier.com/api/v1")
// local controller
localControllerUrl String? @default("http://zerotier:9993")
localControllerSecret String? @default("")
// member table
deAuthorizeWarning Boolean? @default(false)
addMemberIdAsName Boolean? @default(false)
renameNodeGlobally Boolean? @default(false)
// notifications
newDeviceNotification Boolean? @default(true)
deviceIpChangeNotification Boolean? @default(true)
apiRateLimitNotification Boolean? @default(true)
failedLoginNotification Boolean? @default(true)
}
enum AccessLevel {
READ_ONLY
WRITE
ADMINISTRATIVE
}
model UserGroup {
id Int @id @default(autoincrement())
name String @unique
description String?
maxNetworks Int @default(5)
accessLevel AccessLevel @default(WRITE)
isDefault Boolean @default(false)
users User[]
userInvitation Invitation[]
}
model APIToken {
id String @id @default(cuid())
name String
token String @unique
userId String
apiAuthorizationType Json @default("[\"PERSONAL\"]")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
expiresAt DateTime? // null means it never expires
isActive Boolean @default(true)
}
model User {
id String @id @default(cuid())
name String
email String @unique
emailVerified DateTime?
lastLogin DateTime
lastseen DateTime?
online Boolean? @default(false)
role Role @default(USER)
image String?
hash String?
tempPassword String?
firstTime Boolean @default(true)
twoFactorEnabled Boolean @default(false)
twoFactorSecret String?
twoFactorRecoveryCodes String[] @default([])
failedLoginAttempts Int @default(0)
lastFailedLoginAttempt DateTime?
requestChangePassword Boolean @default(false)
userGroupId Int?
memberOfOrgs Organization[] @relation("MemberRelation") // user can be member of multiple organizations
organizationRoles UserOrganizationRole[]
membershipRequests MembershipRequest[] @relation("MembershipRequestsForUser")
messages Messages[]
lastReadByUsers LastReadMessage[]
ActivityLog ActivityLog[]
expiresAt DateTime? // null means it never expires
isActive Boolean @default(true)
createdAt DateTime @default(now())
userGroup UserGroup? @relation(fields: [userGroupId], references: [id], onDelete: Restrict)
options UserOptions?
accounts Account[]
sessions Session[]
network network[]
apiTokens APIToken[]
webhooks Webhook[]
invitations Invitation[] @relation("InvitationsSent")
UserDevice UserDevice[]
}
model UserDevice {
id String @id @default(cuid())
userId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userAgent String
deviceType String
ipAddress String?
location String?
deviceId String @unique
browser String
browserVersion String
os String
osVersion String
lastActive DateTime
isActive Boolean @default(true)
createdAt DateTime @default(now())
@@index([userId])
@@index([deviceId])
}
model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}
model Invitation {
id Int @id @default(autoincrement())
token String @unique
used Boolean @default(false)
email String?
secret String?
groupId String?
userGroupId Int?
url String
expiresAt DateTime
mailSentAt DateTime?
timesCanUse Int @default(1)
timesUsed Int @default(0)
invitedById String
createdAt DateTime @default(now())
role Role @default(READ_ONLY)
require2FA Boolean @default(false)
createdBy User @relation(fields: [invitedById], references: [id], onDelete: Cascade, name: "InvitationsSent")
userGroup UserGroup? @relation(fields: [userGroupId], references: [id], onDelete: Cascade)
organizations OrganizationInvitation[]
}
//
// ORGANIZATION
//
model OrganizationInvitation {
id String @id @default(cuid())
invitationId Int
organizationId String
invitation Invitation @relation(fields: [invitationId], references: [id], onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
}
model Organization {
id String @id @default(cuid())
createdAt DateTime @default(now())
ownerId String
orgName String
description String?
users User[] @relation("MemberRelation")
networks network[]
settings OrganizationSettings?
membershipRequests MembershipRequest[] @relation("MembershipRequestsForOrganization")
isActive Boolean @default(true)
require2FA Boolean @default(false)
userRoles UserOrganizationRole[]
messages Messages[]
lastReadByUsers LastReadMessage[]
ActivityLog ActivityLog[]
webhooks Webhook[]
invitations OrganizationInvitation[]
}
model Webhook {
id String @id @default(cuid())
name String
description String
url String
enabled Boolean @default(false)
eventTypes Json
secret String? @default("")
lastDelivery DateTime? @default(now())
// Relationship with Organization
organization Organization? @relation(fields: [organizationId], references: [id])
organizationId String? // Foreign key
createdAt DateTime @default(now())
// Relationship with User
User User? @relation(fields: [userId], references: [id])
userId String?
}
model UserOrganizationRole {
userId String
organizationId String
role Role
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
@@id([userId, organizationId])
}
model Messages {
id Int @id @default(autoincrement())
content String
createdAt DateTime @default(now())
userId String // Reference to the User model
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organizationId String // Reference to the Organization model
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
lastReadByUsers LastReadMessage[]
}
model LastReadMessage {
id Int @id @default(autoincrement())
lastMessageId Int // ID of the last read message
userId String
organizationId String
lastMessage Messages @relation(fields: [lastMessageId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
@@unique([userId, organizationId])
}
model OrganizationSettings {
id Int @id @default(autoincrement())
renameNodeGlobally Boolean? @default(false)
organizationId String @unique
organization Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
}
model MembershipRequest {
id Int @id @default(autoincrement())
userId String
organizationId String
user User @relation(fields: [userId], references: [id], name: "MembershipRequestsForUser", onDelete: Cascade)
organization Organization @relation(fields: [organizationId], references: [id], name: "MembershipRequestsForOrganization", onDelete: Cascade)
}
model ActivityLog {
id Int @id @default(autoincrement())
action String
createdAt DateTime @default(now())
performedById String
performedBy User @relation(fields: [performedById], references: [id], onDelete: Cascade)
organizationId String?
organization Organization? @relation(fields: [organizationId], references: [id], onDelete: Cascade)
}
// To map your data model to the database schema, you need to use the prisma migrate CLI commands:
// npx prisma migrate dev --name (NAME)
// reset db
// npx prisma migrate reset
// npx prisma db push --preview-feature
// Deploy
// npx prisma migrate deploy --preview-feature
// Issues with migration
// npx prisma migrate resolve --rolled-back 20210112134813_init --preview-feature
// generate local draft
// npx prisma migrate dev --create-only --preview-feature