概述IndexedDB 不直接支持复杂逻辑,但可通过结果集集合运算实现。本文给出并集与交集示例。集合运算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'); s.createIndex('byStatus','status'); }; r.onsuccess = () => resolve(r.result); r.onerror = () => reject(r.error); }); } async function queryUnion(type, status) { const db = await openDB('comb'); const tx = db.transaction('items','readonly'); const idxType = tx.objectStore('items').index('byType'); const idxStatus = tx.objectStore('items').index('byStatus'); const a = await idxType.getAll(type); const b = await idxStatus.getAll(status); db.close(); const map = new Map(); for (const x of a.concat(b)) map.set(x.id, x); return Array.from(map.values()); } async function queryIntersect(type, status) { const db = await openDB('comb'); const tx = db.transaction('items','readonly'); const idxType = tx.objectStore('items').index('byType'); const idxStatus = tx.objectStore('items').index('byStatus'); const a = await idxType.getAll(type); const setA = new Set(a.map(x => x.id)); const b = await idxStatus.getAll(status); db.close(); return b.filter(x => setA.has(x.id)); }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
2.379551s