1

This topic explains how to use wildcards ad the end of the searchterm using IndexedDB.

I am looking for a way to add a wildcard at the end AND at the start of the searchterm. In SQL it would be: LIKE '%SearchTerm%'.

How can I achieve this with IndexedDB? Here is my code:

function getMaterials() {
    var materialNumber = $("#input").val();
    var transaction = db.transaction(["materials"]);
    var objectStore = transaction.objectStore("materials");

    var request = objectStore.openCursor(IDBKeyRange.bound(materialNumber, materialNumber + '\uffff'), 'prev');
    $("#output").find("tr:gt(0)").remove();

    request.onsuccess = function (event) {

        var cursor = event.target.result;
        if (cursor) {
            var newRow = '<tr><td>'+ cursor.value.materialNumber +'</td>'+
                 '<td>'+ cursor.value.description +'</td>'+
                 '<td>'+ cursor.value.pieces +'</td>'+
                 '<td>'+ cursor.value.price +'</td></tr>';

            $('#output').append(newRow);

            cursor.continue();
        }
    };
};

EDIT:

I could achieve this by letting indexDB return all rows and then narrow down in JavaScript. But there must be a better approach in terms of performance.

if (cursor.value.materialNumber.indexOf(materialNumber) != -1){
     //add result...
}
Community
  • 1
  • 1
Anonymoose
  • 2,389
  • 6
  • 36
  • 69

1 Answers1

1

This was not how idb was intended to be used. If you want text searching, parse text into tokens, store the tokens, use an index on the tokens, and do lookups on the index to get a pointer to the full text.

Josh
  • 17,834
  • 7
  • 50
  • 68