Byan.
DTDrizzle Team
•Created by Byan. on 5/5/2024 in #help
Infinite nested comment
I wanna make the nested comment system how do i do that
I only take the 2 level relation
export const posts = sqliteTable('posts', {
id: text('id').primaryKey().notNull(),
userId: text('user_id')
.notNull()
.references(() => users.id),
content: text('content'),
createdAt: text('created_at').default(sql`CURRENT_TIMESTAMP`)
})
export const comments = sqliteTable('comments', {
id: text('id').primaryKey().notNull(),
content: text('content'),
commentRepliedId: text('comments_id')
.references(() => comments.id),
userId: text('user_id')
.notNull()
.references(() => users.id),
postId: text('post_id')
.notNull()
.references(() => posts.id),
})
export const posts = sqliteTable('posts', {
id: text('id').primaryKey().notNull(),
userId: text('user_id')
.notNull()
.references(() => users.id),
content: text('content'),
createdAt: text('created_at').default(sql`CURRENT_TIMESTAMP`)
})
export const comments = sqliteTable('comments', {
id: text('id').primaryKey().notNull(),
content: text('content'),
commentRepliedId: text('comments_id')
.references(() => comments.id),
userId: text('user_id')
.notNull()
.references(() => users.id),
postId: text('post_id')
.notNull()
.references(() => posts.id),
})
export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.userId],
references: [users.id],
}),
likes: many(likes),
comments: many(comments),
}));
export const commentsRelation = relations(comments, ({one, many}) => ({
post: one(posts, {
fields: [comments.postId],
references: [posts.id],
}),
parent: one(comments, {
fields: [comments.commentRepliedId],
references: [comments.id],
relationName: "ParentComment",
}),
children: many(comments, { relationName: "ParentComment" }),
}))
export const postsRelations = relations(posts, ({ one, many }) => ({
author: one(users, {
fields: [posts.userId],
references: [users.id],
}),
likes: many(likes),
comments: many(comments),
}));
export const commentsRelation = relations(comments, ({one, many}) => ({
post: one(posts, {
fields: [comments.postId],
references: [posts.id],
}),
parent: one(comments, {
fields: [comments.commentRepliedId],
references: [comments.id],
relationName: "ParentComment",
}),
children: many(comments, { relationName: "ParentComment" }),
}))
const displayPosts = await db.query.posts.findFirst({
with: {
comments: {
with: {
children: true
},
},
},
})
const displayPosts = await db.query.posts.findFirst({
with: {
comments: {
with: {
children: true
},
},
},
})
3 replies