import { createId } from '@paralleldrive/cuid2';
export const users = pgTable(...);
export const posts = pgTable(...);
export const comments = pgTable('comments', {
id: text('id').primaryKey().$default(() => createId()),
text: text('text').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
authorId: text('author_id').notNull().references(() => users.id),
postId: text('post_id').notNull().references(() => posts.id, { onDelete: 'cascade' }),
replyToId: text('reply_to_id').references(() => comments.id)
});
export const commentRelations = relations(comments, ({ one, many }) => ({
author: one(users, {
fields: [comments.authorId],
references: [users.id]
}),
post: one(posts, {
fields: [comments.postId],
references: [posts.id]
}),
replyTo: one(comments, {
fields: [comments.replyToId],
references: [comments.id]
}),
replies: many(comments)
}))
import { createId } from '@paralleldrive/cuid2';
export const users = pgTable(...);
export const posts = pgTable(...);
export const comments = pgTable('comments', {
id: text('id').primaryKey().$default(() => createId()),
text: text('text').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
authorId: text('author_id').notNull().references(() => users.id),
postId: text('post_id').notNull().references(() => posts.id, { onDelete: 'cascade' }),
replyToId: text('reply_to_id').references(() => comments.id)
});
export const commentRelations = relations(comments, ({ one, many }) => ({
author: one(users, {
fields: [comments.authorId],
references: [users.id]
}),
post: one(posts, {
fields: [comments.postId],
references: [posts.id]
}),
replyTo: one(comments, {
fields: [comments.replyToId],
references: [comments.id]
}),
replies: many(comments)
}))