川柳 Hub 的评论系统支持三级嵌套回复,后端基于 NestJS + TypeORM,前端基于 Vue3 + NaiveUI,共享类型和工具函数通过 @chuanliu-hub/shared 包统一管理。
@chuanliu-hub/shared
id
content
user
post
parent
rootComment
createdAt
一级评论(根评论) parent = null, rootComment = null 二级评论(直接回复) parent = 一级评论, rootComment = 一级评论 三级评论(回复的回复) parent = 二级/三级评论, rootComment = 一级评论
核心设计:每条非根评论都直接持有 rootComment 引用,指向它所属的根评论。这样查询时可以高效地按根评论分组,无需递归遍历。
export interface Comment { id: string content: string user: CommentUser createdAt: string | Date parent?: Comment | null // 直接父评论 rootComment?: Comment | null // 所属根评论 post?: { id: string } }
// 根评论 = Comment + 扁平 replies 列表 export interface RootComment extends Comment { replies: Comment[] }
// packages/shared/src/utils/commentLevel.ts export function getCommentLevel(comment: { parent?: { id: string } | null rootComment?: { id: string } | null }): 1 | 2 | 3 export function isLevel3(comment): boolean
判断逻辑:
parent === null
rootComment.id === parent.id
rootComment.id !== parent.id
评论系统设计
川柳 Hub 的评论系统支持三级嵌套回复,后端基于 NestJS + TypeORM,前端基于 Vue3 + NaiveUI,共享类型和工具函数通过
@chuanliu-hub/shared包统一管理。数据模型
评论表(comments)
idcontentuserpostparentrootCommentcreatedAt三级评论体系
核心设计:每条非根评论都直接持有
rootComment引用,指向它所属的根评论。这样查询时可以高效地按根评论分组,无需递归遍历。共享类型(@chuanliu-hub/shared)
Comment 接口
RootComment 接口
评论层级工具
判断逻辑:
parent === nullrootComment.id === parent.idrootComment.id !== parent.id