0

I am trying to move from websql to indexeddb but can't seem to replicate the SELECT query: "SELECT * FROM tableA WHERE cid ='"+cid+"' AND hid IN("+hid+",1) ORDER BY hid DESC LIMIT 1";

function getMyData(e) {
    var s ="";
    var hid = "value1";
    var cid = "value2";
    var transaction = db.transaction(["table"], "readonly");
    var store = transaction.objectStore("table");
    var index = store.index("myIndex"); //  myIndex was created onupgradeneeded from ['hid','cid']
    var request = index.openCursor(IDBKeyRange.only([hid, cid]));
    request.onsuccess = function(e){
        var cursor = e.target.result;
        if(cursor){
            s +="<h2>Key "+cursor.key+"</h2><p>";
                for (var field in cursor.value){
                    s += field+"="+cursor.value[field]+"<br>";
                }
            s +="</p>"
            cursor.continue();
        }
            document.querySelector("#mySpan").innerHTML = s;
}
}

I'd like to show the data if hid = value1 and cid = value2 but if the store index does not contain [value1,value2], I'd like to display data from store index [aDefaultValue,value2]. (the default value being 1 in the sql statement). I tried upperbound but this returns the index [value1(-1), value2] data rather than [1,value2] data.

Josh
  • 17,834
  • 7
  • 50
  • 68
Íomhar
  • 1
  • 3
  • @lomhar , check this out - https://stackoverflow.com/questions/22419703/or-or-in-operation-in-indexeddb/46051153#46051153 – Ujjwal Kumar Gupta Oct 05 '17 at 17:38
  • Yep - Indexed DB doesn't have a "key set", it can only query based on a single key or key range. So the best thing to do is issue two queries. – Joshua Bell Oct 06 '17 at 18:17

1 Answers1

0

The simplest is probably:

var r1 = index.get([aDefaultValue, cid]);
var r2 = index.get([hid, cid]);
// Since requests are processed in order, r1 guaranteed to succeed
// before r2 succeeds
r2.onsuccess = function() {
  var result = (r2.result !== undefined) ? r2.result : r1.result;
};
Joshua Bell
  • 7,727
  • 27
  • 30