feat(数据库): 添加对话分享表和迁移文件

- 新增 sharedConversations 表用于存储分享信息
- 支持分享短链接码、内容控制选项、浏览统计
- 添加选择性消息分享功能(selectedMessageIds)
- 完善表关系定义和类型导出
This commit is contained in:
gaoziman 2025-12-24 15:57:47 +08:00
parent 6047af071c
commit ea438eea72
6 changed files with 2734 additions and 0 deletions

View File

@ -0,0 +1,18 @@
CREATE TABLE "shared_conversations" (
"id" serial PRIMARY KEY NOT NULL,
"share_id" varchar(64) NOT NULL,
"conversation_id" varchar(64) NOT NULL,
"user_id" varchar(64) NOT NULL,
"share_code" varchar(12) NOT NULL,
"title" varchar(255),
"description" text,
"include_thinking" boolean DEFAULT true,
"include_tool_calls" boolean DEFAULT false,
"include_images" boolean DEFAULT true,
"view_count" integer DEFAULT 0,
"is_active" boolean DEFAULT true,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "shared_conversations_share_id_unique" UNIQUE("share_id"),
CONSTRAINT "shared_conversations_share_code_unique" UNIQUE("share_code")
);

View File

@ -0,0 +1 @@
ALTER TABLE "shared_conversations" ADD COLUMN "snapshot_at" timestamp with time zone;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -92,6 +92,20 @@
"when": 1766339459689,
"tag": "0012_flippant_marvel_apes",
"breakpoints": true
},
{
"idx": 13,
"version": "7",
"when": 1766540812748,
"tag": "0013_charming_natasha_romanoff",
"breakpoints": true
},
{
"idx": 14,
"version": "7",
"when": 1766546111189,
"tag": "0014_naive_kronos",
"breakpoints": true
}
]
}

View File

@ -313,6 +313,40 @@ export const promptOptimizations = pgTable('prompt_optimizations', {
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
});
// ============================================
// 对话分享表
// ============================================
export const sharedConversations = pgTable('shared_conversations', {
id: serial('id').primaryKey(),
// 分享唯一标识
shareId: varchar('share_id', { length: 64 }).notNull().unique(),
// 关联对话
conversationId: varchar('conversation_id', { length: 64 }).notNull(),
// 分享创建者
userId: varchar('user_id', { length: 64 }).notNull(),
// 短链接码(用于分享 URL
shareCode: varchar('share_code', { length: 12 }).notNull().unique(),
// 分享标题(可自定义)
title: varchar('title', { length: 255 }),
// 分享描述
description: text('description'),
// 内容控制
includeThinking: boolean('include_thinking').default(true),
includeToolCalls: boolean('include_tool_calls').default(false),
includeImages: boolean('include_images').default(true),
// 统计信息
viewCount: integer('view_count').default(0),
// 状态
isActive: boolean('is_active').default(true),
// 快照时间点(只显示此时间之前的消息)
snapshotAt: timestamp('snapshot_at', { withTimezone: true }),
// 选择的消息ID列表JSON数组null 表示全部消息)
selectedMessageIds: jsonb('selected_message_ids').$type<string[]>(),
// 时间戳
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
});
// ============================================
// 关系定义
// ============================================
@ -399,6 +433,18 @@ export const notesRelations = relations(notes, ({ one }) => ({
}),
}));
// 分享关系
export const sharedConversationsRelations = relations(sharedConversations, ({ one }) => ({
user: one(users, {
fields: [sharedConversations.userId],
references: [users.userId],
}),
conversation: one(conversations, {
fields: [sharedConversations.conversationId],
references: [conversations.conversationId],
}),
}));
// ============================================
// 类型定义
// ============================================
@ -469,3 +515,6 @@ export type NewNote = typeof notes.$inferInsert;
export type PromptOptimization = typeof promptOptimizations.$inferSelect;
export type NewPromptOptimization = typeof promptOptimizations.$inferInsert;
export type SharedConversation = typeof sharedConversations.$inferSelect;
export type NewSharedConversation = typeof sharedConversations.$inferInsert;