feat(数据库): 添加笔记表结构

- 新增 notes 表,存储用户笔记数据
- 支持标题、内容、标签等字段
- 关联对话和消息来源
- 添加置顶和归档状态
This commit is contained in:
gaoziman 2025-12-21 16:04:08 +08:00
parent 92ab731c62
commit 6d45e3575d
4 changed files with 1187 additions and 0 deletions

View File

@ -0,0 +1,15 @@
CREATE TABLE "notes" (
"id" serial PRIMARY KEY NOT NULL,
"note_id" varchar(64) NOT NULL,
"user_id" varchar(64) NOT NULL,
"conversation_id" varchar(64),
"message_id" varchar(64),
"title" varchar(255) NOT NULL,
"content" text NOT NULL,
"tags" jsonb DEFAULT '[]'::jsonb,
"is_pinned" boolean DEFAULT false,
"is_archived" boolean DEFAULT false,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "notes_note_id_unique" UNIQUE("note_id")
);

File diff suppressed because it is too large Load Diff

View File

@ -50,6 +50,13 @@
"when": 1766206123948,
"tag": "0006_safe_spitfire",
"breakpoints": true
},
{
"idx": 7,
"version": "7",
"when": 1766299055211,
"tag": "0007_fantastic_molten_man",
"breakpoints": true
}
]
}

View File

@ -262,6 +262,31 @@ export const models = pgTable('models', {
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
});
// ============================================
// 笔记表
// ============================================
export const notes = pgTable('notes', {
id: serial('id').primaryKey(),
// 笔记唯一标识
noteId: varchar('note_id', { length: 64 }).notNull().unique(),
// 关联用户
userId: varchar('user_id', { length: 64 }).notNull(),
// 来源信息
conversationId: varchar('conversation_id', { length: 64 }), // 来源对话
messageId: varchar('message_id', { length: 64 }), // 来源消息
// 笔记内容
title: varchar('title', { length: 255 }).notNull(), // 标题
content: text('content').notNull(), // 内容Markdown
// 分类和标签
tags: jsonb('tags').$type<string[]>().default([]), // 标签数组
// 状态
isPinned: boolean('is_pinned').default(false), // 置顶
isArchived: boolean('is_archived').default(false), // 归档
// 时间戳
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow(),
});
// ============================================
// 关系定义
// ============================================
@ -332,6 +357,22 @@ export const messagesRelations = relations(messages, ({ one }) => ({
}),
}));
// 笔记关系
export const notesRelations = relations(notes, ({ one }) => ({
user: one(users, {
fields: [notes.userId],
references: [users.userId],
}),
conversation: one(conversations, {
fields: [notes.conversationId],
references: [conversations.conversationId],
}),
message: one(messages, {
fields: [notes.messageId],
references: [messages.messageId],
}),
}));
// ============================================
// 类型定义
// ============================================
@ -385,3 +426,6 @@ export type NewAssistant = typeof assistants.$inferInsert;
export type UserFavoriteAssistant = typeof userFavoriteAssistants.$inferSelect;
export type NewUserFavoriteAssistant = typeof userFavoriteAssistants.$inferInsert;
export type Note = typeof notes.$inferSelect;
export type NewNote = typeof notes.$inferInsert;