tianji/prisma/schema.prisma

325 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2023-09-02 12:13:50 +00:00
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
2023-09-02 19:49:20 +00:00
provider = "prisma-client-js"
2023-09-02 12:13:50 +00:00
}
generator json {
provider = "prisma-json-types-generator"
}
generator zod {
provider = "zod-prisma"
modelSuffix = "ModelSchema"
2023-12-13 10:21:04 +00:00
imports = "./zod/schemas"
}
2023-09-02 12:13:50 +00:00
model User {
id String @id @unique @default(cuid()) @db.VarChar(30)
2023-09-03 11:28:53 +00:00
username String @unique @db.VarChar(255)
password String @db.VarChar(60)
role String @db.VarChar(50)
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
2023-09-03 11:28:53 +00:00
deletedAt DateTime? @db.Timestamptz(6)
currentWorkspaceId String @db.VarChar(30)
currentWorkspace Workspace @relation(fields: [currentWorkspaceId], references: [id])
2023-09-02 12:13:50 +00:00
2023-09-02 19:49:20 +00:00
workspaces WorkspacesOnUsers[]
2023-09-02 12:13:50 +00:00
}
2023-09-02 19:49:20 +00:00
model Workspace {
id String @id @unique @default(cuid()) @db.VarChar(30)
name String @db.VarChar(100)
dashboardOrder String[]
/// [DashboardLayout]
dashboardLayout Json? @db.Json
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
2023-09-02 12:13:50 +00:00
2023-12-13 10:21:04 +00:00
users WorkspacesOnUsers[]
websites Website[]
notifications Notification[]
monitors Monitor[]
monitorStatusPages MonitorStatusPage[]
2023-09-04 17:18:43 +00:00
// for user currentWorkspace
selectedUsers User[]
2023-09-02 19:49:20 +00:00
}
model WorkspacesOnUsers {
userId String @db.VarChar(30)
workspaceId String @db.VarChar(30)
role String @db.VarChar(100)
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
2023-09-02 19:49:20 +00:00
user User @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade)
workspace Workspace @relation(fields: [workspaceId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-02 19:49:20 +00:00
@@id([userId, workspaceId])
@@index([userId])
@@index([workspaceId])
}
model Website {
id String @id @unique @default(cuid()) @db.VarChar(30)
2023-10-14 16:51:03 +00:00
workspaceId String @db.VarChar(30)
2023-09-02 19:49:20 +00:00
name String @db.VarChar(100)
domain String? @db.VarChar(500)
shareId String? @unique @db.VarChar(50)
resetAt DateTime? @db.Timestamptz(6)
2023-10-14 16:51:03 +00:00
monitorId String? @db.VarChar(30)
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
2023-09-02 19:49:20 +00:00
deletedAt DateTime? @db.Timestamptz(6)
2023-10-04 17:56:33 +00:00
workspace Workspace @relation(fields: [workspaceId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-10-14 16:51:03 +00:00
monitor Monitor? @relation(fields: [monitorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-10-04 17:56:33 +00:00
sessions WebsiteSession[]
eventData WebsiteEventData[]
sessionData WebsiteSessionData[]
2023-09-02 12:13:50 +00:00
2023-09-02 19:49:20 +00:00
@@index([workspaceId])
2023-09-02 12:13:50 +00:00
@@index([createdAt])
@@index([shareId])
}
model WebsiteSession {
id String @id @unique @db.Uuid
websiteId String @db.VarChar(30)
hostname String? @db.VarChar(100)
browser String? @db.VarChar(20)
os String? @db.VarChar(20)
device String? @db.VarChar(20)
screen String? @db.VarChar(11)
language String? @db.VarChar(35)
2023-10-21 07:26:51 +00:00
ip String? @db.VarChar(45) // The max length of ipv6 which adapter with ipv4 is 45(for example: [::ffff:192.168.100.228] => 0000:0000:0000:0000:0000:ffff:192.168.100.228)
country String? @db.Char(2)
subdivision1 String? @db.VarChar(20)
subdivision2 String? @db.VarChar(50)
city String? @db.VarChar(50)
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-02 12:13:50 +00:00
2023-10-04 17:56:33 +00:00
website Website @relation(fields: [websiteId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-02 12:13:50 +00:00
websiteEvent WebsiteEvent[]
sessionData WebsiteSessionData[]
2023-09-02 12:13:50 +00:00
@@index([createdAt])
@@index([websiteId])
@@index([websiteId, createdAt])
@@index([websiteId, createdAt, hostname])
@@index([websiteId, createdAt, browser])
@@index([websiteId, createdAt, os])
@@index([websiteId, createdAt, device])
@@index([websiteId, createdAt, screen])
@@index([websiteId, createdAt, language])
@@index([websiteId, createdAt, country])
@@index([websiteId, createdAt, subdivision1])
@@index([websiteId, createdAt, city])
}
model WebsiteEvent {
id String @id() @default(cuid()) @db.VarChar(30)
websiteId String @db.VarChar(30)
sessionId String @db.Uuid
urlPath String @db.VarChar(500)
urlQuery String? @db.VarChar(500)
referrerPath String? @db.VarChar(500)
referrerQuery String? @db.VarChar(500)
referrerDomain String? @db.VarChar(500)
pageTitle String? @db.VarChar(500)
eventType Int @default(1) @db.Integer
eventName String? @db.VarChar(50)
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-02 12:13:50 +00:00
eventData WebsiteEventData[]
2023-10-04 17:56:33 +00:00
session WebsiteSession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-02 12:13:50 +00:00
@@index([createdAt])
@@index([sessionId])
@@index([websiteId])
@@index([websiteId, createdAt])
@@index([websiteId, createdAt, urlPath])
@@index([websiteId, createdAt, urlQuery])
@@index([websiteId, createdAt, referrerDomain])
@@index([websiteId, createdAt, pageTitle])
@@index([websiteId, createdAt, eventName])
@@index([websiteId, sessionId, createdAt])
}
model WebsiteEventData {
id String @id() @default(cuid()) @db.VarChar(30)
websiteId String @db.VarChar(30)
websiteEventId String @db.VarChar(30)
2023-09-02 12:13:50 +00:00
eventKey String @db.VarChar(500)
stringValue String? @db.VarChar(500)
numberValue Decimal? @db.Decimal(19, 4)
dateValue DateTime? @db.Timestamptz(6)
dataType Int @db.Integer
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-02 12:13:50 +00:00
website Website @relation(fields: [websiteId], references: [id], onUpdate: Cascade, onDelete: Cascade)
websiteEvent WebsiteEvent @relation(fields: [websiteEventId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-02 12:13:50 +00:00
@@index([createdAt])
@@index([websiteId])
@@index([websiteEventId])
@@index([websiteId, createdAt])
@@index([websiteId, createdAt, eventKey])
}
model WebsiteSessionData {
id String @id() @default(cuid()) @db.VarChar(30)
websiteId String @db.VarChar(30)
2023-09-02 12:13:50 +00:00
sessionId String @db.Uuid
stringValue String? @db.VarChar(500)
numberValue Decimal? @db.Decimal(19, 4)
dateValue DateTime? @db.Timestamptz(6)
dataType Int @db.Integer
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-02 12:13:50 +00:00
website Website @relation(fields: [websiteId], references: [id], onUpdate: Cascade, onDelete: Cascade)
session WebsiteSession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-02 12:13:50 +00:00
@@index([createdAt])
@@index([websiteId])
@@index([sessionId])
}
model TelemetrySession {
id String @id @unique @db.Uuid
workspaceId String @db.VarChar(30)
hostname String? @db.VarChar(100)
browser String? @db.VarChar(20)
os String? @db.VarChar(20)
2023-11-27 16:45:13 +00:00
ip String? @db.VarChar(45)
country String? @db.Char(2)
subdivision1 String? @db.VarChar(20)
subdivision2 String? @db.VarChar(50)
city String? @db.VarChar(50)
createdAt DateTime @default(now()) @db.Timestamptz(6)
telemetryEvent TelemetryEvent[]
@@index([createdAt])
@@index([workspaceId, createdAt])
}
model TelemetryEvent {
id String @id() @default(cuid()) @db.VarChar(30)
sessionId String @db.Uuid
workspaceId String @db.VarChar(30)
eventName String? @db.VarChar(100)
urlOrigin String @db.VarChar(500)
urlPath String @db.VarChar(500)
/// [CommonPayload]
payload Json? @db.Json // Other payload info get from query params, should be a object
createdAt DateTime @default(now()) @db.Timestamptz(6)
session TelemetrySession @relation(fields: [sessionId], references: [id], onUpdate: Cascade, onDelete: Cascade)
@@index([createdAt])
@@index([sessionId])
@@index([workspaceId])
@@index([workspaceId, createdAt])
}
2023-09-26 06:45:28 +00:00
model Notification {
id String @id() @default(cuid()) @db.VarChar(30)
workspaceId String @db.VarChar(30)
name String @db.VarChar(100)
type String @db.VarChar(100)
/// [CommonPayload]
payload Json @db.Json
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-26 06:45:28 +00:00
workspace Workspace @relation(fields: [workspaceId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-26 06:45:28 +00:00
2023-09-29 09:12:06 +00:00
monitors Monitor[]
2023-09-26 06:45:28 +00:00
@@index([workspaceId])
}
2023-09-29 09:12:06 +00:00
model Monitor {
id String @id() @default(cuid()) @db.VarChar(30)
workspaceId String @db.VarChar(30)
name String @db.VarChar(100)
type String @db.VarChar(100)
active Boolean @default(true) @db.Boolean
interval Int @default(20) @db.Integer
2023-10-04 17:56:33 +00:00
// TODO
// maxRetry Int @default(0) @db.Integer
// retryInterval Int @default(0) @db.Integer
/// [CommonPayload]
payload Json @db.Json
createdAt DateTime @default(now()) @db.Timestamptz(6)
2023-09-29 09:12:06 +00:00
workspace Workspace @relation(fields: [workspaceId], references: [id])
2023-10-14 16:51:03 +00:00
websites Website[]
2023-09-29 09:12:06 +00:00
notifications Notification[]
events MonitorEvent[]
2023-10-04 17:56:33 +00:00
datas MonitorData[]
status MonitorStatus[]
@@index([workspaceId])
}
model MonitorEvent {
id String @id @default(cuid()) @db.VarChar(30)
message String @db.VarChar(500)
monitorId String @db.VarChar(30)
type String @db.VarChar(100) // UP or DOWN
createdAt DateTime @default(now()) @db.Timestamptz(6)
monitor Monitor @relation(fields: [monitorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
}
model MonitorData {
id String @id @default(cuid()) @db.VarChar(30)
monitorId String @db.VarChar(30)
value Int @default(0) @db.Integer // -1 means error
createdAt DateTime @default(now()) @db.Timestamptz(6)
monitor Monitor @relation(fields: [monitorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
2023-09-29 09:12:06 +00:00
}
// Use for record latest monitor status, for example tls status
model MonitorStatus {
monitorId String @db.VarChar(30)
statusName String @db.VarChar(50)
/// [CommonPayload]
payload Json @db.Json
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
monitor Monitor @relation(fields: [monitorId], references: [id], onUpdate: Cascade, onDelete: Cascade)
@@id([monitorId, statusName])
}
2023-12-13 10:21:04 +00:00
model MonitorStatusPage {
id String @id @default(cuid()) @db.VarChar(30)
workspaceId String @db.VarChar(30)
slug String @unique // url slug
title String @db.VarChar(100)
description String @default("") @db.VarChar(1000)
/// [MonitorStatusPageList]
/// @zod.custom(imports.MonitorStatusPageListSchema)
monitorList Json @default("[]") // monitor list
createdAt DateTime @default(now()) @db.Timestamptz(6)
updatedAt DateTime @updatedAt @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id])
@@index([slug])
}