flat: 暂存
This commit is contained in:
@@ -250,6 +250,46 @@ class IndexedDBHelper {
|
||||
request.onerror = (event) => reject(`Delete Error: ${event.target.error}`);
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 根据条件删除所有匹配的数据
|
||||
* @param {string} storeName - 数据仓库名
|
||||
* @param {function} conditionFn - 判断是否删除 (record) => boolean
|
||||
* @returns {Promise}
|
||||
*/
|
||||
deleteByCondition(storeName, conditionFn) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.db) {
|
||||
reject('Database not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
const transaction = this.db.transaction([storeName], 'readwrite');
|
||||
const store = transaction.objectStore(storeName);
|
||||
const request = store.openCursor();
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
const cursor = event.target.result;
|
||||
if (cursor && cursor.value) {
|
||||
try {
|
||||
// console.log(cursor.value)
|
||||
const shouldDelete = conditionFn(cursor.value);
|
||||
if (shouldDelete) {
|
||||
cursor.delete();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Condition function error:', err);
|
||||
}
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve('All matching records deleted successfully');
|
||||
}
|
||||
};
|
||||
|
||||
request.onerror = (event) => {
|
||||
reject(`Delete by condition failed: ${event.target.error}`);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过索引查询数据
|
||||
|
Reference in New Issue
Block a user