IndexedDB IDBKeyRange Compound/Multiple Index not working

我们两清 提交于 2019-12-24 04:22:18

问题


Can anyone explain why my IDBKeyRange only seems to be filtering on the first column?

I have an index defined as follows:

osDrugs.createIndex("combined", ["name", "strength", "form", "packsize"], {unique: false});

My query is roughly as follows:

var transaction = pimsDB.transaction("drugs");
var objectStore = transaction.objectStore("drugs");
var range = IDBKeyRange.bound([tmpName, tmpStrength, tmpForm, tmpPack],[tmpName+"z", tmpStrength + "z", tmpForm+"z", tmpPack+"z"]);
var index = objectStore.index("combined");
var request = index.openCursor(range);

My results appear to be filtering from tmpName up to tmpName + "z" but completely ignoring the strength, form, and packsize.

Can anyone see what I'm doing wrong?

Oh, all fields are stored as strings....

Cheers,

Dan

ADDITIONAL INFORMATION:

Another table that I created with a composite index appears to be working. The only two difference between these tables are as follows:

1) The table that works only has 2 properties in the index. 2) In the table that works both properties are part of a 3 property primary key.

I've tested that it works on this table by using the "Resources" tab in Chrome which allows me to filter an index by the lower bound key and return data.

MORE ADDITIONAL INFORMATION:

After some more testing, and switching over to FF to make sure that it wasn't a chrome but I was experiencing I think I have found the problem. I don't believe searching that composite index works the way I was expecting...

The IDBKeyRange is actually a range... It appears that when I enter, for example,

["PARA","500","TAB","32"] ["PARAz","500z","TABz","32z"]

IndexedDB searches for the first drug that starts at the LB, and selects everything up to the last drug that ends at the upper bound.

This, I believe, is why it's managing to select strengths that start with any value. It's not actually filtering for strengths that start "500",it's selecting everything between the first instance of "500" on a drug up to the last.

If my assumption is correct, the following article is probably a bit wrong/misleading (as I believe the user wanted to search a rectangle and not a range:

Indexed DB cursor ranges on mulitiple properties

Can anyone confirm what I'm finding and tell me if there's a way I can actually search IndexedDB on multiple values? At the bottom of the article above, someone mentions an intersect function which I think would work but when I search online, it doesn't seem to exist...

Cheers,

Dan


回答1:


I've posted a generalized solution over at:

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

The short summary is that you need to inspect each dimension of the result, and if any value is beyond the bounds for that dimension, skip ahead to the next plausible key.



来源:https://stackoverflow.com/questions/21731347/indexeddb-idbkeyrange-compound-multiple-index-not-working

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