`title: IndexedDB 索引重建与一致性校验实践``categories: Web 开发/前端/数据管理``keywords: IndexedDB,索引重建,一致性校验,迁移,修复``description: 在模式变更或索引损坏时进行索引重建与一致性校验,采用惰性修复与批处理策略,保障查询与排序稳定性。`重建思路遍历对象仓库数据,按新键写入索引;分批与事务控制避免长事务失败。实现示例async function rebuildIndex(db, storeName, indexName, keySelector) {

return new Promise((resolve, reject) => {

const tx = db.transaction(storeName, 'readwrite');

const s = tx.objectStore(storeName);

s.openCursor().onsuccess = e => {

const c = e.target.result;

if (!c) return;

const v = c.value;

const key = keySelector(v);

s.put({ ...v, [indexName]: key });

c.continue();

};

tx.oncomplete = () => resolve();

tx.onerror = () => reject(tx.error);

});

}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部