14

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" ?

Community
  • 1
  • 1
Chito Adinugraha
  • 1,220
  • 2
  • 15
  • 21

1 Answers1

4

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();          
    }
};
x3m
  • 533
  • 5
  • 16