Getting specific ids from Indexeddb

In my project I’m using browser’s indexed-DB and I would like to retrieve some objects from the db with specific ids. According to MDN you could use ranges to get the results you want:

According to MDN:

  • “Cannot call method 'open' of undefined” error when using indexedDB.open
  • indexedDb backup in the cloud
  • IndexedDB - ObjectStores vs multiple databases vs indices?
  • Optimized Bulk (Chunk) Upload Of Objects Into IndexedDB
  • Can I use indexeddb across subdomains?
  • IndexedDB: Retrieve item with max value
  • // Only match "Donna"
    var singleKeyRange = IDBKeyRange.only("Donna");
    
    // Match anything past "Bill", including "Bill"
    var lowerBoundKeyRange = IDBKeyRange.lowerBound("Bill");
    
    // Match anything past "Bill", but don't include "Bill"
    var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("Bill", true);
    
    // Match anything up to, but not including, "Donna"
    var upperBoundOpenKeyRange = IDBKeyRange.upperBound("Donna", true);
    
    // Match anything between "Bill" and "Donna", but not including "Donna"
    var boundKeyRange = IDBKeyRange.bound("Bill", "Donna", false, true);
    
    // To use one of the key ranges, pass it in as the first argument of openCursor()/openKeyCursor()
    index.openCursor(boundKeyRange).onsuccess = function(event) {
      var cursor = event.target.result;
      if (cursor) {
        // Do something with the matches.
        cursor.continue();
      }
    };
    

    However what do you do if you wish to get an array of specific ids that are not in order and are not sequential (ex:[91,819,34,24,501]) with a single request?

  • indexedDb backup in the cloud
  • javascript addEventlistener “click” not working
  • Preventing UI flicker when loading async data in React.js
  • Searching for compound indexes in IndexedDB
  • How can I safety modify a global variable from within indexedDB callbacks?
  • Performance of reading big binary arrays from IndexedDB is low in Javascript
  • 2 Solutions collect form web for “Getting specific ids from Indexeddb”

    Based on your comment, it sounds like you want to do something like SELECT objects FROM objects WHERE object.id = 1 OR object.id = 2 OR object.id = 5, as a single query (request). Unfortunately, indexedDB cannot do OR style queries (unions). It can only do AND style queries (intersections).

    There is a possible solution if the various ids you want to retrieve are known a-priori. You can simply store an additional group-id property, put the related ids into distinct groups, and then query by group-id. This obviously does not work if the ids you want to retrieve overlap (appear in more than one group), or are variable (object to group membership changes from query to query).

    You can’t do this with a single request, but you can fire off multiple requests in parallel:

    var keys = [91,819,34,24,501];
    var results = [];
    keys.forEach(function(key) {
      store.get(key).onsuccess = function(e) {
        results.push(e.target.result);
        if (results.length === keys.length) {
           // you're done!
        }
      };
    });
    

    Note that requests are required to execute in order, so the order of the results array will match the order of the keys array. If a key is not found, the corresponding result index will contain undefined.

    (There’s a feature request for Indexed DB to support this directly: https://github.com/w3c/IndexedDB/issues/19 but no final API design yet.)