概述批量读取能降低交互成本。本文展示在对象仓库与索引上的批量与范围读取。批量与索引范围function openDB(name) { return new Promise((resolve, reject) => { const r = indexedDB.open(name, 1); r.onupgradeneeded = () => { const db = r.result; const s = db.createObjectStore('items', { keyPath:'id' }); s.createIndex('byType','type'); }; r.onsuccess = () => resolve(r.result); r.onerror = () => reject(r.error); }); } async function readAll() { const db = await openDB('bulk'); const tx = db.transaction('items','readonly'); const s = tx.objectStore('items'); const vals = await s.getAll(); db.close(); return vals; } async function readKeys() { const db = await openDB('bulk'); const tx = db.transaction('items','readonly'); const s = tx.objectStore('items'); const keys = await s.getAllKeys(); db.close(); return keys; } async function readByType(type) { const db = await openDB('bulk'); const tx = db.transaction('items','readonly'); const idx = tx.objectStore('items').index('byType'); const vals = await idx.getAll(type); db.close(); return vals; }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
1.715393s