2

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?

Jonathan Smith
  • 2,390
  • 1
  • 34
  • 60

1 Answers1

2

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.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
  • @Kyaw, you have a beautiful package there. Using it now for joins, and I get some output to console. I'm wondering how I would get the results of such a join query into my frontend via angular, so say, `$scope.myResult = ??` – KhoPhi Apr 02 '16 at 22:14
  • @Rexford I think you need to call $scope.apply to notify AngularJs digest loop. http://stackoverflow.com/questions/20737343/indexeddb-callback-not-updating-ui-in-angularjs – Kyaw Tun Apr 03 '16 at 03:56
  • @KyawTun I did that, and results get into html, however, they go in pairs of objects in an array. I do not know how to iterate through using ng-repeat in html, so I thought of adding the values[0] and values[1] before sending to html, and still having issues displaying. What is the recommended way to display the output from the joins with your package? How would you do it? This is the question I posted, looking for help: http://stackoverflow.com/questions/36390024/ng-repeat-not-working-with-merged-array-objects – KhoPhi Apr 03 '16 at 21:46
  • @Rexford Please use sorted merge join http://dev.yathit.com/ydn-db/doc/query/key-joining.html there is an example (but not in AngularJS). – Kyaw Tun Apr 04 '16 at 02:06
  • @KyawTun Thanks. Will take a deeper look later today. – KhoPhi Apr 04 '16 at 02:17