10

I do want to join two table in HTML5 indexed db. I found lot samples to add, Update, Delete and listing record but can't found any samples to join multiple table.

  • Sample URL: *http://users.telenet.be/kristofdegrave/*
muthuvel
  • 1,092
  • 10
  • 17

5 Answers5

6

Was wondering the same question when just generally looking at these technologies. Did not check your particular example, but here is an example from Mozilla on how to do a join if you need it:

https://hacks.mozilla.org/2010/06/comparing-indexeddb-and-webdatabase/

It is quite a lot of code compared to SQL, but I think the idea is that abstracting these using JavaScript later is quite easy.

Ope
  • 777
  • 1
  • 7
  • 13
3

There is no join, but you can open multiple object stores and join during cursor iterations.

Using my own ydn-db library, you can query SELECT * FROM Supplier, Part WHERE Supplier.CITY = Part.CITY by

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.

Josef
  • 2,869
  • 2
  • 22
  • 23
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
2

As far as I know, IndexedDB does not have an API for doing JOINs, yet. The solutions I adopted involved opening a cursor, looping through the results and doing the join manually. It's the dreadful RBAR approach, but I couldn't find a better solution.

Diego
  • 7,312
  • 5
  • 31
  • 38
2

Instead of trying to join, redesign how you store the data so that a join is not required. You do not need to follow the same normalization constraints as SQL when you are using a no-sql approach. No-sql supports storing data redundantly when it is appropriate.

Josh
  • 17,834
  • 7
  • 50
  • 68
1

IndexedDB is in fact an object store and normaly in an object store there is less need for joins because you can just save the nested structure.

In the case you show, data from a codetable gets joined with real data. The case to solve this without a join is just fetch your code table into memory (normally this data never changes) and make the join in code.

Kristof Degrave
  • 4,142
  • 22
  • 32