bramski / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
165 stars 49 forks source link

upgradeDatabase for $indexDB #20

Closed Templarian closed 9 years ago

Templarian commented 9 years ago

So, for a project that dynamically adds object stores it could be useful to have a upgradeDatabase method on $indexDB. There may be a better way (docs seem rather light on examples).

/**
 @ngdoc method
 @name $indexedDB.upgradeDatabase
 @function

 @description Upgrade the database after config.
 */
upgradeDatabase: (function (this_) {
  return function (newVersion, callback) {
    return closeDatabase().then(function () {
      this_.upgradeDatabase(newVersion, callback);
      return openDatabase();
    });
  };
})(this),

A really basic idea of how this could be used is below:

$indexedDB.databaseInfo().then(function (info) {
    $indexedDB.upgradeDatabase(info.version + 1, function (event, db, tx) {
        var objStore = db.createObjectStore('newTable', { keyPath: 'id' });
        objStore.createIndex('new_idx', 'new', { unique: false });
    });
});

I've never used coffeescript, but I'm sure I could guess my way through it and do a pull request if you agree this would be useful.

bramski commented 9 years ago

Already exists. https://github.com/bramski/angular-indexedDB#usage. And there are lots of examples.

Templarian commented 9 years ago

@bramski Correct me if I'm wrong, but that's during the config or start of the project.

I for instance construct the IndexedDB as the user selects what data they want to import (so API responses determine the schema).

bramski commented 9 years ago

Yes I see. No angular-indexed-db does not support this. In general what you are suggesting is somewhat tricky and I would recommend that you just consider creating all the stores for all possible configs as they are quite cheap to create. This will save you nightmares of ensuring that the database is closed and all transactions are finished before try and run an upgrade. Good luck!

Templarian commented 9 years ago

No problem, yea, it seemed like a very specific use case. This solution works, but I'm going to assume that 99% of users will not be modifying the IndexedDB after run (this is the first time I've ever run into a case like this).

Thanks for taking over the project. It's awesome.