Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive searching in indexedDB

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...

like image 409
BriscoCountyJr Avatar asked Dec 21 '22 00:12

BriscoCountyJr


1 Answers

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.

javascript indexeddb html5

like image 165
Amro Avatar answered Jan 30 '23 20:01

Amro