Question

Each of my IndexedDB objects have the following format:

object:
{
     objectName: "someName"
     objectTest: "someTest"
     objectAge: "someAge"
}

Then I have the following indexes already set:

storeObject.createIndex( "by_objectName", "objectName", { unique: false } );
storeObject.createIndex( "by_objectTest", "objectTest", { unique: false } );
storeObject.createIndex( "by_objectAge", "objectAge", { unique: false } );

So first I wanted to loop through all my objects by objectName (this is working):

var openedIndex = storeObject.index("by_objectName");
var numItemsInIndex = openedIndex.count();
if (openedIndex) {
    var curCursor = openedIndex.openCursor();               
    curCursor.onsuccess = function(evt) {                   
        var cursor = evt.target.result; 
        if (cursor) {
                //do something
                cursor.continue();
        }
    }
}

So the above code is taking all the objects and they are sorted by objectName. How can I take all the objects that have objectTest: "certainValue" and the sorting by objectName to remain the same as in the above example. I need to filter the list of result before the line if (openedIndex) { because I need to use the numItemsInIndex later in the loop.

So in other words, if this was relational database, how to implement:

SELECT * FROM objects WHERE objectTest = "certainValue" SORT BY objectName
Was it helpful?

Solution

You can create an index on two properties at once by defining the keyPath parameter to createIndex as an array. Use the property you wish to sort by as the first item in the array. For example, see my other posts, like this one: In IndexedDB, is there a way to make a sorted compound query?

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