flat: 优化语音
This commit is contained in:
@@ -186,19 +186,51 @@ class IndexedDBHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据
|
||||
* @param {string} storeName
|
||||
* @param {Object} data
|
||||
* @returns {Promise}
|
||||
* 更新数据(支持指定 key 或自动使用 keyPath)
|
||||
* @param {string} storeName 存储对象名
|
||||
* @param {Object} data 待更新数据
|
||||
* @param {IDBValidKey|IDBKeyRange} [key] 可选参数,指定更新的 key
|
||||
* @returns {Promise<string>} 更新结果
|
||||
*/
|
||||
update(storeName, data) {
|
||||
update(storeName, data, key) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = this.db.transaction([storeName], "readwrite");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const request = store.put(data);
|
||||
const keyPath = store.keyPath;
|
||||
|
||||
request.onsuccess = () => resolve("Data updated successfully");
|
||||
request.onerror = (event) => reject(`Update Error: ${event.target.error}`);
|
||||
// 有传入 key:直接使用 key 更新
|
||||
if (key !== undefined) {
|
||||
const request = store.put(data, key);
|
||||
request.onsuccess = () => resolve("数据更新成功(指定 key)");
|
||||
request.onerror = (event) => reject(`更新失败: ${event.target.error}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 无传入 key:依赖 keyPath 更新
|
||||
if (!keyPath) {
|
||||
reject("当前 store 未设置 keyPath,必须传入 key 参数");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查数据是否包含 keyPath 属性
|
||||
let missingKeys = [];
|
||||
if (Array.isArray(keyPath)) {
|
||||
missingKeys = keyPath.filter(k => !data.hasOwnProperty(k));
|
||||
} else if (typeof keyPath === 'string') {
|
||||
if (!data.hasOwnProperty(keyPath)) {
|
||||
missingKeys.push(keyPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (missingKeys.length > 0) {
|
||||
reject(`数据缺少必要的 keyPath 属性: ${missingKeys.join(', ')}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 默认使用 keyPath 更新
|
||||
const request = store.put(data);
|
||||
request.onsuccess = () => resolve("数据更新成功(默认 keyPath)");
|
||||
request.onerror = (event) => reject(`更新失败: ${event.target.error}`);
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user