student-manage/packages/common/prisma/schema.prisma

480 lines
16 KiB
Plaintext
Raw Normal View History

2025-02-22 22:06:44 +08:00
// 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"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Taxonomy {
id String @id @default(cuid())
name String @unique
slug String @unique @map("slug")
deletedAt DateTime? @map("deleted_at")
createdAt DateTime @default(now()) @map("created_at")
terms Term[]
objectType String[] @map("object_type")
order Float? @map("order")
@@index([order, deletedAt])
@@map("taxonomy")
}
model Term {
id String @id @default(cuid())
name String
taxonomy Taxonomy? @relation(fields: [taxonomyId], references: [id])
taxonomyId String? @map("taxonomy_id")
order Float? @map("order")
description String?
parentId String? @map("parent_id")
parent Term? @relation("ChildParent", fields: [parentId], references: [id], onDelete: Cascade)
children Term[] @relation("ChildParent")
ancestors TermAncestry[] @relation("DescendantToAncestor")
descendants TermAncestry[] @relation("AncestorToDescendant")
domainId String? @map("domain_id")
domain Department? @relation("TermDom", fields: [domainId], references: [id])
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
createdBy String? @map("created_by")
depts Department[] @relation("department_term")
hasChildren Boolean? @default(false) @map("has_children")
posts Post[] @relation("post_term")
@@index([name]) // 对name字段建立索引以加快基于name的查找速度
@@index([parentId]) // 对parentId字段建立索引以加快基于parentId的查找速度
@@map("term")
}
model TermAncestry {
id String @id @default(cuid())
ancestorId String? @map("ancestor_id")
descendantId String @map("descendant_id")
relDepth Int @map("rel_depth")
ancestor Term? @relation("AncestorToDescendant", fields: [ancestorId], references: [id])
descendant Term @relation("DescendantToAncestor", fields: [descendantId], references: [id])
// 索引建议
@@index([ancestorId]) // 针对祖先的查询
@@index([descendantId]) // 针对后代的查询
@@index([ancestorId, descendantId]) // 组合索引,用于查询特定的祖先-后代关系
@@index([relDepth]) // 根据关系深度的查询
@@map("term_ancestry")
}
model Staff {
id String @id @default(cuid())
showname String? @map("showname")
username String @unique @map("username")
avatar String? @map("avatar")
password String? @map("password")
phoneNumber String? @unique @map("phone_number")
domainId String? @map("domain_id")
deptId String? @map("dept_id")
domain Department? @relation("DomainStaff", fields: [domainId], references: [id])
department Department? @relation("DeptStaff", fields: [deptId], references: [id])
order Float?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
enabled Boolean? @default(true)
deletedAt DateTime? @map("deleted_at")
officerId String? @map("officer_id")
watchedPost Post[] @relation("post_watch_staff")
visits Visit[]
posts Post[]
sentMsgs Message[] @relation("message_sender")
receivedMsgs Message[] @relation("message_receiver")
registerToken String?
ownedResources Resource[]
@@index([officerId])
@@index([deptId])
@@index([domainId])
@@index([username])
@@index([order])
@@map("staff")
}
model Department {
id String @id @default(cuid())
name String
order Float?
ancestors DeptAncestry[] @relation("DescendantToAncestor")
descendants DeptAncestry[] @relation("AncestorToDescendant")
parentId String? @map("parent_id")
parent Department? @relation("ChildParent", fields: [parentId], references: [id])
children Department[] @relation("ChildParent")
domainId String? @map("domain_id")
domainTerms Term[] @relation("TermDom")
deletedAt DateTime? @map("deleted_at")
isDomain Boolean? @default(false) @map("is_domain")
domainStaffs Staff[] @relation("DomainStaff")
deptStaffs Staff[] @relation("DeptStaff")
terms Term[] @relation("department_term")
watchedPost Post[] @relation("post_watch_dept")
hasChildren Boolean? @default(false) @map("has_children")
@@index([parentId])
@@index([isDomain])
@@index([name])
@@index([order])
@@map("department")
}
model DeptAncestry {
id String @id @default(cuid())
ancestorId String? @map("ancestor_id")
descendantId String @map("descendant_id")
relDepth Int @map("rel_depth")
ancestor Department? @relation("AncestorToDescendant", fields: [ancestorId], references: [id])
descendant Department @relation("DescendantToAncestor", fields: [descendantId], references: [id])
// 索引建议
@@index([ancestorId]) // 针对祖先的查询
@@index([descendantId]) // 针对后代的查询
@@index([ancestorId, descendantId]) // 组合索引,用于查询特定的祖先-后代关系
@@index([relDepth]) // 根据关系深度的查询
@@map("dept_ancestry")
}
model RoleMap {
id String @id @default(cuid())
objectId String @map("object_id")
roleId String @map("role_id")
domainId String? @map("domain_id")
objectType String @map("object_type")
role Role @relation(fields: [roleId], references: [id])
@@index([domainId])
@@index([objectId])
@@map("rolemap")
}
model Role {
id String @id @default(cuid())
name String @unique @map("name")
permissions String[] @default([]) @map("permissions")
roleMaps RoleMap[]
system Boolean? @default(false) @map("system")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
@@map("role")
}
model AppConfig {
id String @id @default(cuid())
slug String @unique
title String?
description String?
meta Json?
@@map("app_config")
}
model Post {
// 字符串类型字段
id String @id @default(cuid()) // 帖子唯一标识,使用 cuid() 生成默认值
type String? // Post类型课程、章节、小节、讨论都用Post实现
level String?
state String?
title String? // 帖子标题,可为空
subTitle String?
content String? // 帖子内容,可为空
important Boolean? //是否重要/精选/突出
domainId String? @map("domain_id")
terms Term[] @relation("post_term")
order Float? @default(0) @map("order")
duration Int?
rating Int? @default(0)
// 索引
// 日期时间类型字段
createdAt DateTime @default(now()) @map("created_at")
publishedAt DateTime? @map("published_at") // 发布时间
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at") // 删除时间,可为空
// 关系类型字段
authorId String? @map("author_id")
author Staff? @relation(fields: [authorId], references: [id]) // 帖子作者,关联 Staff 模型
visits Visit[] // 访问记录,关联 Visit 模型
parentId String? @map("parent_id")
parent Post? @relation("PostChildren", fields: [parentId], references: [id]) // 父级帖子,关联 Post 模型
children Post[] @relation("PostChildren") // 子级帖子列表,关联 Post 模型
hasChildren Boolean? @default(false) @map("has_children")
// 闭包表关系
ancestors PostAncestry[] @relation("DescendantPosts")
descendants PostAncestry[] @relation("AncestorPosts")
resources Resource[] // 附件列表
watchableStaffs Staff[] @relation("post_watch_staff") // 可观看的员工列表,关联 Staff 模型
watchableDepts Department[] @relation("post_watch_dept") // 可观看的部门列表,关联 Department 模型
meta Json? // 封面url 视频url objectives具体的学习目标 rating评分Int
// 索引
@@index([type, domainId])
@@index([authorId, type])
@@index([parentId, type])
@@index([parentId, order])
@@index([createdAt])
@@index([updatedAt])
@@index([type, publishedAt])
@@index([state])
@@index([level])
@@index([important])
@@map("post")
}
model PostAncestry {
id String @id @default(cuid())
ancestorId String? @map("ancestor_id")
descendantId String @map("descendant_id")
relDepth Int @map("rel_depth")
ancestor Post? @relation("AncestorPosts", fields: [ancestorId], references: [id])
descendant Post @relation("DescendantPosts", fields: [descendantId], references: [id])
// 复合索引优化
// 索引建议
2025-02-23 20:23:18 +08:00
}
2025-05-13 15:58:47 +08:00
2025-02-22 22:06:44 +08:00
model Message {
id String @id @default(cuid())
url String?
intent String?
option Json?
senderId String? @map("sender_id")
type String?
sender Staff? @relation(name: "message_sender", fields: [senderId], references: [id])
title String?
content String?
receivers Staff[] @relation("message_receiver")
visits Visit[]
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
@@index([type, createdAt])
@@map("message")
}
model Visit {
id String @id @default(cuid()) @map("id")
type String?
views Int @default(1) @map("views")
// sourceIP String? @map("source_ip")
// 关联关系
visitorId String @map("visitor_id")
visitor Staff @relation(fields: [visitorId], references: [id])
postId String? @map("post_id")
post Post? @relation(fields: [postId], references: [id])
message Message? @relation(fields: [messageId], references: [id])
messageId String? @map("message_id")
lectureId String? @map("lecture_id") // 课时ID
// 学习数据
// progress Float? @default(0) @map("progress") // 完成进度(0-100%)
// isCompleted Boolean? @default(false) @map("is_completed") // 是否完成
// lastPosition Int? @default(0) @map("last_position") // 视频播放位置(秒)
// totalWatchTime Int? @default(0) @map("total_watch_time") // 总观看时长(秒)
// // 时间记录
// lastWatchedAt DateTime? @map("last_watched_at") // 最后观看时间
createdAt DateTime @default(now()) @map("created_at") // 创建时间
updatedAt DateTime @updatedAt @map("updated_at") // 更新时间
deletedAt DateTime? @map("deleted_at") // 删除时间,可为空
meta Json?
@@index([postId, type, visitorId])
@@index([messageId, type, visitorId])
@@map("visit")
}
model Resource {
id String @id @default(cuid()) @map("id")
title String? @map("title")
description String? @map("description")
type String? @map("type")
fileId String? @unique
url String?
// 元数据
meta Json? @map("meta")
// 处理状态控制
status String?
createdAt DateTime? @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
createdBy String? @map("created_by")
updatedBy String? @map("updated_by")
deletedAt DateTime? @map("deleted_at")
isPublic Boolean? @default(true) @map("is_public")
owner Staff? @relation(fields: [ownerId], references: [id])
ownerId String? @map("owner_id")
post Post? @relation(fields: [postId], references: [id])
postId String? @map("post_id")
// 索引
@@index([type])
@@index([createdAt])
@@map("resource")
2025-02-23 20:23:18 +08:00
}
//商品表
model Goods {
id String @id @default(cuid()) // 商品ID
name String @unique // 商品名称
2025-05-13 15:58:47 +08:00
description String? // 商品描述
2025-02-23 20:23:18 +08:00
price Float @default(0.0) // 商品价格
images String[] @default([]) // 商品图片
tags Tag[] @relation("GoodsTags") // 多对多关系
2025-05-13 15:58:47 +08:00
reviews Review[] // 一对多关系
2025-02-23 20:23:18 +08:00
createdAt DateTime @default(now()) @map("created_at") // 创建时间
updatedAt DateTime @updatedAt @map("updated_at") // 更新时间
deletedAt DateTime? @map("deleted_at") // 删除时间,可为空
2025-05-13 15:58:47 +08:00
2025-02-23 20:23:18 +08:00
@@index([name])
@@map("goods")
}
// 标签表
model Tag {
2025-05-13 15:58:47 +08:00
id String @id @default(cuid())
name String @unique
goods Goods[] @relation("GoodsTags")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
2025-02-23 20:23:18 +08:00
@@map("tag")
2025-02-22 22:06:44 +08:00
}
2025-02-23 20:23:18 +08:00
model Review {
2025-05-13 15:58:47 +08:00
id String @id @default(cuid())
content String
rating Int @default(0)
goodsId String @map("goods_id")
goods Goods @relation(fields: [goodsId], references: [id])
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime? @map("deleted_at")
2025-02-23 20:23:18 +08:00
@@index([goodsId])
@@index([rating])
@@map("review")
2025-05-13 15:58:47 +08:00
}
model Club {
id Int @id @default(autoincrement())
name String? @unique
introduce String?
car Car[]
driver Driver[]
Game Game[] @relation("ClubGame")
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
@@index([name])
@@map("club")
}
model Driver {
id Int @id @default(autoincrement())
name String?
sex String?
username String?
password String?
club Club? @relation(fields: [clubId], references: [id])
clubId Int? @map("club_id")
parentId Int?
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
@@map("driver")
@@index([name])
}
model Car {
id Int @id @default(autoincrement())
clubId Int?
name String? @unique
type String?
club Club? @relation(fields: [clubId], references: [id])
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
@@map("car")
@@index([name])
}
model Sortie {
totaltime Float @unique
carId Int?
driverId Int?
score Float? @unique
car String?
driver String?
@@map("sortie")
@@index([driverId])
}
model Game {
id Int @id @default(autoincrement())
name String
club Club[] @relation("ClubGame")
clubId Int?
courseId Int?
score Float?
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
@@map("game")
@@index([name])
}
model Class {
id Int @id @default(autoincrement())
name String @unique
students Student[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Student {
id Int @id @default(autoincrement())
name String?
class Class? @relation(fields: [classId], references: [id])
classId Int?
scores Score[]
parentId Int?
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
}
model Course {
id Int @id @default(autoincrement())
name String? @unique
scores Score[]
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
}
model Score {
id Int @id @default(autoincrement())
student Student? @relation(fields: [studentId], references: [id])
studentId Int?
course Course? @relation(fields: [courseId], references: [id])
courseId Int?
score Float?
semester String?
createdAt DateTime? @default(now())
updatedAt DateTime? @updatedAt
@@unique([studentId, courseId, semester])
}