Question

Can someone please tell me if it is possible to execute case-insensitive search on indexeddb object store using index, openCursor and keyRange....
I can do case sensitive and managed to implement something using toUpperCase function but there are so many different varaitions that it is not feasible relly...
Any help welcomed... Thanx... Example of an attempt:

var transaction = db.transaction("products", IDBTransaction.READ_ONLY);
var store = transaction.objectStore("products");
var descIndex = store.index('Index1');
var boundKeyRangeUpper = IDBKeyRange.bound(request.term.toUpperCase(), (request.term.toUpperCase()+'Z'));
descIndex.openCursor(boundKeyRangeUpper).onsuccess = function (e) 

UPDATE
This is what I ended up implementing: Four ranges that cover and possibility i.e. FA,fA,Fa,fa... This is done on the first letter only i.e. 'F'

var boundKeyRangeUpper = IDBKeyRange.bound(term.toUpperCase(), term.toUpperCase()+'Z'); var boundKeyRangeUpper2 = IDBKeyRange.bound(term.toUpperCase(), term.toUpperCase()+'z'); var boundKeyRangeLower = IDBKeyRange.bound(term.toLowerCase(), term.toLowerCase()+'z'); var boundKeyRangeLower2 = IDBKeyRange.bound(term.toLowerCase(), term.toLowerCase()+'Z');

The results of the searches above is stored in another objectStore but with their description as uppercase hence FISH, FRUIT, FRESH SALAD... Now if a letter is pressed after F e.g. 'r' the search will look into this new object store where everything is uppercase... This is not an ideal solution by far but it works and it is quick for my needs... Perhaps we will get case insensative search eventually, perhaps not...

Was it helpful?

Solution

I recommend modifying the structure of the object store, this is by adding a column (i.e. a property to each object in the store) with the same value of the key BUT to upper case, then make this new property the key instead.

//object
var lang = {
    name: 'Javascript',
    keyName: '', //THE NEW PROPERTY - KEY OF THE OBJECTSTORE
    interest: 100
};

when you add an object to the store modify the new property with the uppercase of the name (or term or whatever) so the object will be:

//object
var lang = {
    name: 'Javascript',
    keyName: 'JAVASCRIPT', // NEW PROPERTY MODIFIED TO UPPERCASE OF OLD KEY
    interest: 100
};

then proceed with your code as it.

OTHER TIPS

Dexie.js does great job at case insensitive search without additional index.

The reason you see what you see is that IndexedDB cursors are lexicographically sorted.

Under this paradigm, a > A, unlike alphabetical sorting which sounds more like what you're looking for.

To achieve alphabetical sorting you'd have to cursor through all records in your lexicographical sort (until you get null as event.target.result in your onsuccess), adding the results to a shared global and then, on completion of the lexicographical sort, return an alphabetical sort of your shared global.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top