Provide nice way to insert data during config
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.
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').
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.
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?
Sure. Write the PR and it'll happen.