angular-indexedDB icon indicating copy to clipboard operation
angular-indexedDB copied to clipboard

Provide nice way to insert data during config

Open davetclark opened this issue 9 years ago • 4 comments

Unless I'm reading this wrong, I think to insert data during config/upgrade time you need to go through the native objects, transactions, etc. It would be nice to get the same interface that $indexedDB.openStore gives you.

davetclark avatar Feb 23 '16 20:02 davetclark

This actually represents a chicken & egg problem for the service. The upgrade callback gets called when you try and use the database for the first time. So it's a dependency of the $indexedDB service to do the upgrade, e.g. the promise won't give you the DB handle until this upgrade is finished. Which is nice, as it guarantees your schema will be in place.

Probably you would need a broadcast even to make this possible. $scope.$on('indexedDB:upgradeComplete').

bramski avatar Feb 23 '16 22:02 bramski

I suspect you would have to put the actual firing of the event itself in some kind of $timeout to actually allow the callstack to return.

Seems simple enough but the devil is in the details. Have fun.

bramski avatar Feb 23 '16 22:02 bramski

That makes sense. Probably not worth the change. I think I could accomplish the what I want client side though if upgradeDatabase supported promises.

    $indexedDBProvider.connection('gfeclient')
        .upgradeDatabase(1, function (event, db, tx) {
            var objectStore = db.createObjectStore('store', {
                keyPath: 'key'
            });

            var deferred = $q.defer();
            objectStore.transaction.oncomplete = function (event) {
                var store = db.transaction(['store'], 'readwrite').objectStore('store');
                store.add({key: 'key', value: 'value'});
                store.transaction.oncomplete = function (e) {
                    deferred.resolve();
                }
            }
            return deferred.promise;
        });

The contract is that if .upgradeDatabase returns a promise, the next upgrade won't happen until the promise is resolved. Would you accept something like that?

davetclark avatar Feb 23 '16 23:02 davetclark

Sure. Write the PR and it'll happen.

bramski avatar Oct 26 '16 04:10 bramski