Indexed DB cursor ranges on mulitiple properties

I have a composite index of two properties in the indexeddb object store and you want to get a cursor based on the range of both of these properties.

Here is an example of an object in the repository:

{species: 'Oak',xcoord: 123456, ycoord: 654321} 

and index:

 treeStore.createIndex("treelocation", ["xcoord","ycoord"], { unique: false }); 

Creating the index is successful, and I see it in the Chrome developer tools, but now I would like to open the cursor with the key at both x and y coordinates (which will be the size of the map).

Searching the Internet I donโ€™t see how to do this, and opening an index with an array of key ranges does not work.

thanks

Andrew

+6
source share
3 answers

I was told that the solution is really IDBKeyRange.bound([lowX,lowY],[highX,highY]) .

+6
source

The index you created is a composite index. This is a query like this:

 index = objectStore.index('treelocation'); index.get([123456, 654321]); 

Alternatively, you can use two indexes for each coorrd. In my opinion, this is better.

 x_index = objectStore.index('xcoord'); y_index = objectStore.index('ycoord'); x_species = x_index.get(IDBKeyRange.only(123456)) y_species = y_index.get(IDBKeyRange.only(654321)) species = x_species.intersect(y_species); // this is result 
+4
source

A range suggestion is part of the answer, but even with array keys, it really is just a one-dimensional range, not a 2- (or N-) dimensional data selection. With your current circuit, you need to do something like this:

 index.openCursor([lowX, lowY], [highX, highY]).onsuccess = function(e) { var cursor = e.target.result; if (!cursor) return; // done! var x = cursor.key[0], y = cursor.key[1]; // assert(lowX <= x && x <= highX); if (y < lowY) { cursor.continue([x, lowY]); } else if (y > highY) { cursor.continue([x + 1, lowY]); } else { processRecord(cursor.value); // we got one! cursor.continue(); } }; 

(if the coordinates are not integers, replace + 1 with the corresponding epsilon)

I posted an example of a generic solution:

https://gist.github.com/inexorabletash/704e9688f99ac12dd336

+1
source

Source: https://habr.com/ru/post/944875/


All Articles