import { relations } from "drizzle-orm"; import { index, integer, pgTable, primaryKey, smallint, timestamp, uuid, varchar } from "drizzle-orm/pg-core"; import { voteEnum } from "./enums"; import { events } from "./events"; import { profiles } from "./profiles"; export const comments = pgTable( "comments", { id: uuid("id").primaryKey().defaultRandom(), eventId: uuid("event_id") .notNull() .references(() => events.id), profileId: uuid("profile_id") .notNull() .references(() => profiles.id), parentId: uuid("parent_id").references((): ReturnType => comments.id), body: varchar("body", { length: 500 }).notNull(), depth: smallint("depth").notNull().default(0), score: integer("score").notNull().default(0), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (t) => [ index("comments_event_id_idx").on(t.eventId), index("comments_profile_id_idx").on(t.profileId), index("comments_parent_id_idx").on(t.parentId), ], ); export const commentsRelations = relations(comments, ({ one, many }) => ({ event: one(events, { fields: [comments.eventId], references: [events.id] }), profile: one(profiles, { fields: [comments.profileId], references: [profiles.id] }), parent: one(comments, { fields: [comments.parentId], references: [comments.id], relationName: "replies" }), replies: many(comments, { relationName: "replies" }), votes: many(commentVotes), })); export const commentVotes = pgTable( "comment_votes", { commentId: uuid("comment_id") .notNull() .references(() => comments.id), profileId: uuid("profile_id") .notNull() .references(() => profiles.id), vote: voteEnum("vote").notNull(), }, (t) => [primaryKey({ columns: [t.commentId, t.profileId] })], ); export const commentVotesRelations = relations(commentVotes, ({ one }) => ({ comment: one(comments, { fields: [commentVotes.commentId], references: [comments.id] }), profile: one(profiles, { fields: [commentVotes.profileId], references: [profiles.id] }), }));