- Create chatConversations table with companyId FK, agentId FK (set null), pinned/archived/deleted timestamps - Create chatMessages table with conversationId FK (cascade delete), role, content, agentId - Export both tables from packages/db/src/schema/index.ts - Generate migration 0047_nebulous_klaw.sql with full DDL, FK constraints, and indexes
22 lines
1.1 KiB
TypeScript
22 lines
1.1 KiB
TypeScript
import { pgTable, uuid, text, timestamp, index } from "drizzle-orm/pg-core";
|
|
import { companies } from "./companies.js";
|
|
import { agents } from "./agents.js";
|
|
|
|
export const chatConversations = pgTable(
|
|
"chat_conversations",
|
|
{
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
companyId: uuid("company_id").notNull().references(() => companies.id),
|
|
title: text("title"),
|
|
agentId: uuid("agent_id").references(() => agents.id, { onDelete: "set null" }),
|
|
pinnedAt: timestamp("pinned_at", { withTimezone: true }),
|
|
archivedAt: timestamp("archived_at", { withTimezone: true }),
|
|
deletedAt: timestamp("deleted_at", { withTimezone: true }),
|
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
|
},
|
|
(table) => ({
|
|
companyUpdatedIdx: index("chat_conversations_company_updated_idx").on(table.companyId, table.updatedAt),
|
|
companyDeletedIdx: index("chat_conversations_company_deleted_idx").on(table.companyId, table.deletedAt),
|
|
}),
|
|
);
|