概述IndexedDB 提供浏览器端结构化存储与事务模型。通过版本升级创建/迁移对象仓库(objectStore)与索引,使用只读/读写事务进行原子操作。用法/示例const req = indexedDB.open('app-db', 3) req.onupgradeneeded = e => { const db = e.target.result if (!db.objectStoreNames.contains('users')) { const store = db.createObjectStore('users', { keyPath: 'id' }) store.createIndex('by_email', 'email', { unique: true }) } } req.onsuccess = () => { const db = req.result const tx = db.transaction(['users'], 'readwrite') const store = tx.objectStore('users') store.put({ id: 1, email: '[email protected]', name: 'A' }) tx.oncomplete = () => db.close() } // 游标遍历 const tx = db.transaction(['users'], 'readonly') const store = tx.objectStore('users') store.openCursor().onsuccess = e => { const cursor = e.target.result if (cursor) { // 处理 cursor.value cursor.continue() } } 工程建议在版本升级中进行迁移脚本管理与回滚保护;避免在运行时创建仓库。以事务为边界控制一致性;处理 `onabort/onerror` 并上报失败。对大对象采用分块与引用,结合存储配额与清理策略。参考与验证MDN:IndexedDB — https://developer.mozilla.org/docs/Web/API/IndexedDB_APIweb.dev:IndexedDB — https://web.dev/articles/indexeddb

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部