Infinite nested comment

I wanna make the nested comment system how do i do that
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
},
},
},
})
I only take the 2 level relation
2 Replies
Sillvva
Sillvva7mo ago
When using a self-referencing column, don't forget to add an explicit return type here
commentRepliedId: text('comments_id').references((): AnySQLiteColumn => comments.id),
commentRepliedId: text('comments_id').references((): AnySQLiteColumn => comments.id),
As for infinite nesting, I'm not sure. You can manually pull a specific number of additional nestings like this:
const displayPosts = await db.query.posts.findFirst({
with: {
comments: {
with: {
children: {
with: {
children: true
}
}
}
}
}
});
const displayPosts = await db.query.posts.findFirst({
with: {
comments: {
with: {
children: {
with: {
children: true
}
}
}
}
}
});
I know many comment sections will have a finite number of nestings. One example being Disqus. It only goes up to 3 levels deep, but it also points out the author of the post it is responding to for clarity.
No description
Byan.
Byan.OP7mo ago
Thanks for the respond, will try this
Want results from more Discord servers?
Add your server