IN Operator equivalence in indexed DB

I want to execute this query select * from properties where propertyCode IN ("field1", "field2", "field3")

How can I achieve this in IndexedDB I tried this thing

getData : function (indexName, params, objectStoreName) {
            var defer = $q.defer(),
                db, transaction, index, cursorRequest, request, objectStore, resultSet, dataList = [];
            request = indexedDB.open('test');
            request.onsuccess = function (event) {
                db = request.result;
                transaction = db.transaction(objectStoreName);
                objectStore = transaction.objectStore(objectStoreName);
                index = objectStore.index(indexName);
                cursorRequest = index.openCursor(IDBKeyRange.only(params));
                cursorRequest.onsuccess = function () {

                    resultSet = cursorRequest.result;
                    if(resultSet){
                        dataList.push(resultSet.value);
                        resultSet.continue();
                    }
                    else{
                        console.log(dataList);
                        defer.resolve(dataList);
                    }
                };

                cursorRequest.onerror = function (event) {
                    console.log('Error while opening cursor');
                }
            }
            request.onerror = function (event) {
                console.log('Not able to get access to DB in executeQuery');
            }
            return defer.promise;
Run codeHide result

But it didn’t work. I tried Google, but could not find the exact answer.

+4
source share
1 answer

If you think that IN is essentially equivalent field1 == propertyCode OR field2 == propertyCode, you can say that IN is another way to use OR.

IndexedDB cannot execute ORs (unions) from a single query.

, - , . , . , , .

- , , . , .

-, , :

  • . , hasPropertyCodeX.
  • , ( ), ( , , ).
  • , .
  • , , ( ).
  • indexedDB.
  • . .

var request = indexedDB.open(...);
request.onupgradeneeded = upgrade;

function upgrade(event) {
  var db = event.target.result;
  var store = db.createObjectStore('store', ...);

  // Create another index for the special property
  var index = store.createIndex('hasPropCodeX', 'hasPropCodeX');
}

function putThing(db, thing) {

  // Before storing the thing, secretly update the hasPropCodeX value
  // which is derived from the thing other properties
  if(thing.field1 === 'propCode' || thing.field2 === 'propCode' || 
    thing.field3 === 'propCode') {
    thing.hasPropCodeX = 1;
  } else {
    delete thing.hasPropCodeX;
  }

  var tx = db.transaction('store', 'readwrite');
  var store = tx.objectStore('store');
  store.put(thing);
}

function getThingsWherePropCodeXInAnyof3Fields(db, callback) {
  var things = [];
  var tx = db.transaction('store');
  var store = tx.objectStore('store');
  var index = store.index('hasPropCodeX');
  var request = index.openCursor();
  request.onsuccess = function(event) {
    var cursor = event.target.result;
    if(cursor) {
      var thing = cursor.value;
      things.push(thing);
      cursor.continue();
    } else {
      callback(things);
    }
  };
  request.onerror = function(event) {
    console.error(event.target.error);
    callback(things);
  };
}

// Now that you have an api, here is some example calling code
// Not bothering to promisify it
function getData() {
  var request = indexedDB.open(...);
  request.onsuccess = function(event) {
    var db = event.target.result;
    getThingsWherePropCodeXInAnyof3Fields(db, function(things) {
      console.log('Got %s things', things.length);
      for(let thing of things) {
        console.log('Thing', thing);
      }
    });
  };
}
+1

Source: https://habr.com/ru/post/1653148/


All Articles