I was wondering if it is possible to perform a search on an indexeddb object store using wildcards. It would be handy to find all object with a key beginning with '555' for example
This is possible out of the box using either compound keys or key fragments. The way keys work in IndexedDB is that you generate a "keyRange" object and pass it to your cursor invocation. The keyrange passes info like "start at A and end at Z, inclusive."
By nature, there is partial matching built into this; the downside is that your cursor will return any keys that come between your keys and you might have to filter down those results further.
Say you have these words as keys in a object store:
The key range "A to Z, inclusive" would return all of these but "Ap to Z, inclusive" would return just the last three.
Another technique I've used to implement this is by passing a "filter" function to my methods that invoke IndexedDB. Inside the methods onsuccess callback, pass the result (event.target.result
) through your filter function and if it returns true then call your method invoker's onsuccess callback.
Yes it is feasible to use wildcards, sort of.
I can't yet vote or even comment on previous answers (hmmm...) so I'll just repeat user2025527's answer as it totally worked for my needs.
Use the bounds method and specify the base value for the 1st argument and the same value plus an extra character for the 2nd argument.
In most cases the extra character character should be the last one in your charset: \uffff
But you're free to decide what constitutes the limit, especially when dealing with localization.
Lest say you have the following values in your index:
To find everything stating with "BA" you should use
var range = IDBKeyRange.bound("BA", "BA" + '\uffff');
It isn't possible by default, but my library i wrote for the indexeddb supports it. Try linq2indexeddb.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With