model Notification {
id String @id @default(cuid())
recipientId String
recipient User @relation("Recipient", fields: [recipientId], references: [id], onDelete: Cascade)
issuerId String
issuer User @relation("Issuer", fields: [issuerId], references: [id], onDelete: Cascade)
postId String?
post Post? @relation(fields: [postId], references: [id], onDelete: Cascade)
type NotificationType
read Boolean @default(false)
createdAt DateTime @default(now())
@@map("notifications")
}
这个 Notification 模型是用来实现用户通知系统的数据表设计。详细解释一下各个字段的用途:
1、基本字段:
- id: 通知的唯一标识符
- createdAt: 通知创建时间
- read: 标记通知是否已读
2、关系字段:
recipientId和recipient: 通知的接收者(被通知的用户)issuerId和issuer: 通知的发出者(触发通知的用户)postId和post: 相关的帖子(可选,因为不是所有通知都与帖子相关)
3、类型字段:
type: 通知类型,通过NotificationType枚举定义了三种类型:LIKE: 当有人点赞用户的帖子时FOLLOW: 当有人关注该用户时COMMENT: 当有人评论用户的帖子时
4、使用场景示例:
1、当用户A关注用户B时:
{
recipientId: "B的ID",
issuerId: "A的ID",
type: "FOLLOW",
postId: null
}
2、当用户A评论用户B的帖子时:
{
recipientId: "B的ID",
issuerId: "A的ID",
type: "COMMENT",
postId: "被评论帖子的ID"
}
3、当用户A点赞用户B的帖子时:
{
recipientId: "B的ID",
issuerId: "A的ID",
type: "LIKE",
postId: "被点赞帖子的ID"
}
这种设计可以让系统:
追踪所有用户互动产生的通知
显示通知的详细信息(谁对你做了什么操作)
标记通知的已读状态
通过 createdAt 字段按时间顺序显示通知
通过 type 字段区分不同类型的通知,以便不同的显示处理
这是社交应用中很常见的功能,类似于 Facebook、Twitter 等平台看到的通知系统。
592

被折叠的 条评论
为什么被折叠?



