Query using multiple conditions

后端 未结 5 749
暗喜
暗喜 2020-12-24 11:37

I recently discovered (sadly) that WebSQL is no longer being supported for HTML5 and that IndexedDB will be replacing it instead.

I\'m wondering if there is any way

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 12:14

    I'm a couple of years late, but I'd just like to point out that Josh's answer works on the presumption that all of the "columns" in the condition are part of the index's keyPath.

    If any of said "columns" exist outside the the index's keyPath, you will have to test the conditions involving them on each entry which the cursor created in the example iterates over. So if you're dealing with such queries, or your index isn't unique, be prepared to write some iteration code!

    In any case, I suggest you check out BakedGoods if you can represent your query as a boolean expression.

    For these types of operations, it will always open a cursor on the focal objectStore unless you're performing a strict equality query (x ===? y, given x is an objectStore or index key), but it will save you the trouble writing your own cursor iteration code:

    bakedGoods.getAll({
        filter: "keyObj > 5 && valueObj.someProperty !== 'someValue'",
        storageTypes: ["indexedDB"],
        complete: function(byStorageTypeResultDataObj, byStorageTypeErrorObj){}
    });
    

    Just for the sake of complete transparency, BakedGoods is maintained by moi.

提交回复
热议问题