import { index, pgTable, primaryKey, text, timestamp, uuid, } from "drizzle-orm/pg-core"; import { users } from "./users"; export const conversations = pgTable("conversations", { id: uuid("id").primaryKey().defaultRandom(), createdAt: timestamp("created_at", { withTimezone: true }) .notNull() .defaultNow(), updatedAt: timestamp("updated_at", { withTimezone: true }) .notNull() .defaultNow(), }); export const conversationParticipants = pgTable( "conversation_participants", { conversationId: uuid("conversation_id") .notNull() .references(() => conversations.id, { onDelete: "cascade" }), userId: uuid("user_id") .notNull() .references(() => users.uuid, { onDelete: "cascade" }), joinedAt: timestamp("joined_at", { withTimezone: true }) .notNull() .defaultNow(), }, (t) => ({ pk: primaryKey({ columns: [t.conversationId, t.userId] }), }), ); export const messages = pgTable( "messages", { id: uuid("id").primaryKey().defaultRandom(), conversationId: uuid("conversation_id") .notNull() .references(() => conversations.id, { onDelete: "cascade" }), senderId: uuid("sender_id") .notNull() .references(() => users.uuid, { onDelete: "cascade" }), text: text("text").notNull(), createdAt: timestamp("created_at", { withTimezone: true }) .notNull() .defaultNow(), readAt: timestamp("read_at", { withTimezone: true }), }, (table) => ({ conversationIdIdx: index("messages_conversation_id_idx").on( table.conversationId, ), senderIdIdx: index("messages_sender_id_idx").on(table.senderId), }), ); export type ConversationInDb = typeof conversations.$inferSelect; export type NewConversationInDb = typeof conversations.$inferInsert; export type MessageInDb = typeof messages.$inferSelect; export type NewMessageInDb = typeof messages.$inferInsert;