2

Is it possible to search an indexedDb object store with a "bound" keyRange on multiple keys. With the following data:

  let simpleA = {
    id: 'A',
    bottomX: 2.5,
    bottomY: 5.5,
    topX: 3.5,
    topY: 6.5
  };

  let simpleB = {
    id: 'B',
    bottomX: 4.2,
    bottomY: 4.2,
    topX: 4.8,
    topY: 4.8
  };

  let simpleC = {
    id: 'C',
    bottomX: 8.5,
    bottomY: 6.5,
    topX: 9.5,
    topY: 7.5
  };

I have tried to create a index on multiple keys:

simpleStore.createIndex('bottomX bottomY topX topY', ['bottomX', 'bottomY', 'topX', 'topY']);

and a key range like this:

let keyRange = IDBKeyRange.bound(
      [0,5,0,0],
      [10,6,10,10]
);

And then creating a cursor:

let mycursor = index.openCursor(keyRange);

I was hoping that this would give me a result of simpleA and simpleB but not simpleC, because the bottomY property is not in the range. However, as I tested this code, I realized that only the first key of the index is tests with the bound keyrange.

Is there another way to achieve this range search on multiple keys?

For infos, I got the inspiration of the multiple index from this post: Javascript: Searching indexeddb using multiple indexes

Community
  • 1
  • 1
Ben
  • 674
  • 9
  • 21

2 Answers2

2

You have to manually combine all four individual key range queries.

IndexedDB query support varying only the last key. For example, the following query will work.

let keyRange = IDBKeyRange.bound(
      [0,5,10,0],
      [0,5,10,10]
);
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
1

I believe the question you're asking is equivalent to:

Indexed DB cursor ranges on mulitiple properties

Which is to say, you're trying to do query bounded by multiple dimensions. Indexed DB queries are (currently) limited to ranges, and ranges are 1-dimensional. So the range start [s1, s2] = [0, 0, 0] to end [e1, e2] = [2, 2] includes value [v1, v2] = [1, 3] because s1 <= v1 <= e1, so v2 isn't considered.

There's a solution linked to from that article.

Community
  • 1
  • 1
Joshua Bell
  • 7,727
  • 27
  • 30