nexus/packages/db/src/schema/chat_conversations.ts
Mikkel Georgsen 0152d95865 feat(21-01): DB schema, shared types, validators, and chat service with tests
- Add chat_conversations and chat_messages Drizzle schema tables
- Generate migration 0047 with cascade FK and indexes
- Add ChatConversation, ChatMessage, ChatConversationListResponse types
- Add createConversationSchema, updateConversationSchema, createMessageSchema Zod validators
- Implement chatService factory with CRUD, auto-title on first message, updatedAt bump
- Add chat-service test suite (12 tests, all passing)

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-01 13:01:28 +02:00

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),
}),
);