-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.prisma
532 lines (453 loc) · 17.1 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
generator client {
provider = "prisma-client-js"
}
// to generate dtos/entities
generator nestjsDto {
provider = "prisma-generator-nestjs-dto"
output = "../src"
outputToNestJsResourceStructure = "true"
exportRelationModifierClasses = "true"
reExport = "false"
createDtoPrefix = "Create"
updateDtoPrefix = "Update"
dtoSuffix = "Dto"
entityPrefix = ""
entitySuffix = ""
fileNamingStyle = "kebab"
classValidation = "true"
prettier = "true"
}
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
// db sharding base on tenantId:
// 1. id segmentation, ALTER SEQUENCE [表名]_[id]_seq RESTART WITH 1001;
// 2.chard tenant based on tenant_id, eq (0~10000] db1, (10000,20000] db2
//////////////////// Authentication and Account ////////////////////////////
model Tenant {
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
emailHost String? @unique @db.VarChar(36) /// @description unique tenant email host
name String? @db.VarChar(50)
avatar String? @db.VarChar(1023)
type Int @default(1) /// @description account type: 1: individual, 2: organization.
statusCode Int @default(0) /// @description user statusCode: 0: pending, 1: active, -1: inactive.
User User[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
}
model User {
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
name String @db.VarChar(36)
email String? @db.VarChar(255) /// @description account primary verified email
avatar String? @db.VarChar(1023)
locale String? @default("en_US") @db.VarChar(10)
tenant Tenant @relation(fields: [tenantId], references: [id])
/// @DtoRelationIncludeId
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
/// @DtoReadOnly
/// @DtoEntityHidden
userIdentity UserIdentity[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([tenantId])
}
model UserIdentity {
id Int @id @default(autoincrement())
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
provider String @db.VarChar(36)
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uid String @db.VarChar(255) /// @description userId from provider.
credentials String @db.VarChar(2048) /// @description user cridentials
name String? @db.VarChar(255)
/// @MinLength(6)
/// @IsEmail
/// @example user@example.com
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
email String? @db.VarChar(255)
email_verified Boolean @default(false)
avatar String? @db.VarChar(1023)
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
info Json? @map("info") /// @description authentication info from realm
user User @relation(fields: [userId], references: [id])
userId Int
/// @DtoReadOnly
/// @DtoEntityHidden
userUuid String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@unique([provider, uid])
@@index([userId])
@@index([userUuid])
@@index([tenantId])
}
/// @DtoIgnoreModel
/// @description non-organization email hosts
model PublicMailHost {
id Int @id @default(autoincrement())
/// @description starts with `.`, e.g.: '.gmail.com', '.hotmail.com'
dotHost String @unique @db.VarChar(255)
createdAt DateTime @default(now())
}
//////////////////// Callgent ////////////////////////////
model Callgent {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
name String @db.VarChar(255)
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
summary String? @db.VarChar(4095)
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@unique([tenantId, name])
@@index([tenantId])
}
model CallgentFunction {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
name String @db.VarChar(255)
/// @description js function name to invoke this callgent function
funName String @db.VarChar(255)
params String[] @db.VarChar(31)
/// @description js function api docs
documents String @db.VarChar(4095)
/// @description js actual code to invoke this callgent function
fullCode String @db.VarChar(1023)
/// @description endpoint uuid or command or code
content Json @db.Json
// @DtoReadOnly
/// @CustomValidator(EntityIdExists, 'callgent', 'uuid', ../../infra/repo/validators/entity-exists.validator)
callgentUuid String @db.VarChar(36)
/// @description actual server endpoint, when action type is `ENDPOINT`
/// @CustomValidator(EntityIdExists, 'endpoint', 'uuid', ../../infra/repo/validators/entity-exists.validator)
endpointUuid String? @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@unique([callgentUuid, name])
@@index([tenantId])
@@index([callgentUuid])
}
model Task {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
statusCode Int @default(-1) /// @description Task statusCode, -1: pending, 0: done, <-1: failed
name String? @db.VarChar(64) /// @description task name
brief String? @db.VarChar(255) /// @description task brief or description
content Json? /// @description optional task content
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([tenantId])
}
model TaskAction {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
// client side information
/// @description action owner callgent uuid, responsible for progressive response
cepUuid String? @db.VarChar(36) /// @description client endpoint uuid
funName String? @db.VarChar(255) /// @description optional callgent function name
cAdaptor String @db.VarChar(36) /// @description client adaptor key
callback Json? @db.Json /// @description callback url template or callgent function
progressive String? @db.VarChar(36) /// @description progressive requesting url template or callgent function
// processing
returns Boolean @default(false) /// @description whether action has response object
req Json @db.Json /// @description request object
res Json? @db.Json /// @description response object
stage Int @default(-1) /// @description processing stage in the task, -1: init; 0: done
statusCode Int @default(0) /// execution status code
message String? @db.VarChar(255) /// @description error message
// @DtoReadOnly
/// @CustomValidator(EntityIdExists, 'callgent', 'uuid', ../../infra/repo/validators/entity-exists.validator)
taskUuid String @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36) /// @description user uuid, anonymous ip, etc.
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([tenantId])
@@index([taskUuid])
}
/// @DtoIgnoreModel
model AuthToken {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
token String @unique @db.VarChar(36)
type String @db.VarChar(10) /// @description token type: JWT, API_KEY, ..
/// @DtoCastType(JwtPayload, ../infra/auth/jwt/jwt.service)
payload Json
revoked Boolean @default(false)
expiresAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}
enum EndpointType {
CLIENT
SERVER
EVENT
}
/// @description a callgent may have multiple endpoints, including cep & sep
model Endpoint {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
type EndpointType /// @description endpoint type: CLIENT, SERVER OR EVENT
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
adaptorKey String @db.VarChar(127) /// @description endpoint adaptor key
priority Int @default(0) /// @description priority in the callgent
host Json @db.Json /// @description host address & configs
initParams Json? @db.Json /// @description initializing parameters
content Json? @db.Json /// @description generated content/code by init method
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
/// @CustomValidator(EntityIdExists, 'callgent', 'uuid', ../../infra/repo/validators/entity-exists.validator)
callgentUuid String @db.VarChar(36) /// @description callgent owning the endpoint
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([tenantId])
@@index([callgentUuid])
}
enum EndpointAuthType {
NONE /// @description no auth, or treat auth as normal progressive params
APP /// @description all task same auth
USER /// @description auth each task owner/assignee
}
/// @description endpoint app or user auth credentials
model EndpointAuth {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
/// @CustomValidator(EntityIdExists, 'endpoint', 'uuid', ../../infra/repo/validators/entity-exists.validator)
endpointUuid String @db.VarChar(36) /// @description endpoint uuid.
userKey String? @db.VarChar(63) /// @description unique user key, e.g. assignee userId
credentials Json @db.Json /// @description auth credentials
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([endpointUuid, userKey])
@@index([tenantId])
@@index([endpointUuid])
}
/// @DtoIgnoreModel
model LlmTemplate {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
name String @db.VarChar(32)
prompt String @db.VarChar(4096)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([name])
}
/// @DtoIgnoreModel
model LlmCache {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
name String @db.VarChar(32)
prompt String @db.VarChar(4096)
result String @db.VarChar(4096)
createdAt DateTime @default(now())
@@unique([prompt, name])
}
model PersistedAsync {
/// @DtoEntityHidden
id BigInt @id @default(autoincrement())
statusCode Int @default(-1) /// @description statusCode, -1: pending, 1: sent, 0: done, <-1: failed
service String @db.VarChar(127) /// @description callback service name
method String @db.VarChar(127) /// @description callback method name
parentAsyncId BigInt? @db.BigInt /// @description parent async id
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
deletedAt DateTime? /// @description logical deletion.
}
enum ServiceType {
SERVICE
CALLGENT
}
model EventListener {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
srcUuid String @db.VarChar(36) /// @description event source id
eventType String @db.VarChar(36)
dataType String @db.VarChar(36)
priority Int? @default(0)
serviceType ServiceType // callgent, service
serviceName String @db.VarChar(255)
funName String @db.VarChar(255)
// TODO add the listener's input/output props in event.context, for automatic checking
/// @DtoReadOnly
/// @DtoEntityHidden
createdBy String @db.VarChar(36)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([srcUuid])
@@index([tenantId])
}
enum EventCallbackType {
URL
EVENT
}
model EventStore {
/// @DtoEntityHidden
id Int @id @default(autoincrement())
/// @DtoCreateApiResponse
/// @DtoUpdateApiResponse
/// @DtoPlainApiResponse
uuid String @unique @db.VarChar(36)
/// @DtoReadOnly
/// @DtoEntityHidden
// add to migration.sql
tenantId Int @default(dbgenerated("(current_setting('tenancy.tenantId')::int)"))
/// @Description src entity uuid which bind to the listener
srcId String @db.VarChar(36)
/// @Description target uuid to relate several events
targetId String? @db.VarChar(36)
eventType String @db.VarChar(36)
dataType String @db.VarChar(36)
/// @Description callback url or parent event id
callback String? @db.VarChar(1023)
/// @Description callback type, 'URL' or 'EVENT'
callbackType EventCallbackType @default(EVENT)
data Json? @db.Json
context Json? @db.Json
/// @description statusCode, -1: processing, 0: done, 1: pending, >1: error
statusCode Int @default(-1)
message String? @db.VarChar(255)
stopPropagation Boolean
defaultPrevented Boolean
listenerUuid String? @db.VarChar(36)
funName String? @db.VarChar(255)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
// DONOT FORGET TO ADD the model into `createSoftDeleteMiddleware`
/// @DtoReadOnly
/// @DtoEntityHidden
deletedAt DateTime? /// @description logical deletion.
@@index([srcId])
@@index([targetId])
@@index([tenantId])
@@index([uuid])
}