IndexedDB - During upgrade force abort, but with Promises

102 Views Asked by At

I used this post: IndexedDB: upgrade with promises?

And implemented the part here: https://stackoverflow.com/a/25565755/15778635

This works for what I need. the part I am having trouble with is this:

var newMigrationPromise = function (dbName, version, migration) {
    return newPromise(function (deferred) {
        var request = indexedDB.open(dbName, version);

        // NB: caller must ensure upgrade callback always called
        request.onupgradeneeded = function (event) {
            var db = request.result;
            newTransactionPromise(
                function () {
                    var syncUPStore = transaction.objectStore("syncUP");
                    var syncCountRequest = syncUPStore.count();

                    syncCountRequest.oncomplete = function (event) {
                        if (syncCountRequest.result > 0)
                            deferred.reject(syncCountRequest.result + " SyncUp Records exist, database upgrade aborted, keeping at current version.");
                        else {
                            //Good, continue with update
                            migration(db, request.transaction);
                            return request.transaction;
                        }
                    }
                })
                .then(function () { db.close(); })
                .then(deferred.resolve, deferred.reject);
        };

        request.onerror = function (ev) { deferred.reject(request.error); };
    });
};

I have a syncUP object store that has data that needs to be sent to the server when the user goes online. In this particular case the service worker is installing (because they came online and a change was put on the server) and needs to know if syncUP records exist prior to allowing the service worker to update. If they do exist then it needs to abort the install until it is empty.

The service worker abort works fine, and the database aborting upgrade works fine if I were to throw an error where var syncCountRequest = syncUPStore.count(); is.

My question: How can I check if there are records in the "syncUP" object store and still use the implementation I mentioned above? I had considered moving the logic to another method, but I found I was having the same issue of not knowing how to handle the reject/resolve. My Promises knowledge is ok, but not good enough yet to figure it out on my own.

1

There are 1 best solutions below

0
Josh On

a rushed example:

var request = indexedDb.open(...);
request.onupgradeneeded = function(event) {
  if(conditionShouldDoMigrationFromVersionXToNowIsTrue) {
    migrate(event.transaction);
  }
};

function migrate(versionChangeTransaction) {
  var store = versionChangeTransaction.objectStore('x');
  var request = store.getAll();
  request.onsuccess = function(event) {
     var objects = event.target.result;
     for (var object of objects) {
       // do some mutation to the object
       object.x++;
       // write it back
       store.put(object);
     }
  }; 
}