diff --git a/packages/common/prisma/schema.prisma b/packages/common/prisma/schema.prisma index 8ad7da3..1042412 100644 --- a/packages/common/prisma/schema.prisma +++ b/packages/common/prisma/schema.prisma @@ -44,7 +44,8 @@ model Term { createdBy String? @map("created_by") depts Department[] @relation("department_term") hasChildren Boolean? @default(false) @map("has_children") - posts Post[] @relation("post_term") + posts Post[] @relation("post_term") + @@index([name]) // 对name字段建立索引,以加快基于name的查找速度 @@index([parentId]) // 对parentId字段建立索引,以加快基于parentId的查找速度 @@map("term") @@ -87,14 +88,14 @@ model Staff { deletedAt DateTime? @map("deleted_at") officerId String? @map("officer_id") - watchedPost Post[] @relation("post_watch_staff") + watchedPost Post[] @relation("post_watch_staff") visits Visit[] posts Post[] - sentMsgs Message[] @relation("message_sender") - receivedMsgs Message[] @relation("message_receiver") + sentMsgs Message[] @relation("message_sender") + receivedMsgs Message[] @relation("message_receiver") registerToken String? enrollments Enrollment[] - teachedPosts PostInstructor[] + teachedPosts PostInstructor[] ownedResources Resource[] @@index([officerId]) @@ -186,41 +187,42 @@ model AppConfig { model Post { // 字符串类型字段 - id String @id @default(cuid()) // 帖子唯一标识,使用 cuid() 生成默认值 + id String @id @default(cuid()) // 帖子唯一标识,使用 cuid() 生成默认值 type String? // Post类型,课程、章节、小节、讨论都用Post实现 - level String? - state String? + level String? + state String? title String? // 帖子标题,可为空 - subTitle 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? - + important Boolean? //是否重要/精选/突出 + domainId String? @map("domain_id") + terms Term[] @relation("post_term") + order Float? @default(0) @map("order") + duration Int? + // 日期时间类型字段 - createdAt DateTime @default(now()) @map("created_at") - publishedAt DateTime? @map("published_at") // 发布时间 - updatedAt DateTime @updatedAt @map("updated_at") - deletedAt DateTime? @map("deleted_at") // 删除时间,可为空 - instructors PostInstructor[] + createdAt DateTime @default(now()) @map("created_at") + publishedAt DateTime? @map("published_at") // 发布时间 + updatedAt DateTime @updatedAt @map("updated_at") + deletedAt DateTime? @map("deleted_at") // 删除时间,可为空 + instructors PostInstructor[] // 关系类型字段 - authorId String? @map("author_id") - author Staff? @relation(fields: [authorId], references: [id]) // 帖子作者,关联 Staff 模型 - enrollments Enrollment[] // 学生报名记录 - 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 + authorId String? @map("author_id") + author Staff? @relation(fields: [authorId], references: [id]) // 帖子作者,关联 Staff 模型 + enrollments Enrollment[] // 学生报名记录 + 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]) @@ -236,15 +238,15 @@ model 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]) + 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]) // 复合索引优化 - // 索引建议 + // 索引建议 @@index([ancestorId]) // 针对祖先的查询 @@index([descendantId]) // 针对后代的查询 @@index([ancestorId, descendantId]) // 组合索引,用于查询特定的祖先-后代关系 @@ -292,18 +294,16 @@ model Visit { // 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") // 更新时间 + createdAt DateTime @default(now()) @map("created_at") // 创建时间 + updatedAt DateTime @updatedAt @map("updated_at") // 更新时间 deletedAt DateTime? @map("deleted_at") // 删除时间,可为空 - meta Json? + meta Json? @@index([postId, type, visitorId]) @@index([messageId, type, visitorId]) @@map("visit") } - - model Enrollment { id String @id @default(cuid()) @map("id") status String @map("status") @@ -316,8 +316,8 @@ model Enrollment { // 关联关系 student Staff @relation(fields: [studentId], references: [id]) studentId String @map("student_id") - post Post @relation(fields: [postId], references: [id]) - postId String @map("post_id") + post Post @relation(fields: [postId], references: [id]) + postId String @map("post_id") @@unique([studentId, postId]) @@index([status]) @@ -326,14 +326,14 @@ model Enrollment { } model PostInstructor { - postId String @map("post_id") + postId String @map("post_id") instructorId String @map("instructor_id") role String @map("role") createdAt DateTime @default(now()) @map("created_at") order Float? @default(0) @map("order") - post Post @relation(fields: [postId], references: [id]) - instructor Staff @relation(fields: [instructorId], references: [id]) + post Post @relation(fields: [postId], references: [id]) + instructor Staff @relation(fields: [instructorId], references: [id]) @@id([postId, instructorId]) @@map("post_instructor") @@ -347,7 +347,7 @@ model Resource { fileId String? @unique url String? // 元数据 - meta Json? @map("meta") + meta Json? @map("meta") // 处理状态控制 status String? createdAt DateTime? @default(now()) @map("created_at") @@ -360,7 +360,7 @@ model Resource { ownerId String? @map("owner_id") post Post? @relation(fields: [postId], references: [id]) postId String? @map("post_id") - + // 索引 @@index([type]) @@index([createdAt]) @@ -404,3 +404,20 @@ model NodeEdge { @@index([targetId]) @@map("node_edge") } + +model Animal { + id String @id @default(cuid()) + name String + age Int + gender Boolean + personId String? + person Person? @relation(fields: [personId], references: [id]) +} + +model Person { + id String @id @default(cuid()) + name String + age Int + gender Boolean + animals Animal[] +}