Skip to content

Commit 7fab7b4

Browse files
author
Ronald Maravilla
committed
Fix incremental upgrade problem.
1 parent 1268a5d commit 7fab7b4

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,25 @@ angular.module('myModuleName', ['xc.indexedDB'])
4949
```
5050
The connection method takes the databasename as parameter,
5151
the upgradeCallback has 3 parameters:
52-
function callback(event, database, transaction). For upgrading your db structure, see
52+
function callback(event, database, transaction). AngularJS-indexedDB supports incremental
53+
upgrades. Simply define what to do for each version incrementally:
54+
```javascript
55+
angular.module('myModuleName', ['xc.indexedDB'])
56+
.config(function ($indexedDBProvider) {
57+
$indexedDBProvider
58+
.connection('myIndexedDB')
59+
.upgradeDatabase(1, function(event, db, tx){
60+
var objStore = db.createObjectStore('people', {keyPath: 'ssn'});
61+
objStore.createIndex('name_idx', 'name', {unique: false});
62+
objStore.createIndex('age_idx', 'age', {unique: false});
63+
});
64+
.upgradeDatabase(2, function(event, db, tx){
65+
db.createObjectStore('peoplePhones', {keyPath: 'person_ssn'});
66+
});
67+
});
68+
```
69+
When upgrade is required only the migrations which have not been run yet will be run.
70+
For upgrading your db structure, see
5371
https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB.
5472

5573
You can also define your own error handlers, overwriting the default ones, which log to console.

src/indexeddb.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() {
2727
module.db = null;
2828
module.dbPromise = null;
2929
module.outStandingTransactionCount = 0;
30+
module.upgradesByVersion = {}
3031

3132
/** predefined callback functions, can be customized in angular.config */
3233
module.onTransactionComplete = function(e) {
@@ -76,8 +77,8 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() {
7677
* @returns {object} this
7778
*/
7879
module.upgradeDatabase = function(newVersion, callback) {
79-
module.dbVersion = newVersion;
80-
module.upgradeCallback = callback;
80+
module.upgradesByVersion[newVersion] = callback;
81+
module.dbVersion = Math.max.apply(null, Object.keys(module.upgradesByVersion) )
8182
return this;
8283
};
8384

@@ -91,6 +92,16 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() {
9192
}
9293
};
9394
};
95+
96+
var applyNeededUpgrades = function(oldVersion, event, db, tx) {
97+
for(var version in module.upgradesByVersion) {
98+
if( !module.upgradesByVersion.hasOwnProperty(version) || version <= oldVersion ) {
99+
continue;
100+
}
101+
console.log("Running upgrade : " + version + " from " + oldVersion);
102+
module.upgradesByVersion[version](event , db, tx)
103+
}
104+
};
94105

95106
module.$get = ['$q', '$rootScope', function($q, $rootScope) {
96107
/**
@@ -138,7 +149,7 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() {
138149
var db = e.target.result, tx = e.target.transaction;
139150
console.log('upgrading database "' + db.name + '" from version ' + e.oldVersion+
140151
' to version ' + e.newVersion + '...');
141-
module.upgradeCallback && module.upgradeCallback(e, db, tx);
152+
applyNeededUpgrades(e.oldVersion, e, db, tx);
142153
};
143154
}
144155

0 commit comments

Comments
 (0)