refactor(db): 重构会话表主键结构并清理旧数据
- 将会话表主键从自增id改为index_name - 添加数据库版本升级逻辑清理旧数据 - 更新所有相关操作方法使用新主键 - 添加详细的版本变更注释
This commit is contained in:
parent
0b634e8cdd
commit
c64a562913
@ -181,7 +181,7 @@ export const useTalkStore = defineStore('talk', {
|
||||
|
||||
// 更新状态和本地数据库
|
||||
this.items = serverItems
|
||||
|
||||
console.log('serverItems',serverItems)
|
||||
// 将最新的会话列表保存到本地数据库
|
||||
for (const item of serverItems) {
|
||||
await addOrUpdateConversation(item)
|
||||
|
@ -18,7 +18,7 @@ export function isLoggedIn() {
|
||||
*/
|
||||
export function getAccessToken() {
|
||||
// return storage.get(AccessToken) || ''
|
||||
return JSON.parse(localStorage.getItem('token'))||'46d71a72d8d845ad7ed23eba9bdde260e635407190c2ce1bf7fd22088e41682ea07773ec65cae8946d2003f264d55961f96e0fc5da10eb96d3a348c1664e9644ce2108c311309f398ae8ea1b8200bfd490e5cb6e8c52c9e5d493cbabb163368f8351420451a631dbfa749829ee4cda49b77b5ed2d3dced5d0f2b7dd9ee76ba5465c84a17c23af040cd92b6b2a4ea48befbb5c729dcdad0a9c9668befe84074cc24f78899c1d947f8e7f94c7eda5325b8ed698df729e76febb98549ef3482ae942fb4f4a1c92d21836fa784728f0c5483aab2760a991b6b36e6b10c84f840a6433a6ecc31dee36e8f1c6158818bc89d22f227a3ad4383a2ddef0a2f43b855f869560968fd2dae0412498273e591b78554b2373a17017cdaae7c9ec427325b7d078d54cb00b001c9894c8e1f990747c8db3b62b17eb8ed39e2b2c2b6d63ce26756'
|
||||
return JSON.parse(localStorage.getItem('token'))||'46d71a72d8d845ad7ed23eba9bdde260e635407190c2ce1bf7fd22088e41682ea07773ec65cae8946d2003f264d55961f96e0fc5da10eb96d3a348c1664e9644ce2108c311309f398ae8ea1b8200bfd490e5cb6e8c52c9e5d493cbabb163368f8351420451a631dbfa749829ee4cda49b77b5ed2d3dced5d0f2b7dd9ee76ba5465c84a17c23af040cd92b6b2a4ea48befbb5c729dcdad0a9c9668befe84074cc24f78899c1d947f8e7f94c7eda5325b8ed698df729e76febb98549ef3482ae942fb4f4a1c92d21836fa784728f0c5483aab2760a991b6b36e6b10c84f840a6433a6ecc31dee36e8f1c6158818bc89d22eec7a138bb20774ef183e109945229d43e1f63fb01cdee46f5f663037f4ed946a0c04441b1f642c945d218180e84e91d272dc621be157602785ef226dd21b9b6c92c292bc73be90fad0320bad0812e11'
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,11 +1,30 @@
|
||||
|
||||
import Dexie from 'dexie';
|
||||
|
||||
/**
|
||||
* 聊天历史数据库
|
||||
* 版本5-6: 修复会话表主键问题
|
||||
* - 版本5: 删除旧的会话表结构
|
||||
* - 版本6: 使用index_name作为主键重新创建会话表
|
||||
*
|
||||
* 注意: Dexie不支持直接更改主键,必须通过删除并重建表的方式实现
|
||||
*/
|
||||
export const db = new Dexie('chatHistory');
|
||||
|
||||
// 定义数据库表结构和索引
|
||||
// 版本3:优化了索引,提高了查询和排序性能
|
||||
db.version(4).stores({
|
||||
// 版本6:修复主键更改问题
|
||||
// 版本5:删除旧的会话表
|
||||
db.version(5).stores({
|
||||
conversations: null
|
||||
}).upgrade(function(trans) {
|
||||
// 确保物理删除表
|
||||
if (trans.idbtrans.db.objectStoreNames.contains('conversations')) {
|
||||
trans.idbtrans.db.deleteObjectStore('conversations');
|
||||
}
|
||||
});
|
||||
|
||||
// 版本6:使用新的主键结构重新创建会话表
|
||||
db.version(6).stores({
|
||||
/**
|
||||
* 聊天记录表
|
||||
* - msg_id: 消息唯一ID (主键)
|
||||
@ -18,12 +37,20 @@ db.version(4).stores({
|
||||
|
||||
/**
|
||||
* 会话表
|
||||
* - ++id: 自增主键
|
||||
* - &index_name: 唯一索引 (talk_type + '_' + receiver_id)
|
||||
* - index_name: 主键 (talk_type + '_' + receiver_id)
|
||||
* - updated_at: 索引,用于排序
|
||||
* - is_top: 索引,用于置顶排序
|
||||
*/
|
||||
conversations: 'id, &index_name, talk_type, receiver_id, updated_at, unread_num, is_top',
|
||||
conversations: 'index_name, talk_type, receiver_id, updated_at, unread_num, is_top',
|
||||
});
|
||||
|
||||
// 清理旧版本数据
|
||||
db.on('versionchange', function(event) {
|
||||
if (event.oldVersion < 6 && event.newVersion >= 6) {
|
||||
console.log('数据库版本升级到6,清理旧数据');
|
||||
db.conversations.clear();
|
||||
console.log('会话表数据已清理,主键结构已更新');
|
||||
}
|
||||
});
|
||||
|
||||
db.on('ready', () => {
|
||||
@ -219,14 +246,24 @@ export async function deleteMessage(msgId) {
|
||||
/**
|
||||
* 添加或更新会话
|
||||
* @param {object} conversation - 会话对象
|
||||
* @returns {Promise<number>} 会话ID
|
||||
* @returns {Promise<string>} 会话索引名称
|
||||
*/
|
||||
export async function addOrUpdateConversation(conversation) {
|
||||
try {
|
||||
// put 方法会根据唯一索引 index_name 自动判断是添加还是更新
|
||||
// 确保 index_name 存在,这是会话表的主键
|
||||
if (!conversation.index_name && conversation.talk_type && conversation.receiver_id) {
|
||||
conversation.index_name = `${conversation.talk_type}_${conversation.receiver_id}`;
|
||||
}
|
||||
|
||||
if (!conversation.index_name) {
|
||||
throw new Error('无法添加会话:缺少必要的index_name或无法生成');
|
||||
}
|
||||
|
||||
// 使用 put 方法,如果主键已存在则更新,否则添加
|
||||
return await db.conversations.put(conversation);
|
||||
} catch (error) {
|
||||
console.error('添加或更新会话失败:', error);
|
||||
console.error('错误详情:', error.message, error.stack);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@ -274,7 +311,7 @@ export async function getConversations(includeEmpty = false) {
|
||||
export async function getConversation(talkType, receiverId) {
|
||||
try {
|
||||
const indexName = `${talkType}_${receiverId}`;
|
||||
return await db.conversations.get({ index_name: indexName });
|
||||
return await db.conversations.get(indexName);
|
||||
} catch (error) {
|
||||
console.error('获取会话失败:', error);
|
||||
throw error;
|
||||
@ -291,11 +328,11 @@ export async function getConversation(talkType, receiverId) {
|
||||
export async function updateConversationUnreadNum(talkType, receiverId, unreadNum = null) {
|
||||
try {
|
||||
const indexName = `${talkType}_${receiverId}`;
|
||||
const conversation = await db.conversations.get({ index_name: indexName });
|
||||
const conversation = await db.conversations.get(indexName);
|
||||
|
||||
if (conversation) {
|
||||
const newUnreadNum = unreadNum === null ? (conversation.unread_num || 0) + 1 : unreadNum;
|
||||
return await db.conversations.update(conversation.id, { unread_num: newUnreadNum });
|
||||
return await db.conversations.update(indexName, { unread_num: newUnreadNum });
|
||||
}
|
||||
return 0;
|
||||
} catch (error) {
|
||||
@ -316,18 +353,18 @@ export function clearConversationUnreadNum(talkType, receiverId) {
|
||||
|
||||
/**
|
||||
* 删除会话及其相关的消息
|
||||
* @param {number} conversationId - 会话ID
|
||||
* @param {string} indexName - 会话索引名称
|
||||
* @param {boolean} [deleteMessages=false] - 是否同时删除相关的消息记录
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function deleteConversation(conversationId, deleteMessages = false) {
|
||||
export async function deleteConversation(indexName, deleteMessages = false) {
|
||||
try {
|
||||
await db.transaction('rw', db.conversations, db.messages, async () => {
|
||||
const conversation = await db.conversations.get(conversationId);
|
||||
const conversation = await db.conversations.get(indexName);
|
||||
if (!conversation) return;
|
||||
|
||||
// 删除会话
|
||||
await db.conversations.delete(conversationId);
|
||||
await db.conversations.delete(indexName);
|
||||
|
||||
// 如果需要,删除关联的消息
|
||||
if (deleteMessages) {
|
||||
@ -352,7 +389,7 @@ export async function updateConversationLastMessage(message) {
|
||||
const targetReceiverId = talk_type === TalkType.PRIVATE ? (user_id === receiver_id ? user_id : receiver_id) : receiver_id;
|
||||
const indexName = `${talk_type}_${targetReceiverId}`;
|
||||
|
||||
const conversation = await db.conversations.get({ index_name: indexName });
|
||||
const conversation = await db.conversations.get(indexName);
|
||||
if (!conversation) return 0;
|
||||
|
||||
let msgText = '';
|
||||
@ -367,7 +404,7 @@ export async function updateConversationLastMessage(message) {
|
||||
default: msgText = '[未知消息]';
|
||||
}
|
||||
|
||||
return await db.conversations.update(conversation.id, {
|
||||
return await db.conversations.update(indexName, {
|
||||
msg_text: msgText,
|
||||
content: message.content || '',
|
||||
updated_at: message.created_at,
|
||||
|
Loading…
Reference in New Issue
Block a user