Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide nice way to insert data during config #57

Open
davetclark opened this issue Feb 23, 2016 · 4 comments
Open

Provide nice way to insert data during config #57

davetclark opened this issue Feb 23, 2016 · 4 comments

Comments

@davetclark
Copy link

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.

@bramski
Copy link
Owner

bramski commented Feb 23, 2016

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
Copy link
Owner

bramski commented Feb 23, 2016

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.

@davetclark
Copy link
Author

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?

@bramski
Copy link
Owner

bramski commented Oct 26, 2016

Sure. Write the PR and it'll happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants