prisma社交通知数据库简单设计

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、关系字段:

  • recipientIdrecipient: 通知的接收者(被通知的用户)
  • issuerIdissuer: 通知的发出者(触发通知的用户)
  • postIdpost: 相关的帖子(可选,因为不是所有通知都与帖子相关)

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 字段区分不同类型的通知,以便不同的显示处理
这是社交应用中很常见的功能,类似于 FacebookTwitter 等平台看到的通知系统。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值