From d9d16d7a7e6bc54f072b35c1c211fd6d18b3e106 Mon Sep 17 00:00:00 2001 From: mr-shortman Date: Tue, 18 Mar 2025 21:49:35 +0100 Subject: [PATCH] added comment vote table --- src/server/db/schema/comments.ts | 37 +++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/server/db/schema/comments.ts b/src/server/db/schema/comments.ts index d846032..3216f0b 100644 --- a/src/server/db/schema/comments.ts +++ b/src/server/db/schema/comments.ts @@ -1,9 +1,17 @@ -import { index, jsonb, timestamp, varchar } from "drizzle-orm/pg-core"; +import { + index, + integer, + jsonb, + primaryKey, + timestamp, + varchar, +} from "drizzle-orm/pg-core"; import { articles } from "./article"; import { JSONContent } from "novel"; import { relations, sql } from "drizzle-orm"; import { createId, createTable } from "./schema-utils"; import { users } from "./auth"; +import { User } from "next-auth"; export const comments = createTable( "comment", @@ -37,3 +45,30 @@ export const comments = createTable( export const commentsRelations = relations(comments, ({ many }) => ({ comments: many(comments), })); + +export type Comment = typeof comments.$inferSelect & { + author?: User; + parent?: Comment; +}; + +export const commentVotes = createTable( + "comment_vote", + { + vote: integer("vote").notNull().$type<1 | -1>().default(1), + commentId: varchar("comment_id", { + length: 255, + }) + .references(() => comments.id) + .notNull(), + userId: varchar("user_id", { + length: 255, + }) + .references(() => users.id) + .notNull(), + }, + (example) => ({ + comboundKey: primaryKey({ + columns: [example.commentId, example.userId], + }), + }), +);