Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search indexeddb for a string

I am trying to set up a search feature on my site, i'm trying to find IndexedDB code for

SELECT "column" FROM "table" WHERE "column" LIKE "%keyword%"

i found a solution in IndexedDB Fuzzy Search

db.transaction(['table'], 'readonly')
    .objectStore('table')
    .openCursor(IDBKeyRange.bound(keyword, keyword + '\uffff'), 'prev')
    .onsuccess = function (e) {
        e || (e = event);
        var cursor = e.target.result;
        if (cursor) {
            console.log(cursor.value.column);
            cursor.continue();
        }
    };

but how can i find "%keyword%" instead of "%keyword" ?

like image 502
Chito Adinugraha Avatar asked Apr 14 '13 03:04

Chito Adinugraha


1 Answers

There is nothing similar to SQL WHERE in IndexedDB.

I would loop through the table/object store values and see if the current cursor column contains the keyword:

var keyword = "foo";
var transaction = db.transaction("table", "readwrite");
var objectStore = transaction.objectStore("table");
var request = objectStore.openCursor();
request.onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
        if (cursor.value.column.indexOf(keyword) !== -1) {                
            console.log("We found a row with value: " + JSON.stringify(cursor.value));
        }  

        cursor.continue();          
    }
};
like image 82
x3m Avatar answered Oct 21 '22 06:10

x3m