97 lines
2.6 KiB
Plaintext
97 lines
2.6 KiB
Plaintext
// prisma/schema.prisma - OCS 答题服务数据库 schema
|
||
// 包含 Better Auth 认证表(User, Session, Account, Verification)和业务表(QaRecord)
|
||
|
||
generator client {
|
||
provider = "prisma-client"
|
||
output = "./generated"
|
||
}
|
||
|
||
datasource db {
|
||
provider = "mysql"
|
||
}
|
||
|
||
// Better Auth 标准用户表
|
||
// apiToken 由服务端注册 hook 生成,供 OCS 油猴脚本跨域身份验证
|
||
model User {
|
||
id String @id
|
||
name String
|
||
email String @unique
|
||
emailVerified Boolean
|
||
image String?
|
||
createdAt DateTime
|
||
updatedAt DateTime
|
||
apiToken String? @unique
|
||
/// 用户最近一次清缓存的时间;只有在此时间之后写入的记录才算缓存命中
|
||
cacheClearedAt DateTime?
|
||
sessions Session[]
|
||
accounts Account[]
|
||
qaRecords QaRecord[]
|
||
|
||
@@map("user")
|
||
}
|
||
|
||
// Better Auth 会话表
|
||
model Session {
|
||
id String @id
|
||
expiresAt DateTime
|
||
token String @unique
|
||
createdAt DateTime
|
||
updatedAt DateTime
|
||
ipAddress String?
|
||
userAgent String?
|
||
userId String
|
||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||
|
||
@@map("session")
|
||
}
|
||
|
||
// Better Auth 账号表(用于邮密和 OAuth Provider 关联)
|
||
model Account {
|
||
id String @id
|
||
accountId String
|
||
providerId String
|
||
userId String
|
||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||
accessToken String?
|
||
refreshToken String?
|
||
idToken String? @db.Text
|
||
accessTokenExpiresAt DateTime?
|
||
refreshTokenExpiresAt DateTime?
|
||
scope String?
|
||
password String?
|
||
createdAt DateTime
|
||
updatedAt DateTime
|
||
|
||
@@map("account")
|
||
}
|
||
|
||
// Better Auth 邮箱验证令牌表
|
||
model Verification {
|
||
id String @id
|
||
identifier String
|
||
value String
|
||
expiresAt DateTime
|
||
createdAt DateTime?
|
||
updatedAt DateTime?
|
||
|
||
@@map("verification")
|
||
}
|
||
|
||
// 用户问答记录,按用户隔离存储
|
||
// search 接口在拿到有效 session 或 apiToken 后写入,与当前登录用户绑定
|
||
model QaRecord {
|
||
id String @id @default(cuid())
|
||
userId String
|
||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||
question String @db.Text
|
||
type String
|
||
options String? @db.Text
|
||
answer String? @db.Text
|
||
/// 题目+题型+选项的 MD5,用于 DB 缓存查询;老记录为 null
|
||
hash String? @db.Char(32)
|
||
createdAt DateTime @default(now())
|
||
|
||
@@index([userId, hash])
|
||
@@map("qa_record")
|
||
}
|