javascript - Filter IndexedDB records with multiple values -
this question has answer here:
- searching compound indexes in indexeddb 2 answers
i need able filter data indexeddb in 3 different ways.
for example, if data stored following object:
var cars = [ {type: 'econmoy',colour: 'red',agerange:'1 - 10'}, {type: 'econmoy',colour: 'blue',agerange:'11 - 20'}, {type: 'luxury',colour: 'yellow',agerange:'1 - 10'} ]
i wanted allow users filter results type, colour , agerange separately and/or jointly. hoping idbkeyrange.bound method answer doesn't work:
var lowerarray = ['econmoy','blue','1 - 10']; var upperarray = ['luxury','yellow','11 - 20']; var rangetest = idbkeyrange.bound(lowerarray,upperarray); index.opencursor(rangetest).onsuccess = ...
can suggest way of going it?
here rough fiddle of initial thoughts: http://jsfiddle.net/as55s/
any feedback appreciated.
your example seems works pretty well, unfortunately afaik, cannot filter multiple properties separately on index array.
the whole indexeddb filter processing rely on order of index keys. , compound indexe keys must seen simple concatenation of multiple values, can search using them, need respect properties order...
in example compound index order type=>colour=>agerange...
objectstore.createindex("name", ['type','colour','agerange'], { unique: false });
so can search on:
- type
- type and colour
- type and colour and agerange
but can't filter separately colour , agerange in relational database. personnaly have no way here 3 possible ugly workaround:
execute cursor on whole object store, , filter each object in cursor loop. easiest code, , ll able more complexe filtering (startwith/endwith), ll slower, particulary huge amount of rows browse...
(tips: can make method faster if force user fill @ last 1 specific field bind index, reduce numbre of rows filtered cursor...)
create compound indexe every properties order , use right 1 depending on search fields values. ll result in large amount codes , indexes objectsore, according this comment, 20 indexes single object store reasonables, support @ least 4 search field...
[edit: according comment - may misunderstood]
create standard index each properties , use them separately index cursor primary keys, merge results in array keeping primary keys found in every indexes query. finally, load 1 one corresponding object primary keys merged in array.
this way pretty because don't have create , manage lot of indexes, , every fields ll support startwith filtering. loose straight cursor features, , wonder performance of loading 1 one each record js?
anyway, think indexeddb not designed such usecases. if you're planning complex search (multi-criteria queries, statistics...) relational databases (like websql) more suitable.
Comments
Post a Comment