`title: IndexedDB 手动关联与连接(Join)模式实践``categories: Web 开发/前端/数据管理``keywords: IndexedDB,Join,关联,多仓库,查询``description: 在 IndexedDB 中通过手动关联实现类似 Join 的查询,采用多对象仓库与索引策略,支持高效的关联检索与分页。`模型与策略拆分仓库:`users` 与 `orders`;在 `orders` 建 `userId` 索引。关联查询示例async function findOrdersWithUser(db, userId, limit = 50) { return new Promise((resolve, reject) => { const tx = db.transaction(['users','orders'], 'readonly'); const users = tx.objectStore('users'); const orders = tx.objectStore('orders'); const out = []; orders.index('byUserId').openCursor(IDBKeyRange.only(userId)).onsuccess = e => { const c = e.target.result; if (!c || out.length >= limit) return resolve(out); const uReq = users.get(userId); uReq.onsuccess = () => { out.push({ order: c.value, user: uReq.result }); c.continue(); }; }; tx.onerror = () => reject(tx.error); }); }

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部
1.993242s