53129
Pa
PAT / files
PAT / src / lib / db / schema.ts
1.5 KB
1import { relations } from "drizzle-orm"; 2import { integer, pgTable, serial, text, timestamp } from "drizzle-orm/pg-core"; 3 4export const threads = pgTable("threads", { 5 id: serial("id").primaryKey(), 6 title: text("title"), 7 model: text("model").notNull(), 8 maxPromptLength: integer("max_prompt_length"), // null, 1024, or 4096 9 userPrompt: text("user_prompt"), // User-defined prompt to include in system prompt 10 createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), 11 updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), 12}); 13 14export const messages = pgTable("messages", { 15 id: serial("id").primaryKey(), 16 threadId: integer("thread_id") 17 .notNull() 18 .references(() => threads.id, { onDelete: "cascade" }), 19 role: text("role").notNull(), // "user" or "assistant" 20 content: text("content").notNull(), 21 thinking: text("thinking"), // Thinking (for assistant messages) 22 createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), 23}); 24 25// Define relations 26export const threadsRelations = relations(threads, ({ many }) => ({ 27 messages: many(messages), 28})); 29 30export const messagesRelations = relations(messages, ({ one }) => ({ 31 thread: one(threads, { 32 fields: [messages.threadId], 33 references: [threads.id], 34 }), 35})); 36 37export type Thread = typeof threads.$inferSelect; 38export type NewThread = typeof threads.$inferInsert; 39export type Message = typeof messages.$inferSelect; 40export type NewMessage = typeof messages.$inferInsert;