How do I do an JOIN-type query in IndexedDB

孤者浪人 提交于 2020-01-15 05:55:10

问题


I have tried following the tutorial at http://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/ with regards to doing queries in IndexedDB, but their example does not work.

How to I do a JOIN type query in IndexedDB? I have set my objectstores up with indexes, but I just cant seem to get the syntax?


回答1:


IndexedDB is key-value (document) store. It doesn't have JOIN query or querying over multiple object store. However you can query multiple stores in a transaction. That is how suppose to make join query in IndexedDB.

I have a bit of writeup modeling relationship http://dev.yathit.com/ydn-db/schema.html using my library.

Here is joining query for SELECT * FROM Supplier, Part WHERE Supplier.CITY = Part.CITY.

var iter_supplier = new ydn.db.IndexValueIterator('Supplier', 'CITY');
var iter_part = new ydn.db.IndexValueIterator('Part', 'CITY');
var req = db.scan(function(keys, values) {
  var SID = keys[0];
  var PID = keys[1];
  console.log(SID, PID);
  if (!SID || !PID) {
    return []; // done
  }
  var cmp = ydn.db.cmp(SID, PID); // compare keys
  if (cmp == 0) {
    console.log(values[0], values[1]);
    return [true, true]; // advance both
  } else if (cmp == 1) {
    return [undefined, SID]; // jump PID cursor to match SID
  } else {
    return [PID, undefined]; // jump SID cursor to match PID
  }
}, [iter_supplier, iter_part]);

See more detail on Join query article.



来源:https://stackoverflow.com/questions/16547954/how-do-i-do-an-join-type-query-in-indexeddb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!