diff --git a/build/ngDexie.js b/build/ngDexie.js index 8311db4..e12d6f0 100644 --- a/build/ngDexie.js +++ b/build/ngDexie.js @@ -1,6 +1,6 @@ /** * Angularjs wrapper around Dexie.js an IndexedDB handler - * @version v0.0.11 - build 2015-02-02 + * @version v0.0.12 - build 2015-02-13 * @link https://github.com/FlussoBV/NgDexie * @license Apache License, http://www.apache.org/licenses/ */ @@ -76,9 +76,9 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex } configuration.call(this, db); - db.open().then(function(){ + db.open().then(function () { db.close(); - db.open().then(function(){ + db.open().then(function () { $log.debug("NgDexie :: database is open"); }); }); @@ -94,6 +94,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex get: get, getByIndex: getByIndex, getDb: getDb, + getTransaction: getTransaction, list: list, listByIndex: listByIndex, put: put, @@ -162,6 +163,24 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex return db; } + /** + * Get an dexie.transaction in RW mode + * @param {type} storeName + * @param {type} handle which receives the transaction + * @returns {$q@call;defer.promise} + */ + function getTransaction(storeName, handle) { + var deferred = $q.defer(); + db.transaction("rw", storeName, function () { + handle.call(self, db); + }).then(function () { + deferred.resolve(); + }).catch(function (err) { + deferred.reject(err); + }); + return deferred.promise; + } + /** * Get all entries from the storeName * @param {type} storeName @@ -290,8 +309,7 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex resync: resync, unsyncedChanges: unsyncedChanges }; - - + /** * Resync the database * @param {type} url @@ -307,26 +325,29 @@ if (typeof module !== "undefined" && typeof exports !== "undefined" && module.ex // Disconnect the synchronisation database db.syncable.disconnect(url).then(function () { - var dbTables = []; + var clearTables = 0; angular.forEach(storeNames, function (storeName) { - dbTables.push(db.table(storeName)); - }); - - db.transaction("rw", dbTables, function () { - // Clear storenames - angular.forEach(dbTables, function (dbTable) { + var dbTable = db.table(storeName); + // Use single table transactions for safari + db.transaction("rw", dbTable, function () { dbTable.clear(); - }); - }).then(function () { - return db.syncable.delete(url).then(function () { - setTimeout(function () { - db.syncable.connect("iSyncRestProtocol", url); - }, 1500); + }).then(function () { + clearTables++; + if (clearTables === storeNames.length) { + db.syncable.delete(url).then(function () { + setTimeout(function () { + db.syncable.connect("iSyncRestProtocol", url); + }, 1500); + }); + } }); }); + }); } + /** + * Resync the database /** * Check if there are synchronisation changes * @param {type} url diff --git a/build/ngDexie.min.js b/build/ngDexie.min.js index 8df19af..af71185 100644 --- a/build/ngDexie.min.js +++ b/build/ngDexie.min.js @@ -1,7 +1,7 @@ /** * Angularjs wrapper around Dexie.js an IndexedDB handler - * @version v0.0.11 - build 2015-02-02 + * @version v0.0.12 - build 2015-02-13 * @link https://github.com/FlussoBV/NgDexie * @license Apache License, http://www.apache.org/licenses/ */ -"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="ngDexie"),function(){"use strict";function a(){function a(){return c}var b=this,c={name:"",debug:!1},d=function(){console.error("NgDexie :: No configuration hook has been set!")};b.setOptions=function(a){c=angular.extend({},c,a)},b.setConfiguration=function(a){d=a},b.$get=["$rootScope","$q","$log","ngDexieUtils",function(c,e,f,g){function h(a,b){var c=e.defer();return p.table(a).get(b,function(a){c.resolve(a)}),c.promise}function i(a,b,c){var d=e.defer();return p.table(a).where(b).equals(c).toArray(function(a){angular.isArray(a)&&a.length>0?d.resolve(a[0]):d.reject()}),d.promise}function j(a,d){return angular.isUndefined(a)?p:(angular.isUndefined(d)&&(d=!1),d?c.$apply(function(){a.call(b,p)}):a.call(b,p),p)}function k(a){var b=e.defer();return p.table(a).toArray(function(a){b.resolve(a)}),b.promise}function l(a,b,c){var d=e.defer();return p.table(a).where(b).equals(c).toArray(function(a){d.resolve(a)}),d.promise}function m(a,b){var c=e.defer();return p.table(a).put(g.deepClone(b)).then(function(a){c.resolve(a)}),c.promise}function n(a){var b=e.defer();return p.close(),a&&a.call(this,p),p.open().then(function(){b.resolve()}),b.promise}f.debug("NgDexie :: init");var o=a(),p=new Dexie(o.name);return o.debug&&f.warn("NgDexie :: debug mode enabled"),o.debug&&p["delete"]().then(function(){f.warn("debug mode :: Database deleted")}),d.call(this,p),p.open().then(function(){p.close(),p.open().then(function(){f.debug("NgDexie :: database is open")})}),p.on("blocked",function(){f.warn("database seems to be blocked")}),{getOptions:a,get:h,getByIndex:i,getDb:j,list:k,listByIndex:l,put:m,reopen:n}}]}angular.module("ngdexie",["ngdexie.core"]),angular.module("ngdexie.core",["ngdexie.utils"]),angular.module("ngdexie.core").provider("ngDexie",a)}(),function(){"use strict";function a(){function a(a){var b=Dexie.deepClone(a);return angular.isDefined(b.$$hashKey)&&delete b.$$hashKey,a}return{deepClone:a}}angular.module("ngdexie.utils",["ngdexie.core"]),angular.module("ngdexie.utils").factory("ngDexieUtils",a)}(),function(){"use strict";function a(a,b,c,d){function e(a,b){var c=d.getDb();angular.isArray(b)||(b=[b]),c.syncable.disconnect(a).then(function(){var d=[];angular.forEach(b,function(a){d.push(c.table(a))}),c.transaction("rw",d,function(){angular.forEach(d,function(a){a.clear()})}).then(function(){return c.syncable["delete"](a).then(function(){setTimeout(function(){c.syncable.connect("iSyncRestProtocol",a)},1500)})})})}function f(a){var b=c.defer(),e=d.getDb();return angular.isDefined(e)&&e.isOpen()&&e.syncable.unsyncedChanges(a).then(function(a){b.resolve(a)}),b.promise}return d.getDb().syncable&&d.getDb().syncable.on("statusChanged",function(a,c){b.$apply(function(){b.$broadcast("ngDexieStatusChanged",{status:a,statusText:Dexie.Syncable.StatusTexts[a],url:c})})}),{resync:e,unsyncedChanges:f}}angular.module("ngdexie.sync",["ngdexie.core"]),angular.module("ngdexie.sync").factory("ngDexieSync",a),a.$inject=["$log","$rootScope","$q","ngDexie"]}(); \ No newline at end of file +"undefined"!=typeof module&&"undefined"!=typeof exports&&module.exports===exports&&(module.exports="ngDexie"),function(){"use strict";function a(){function a(){return c}var b=this,c={name:"",debug:!1},d=function(){console.error("NgDexie :: No configuration hook has been set!")};b.setOptions=function(a){c=angular.extend({},c,a)},b.setConfiguration=function(a){d=a},b.$get=["$rootScope","$q","$log","ngDexieUtils",function(c,e,f,g){function h(a,b){var c=e.defer();return q.table(a).get(b,function(a){c.resolve(a)}),c.promise}function i(a,b,c){var d=e.defer();return q.table(a).where(b).equals(c).toArray(function(a){angular.isArray(a)&&a.length>0?d.resolve(a[0]):d.reject()}),d.promise}function j(a,d){return angular.isUndefined(a)?q:(angular.isUndefined(d)&&(d=!1),d?c.$apply(function(){a.call(b,q)}):a.call(b,q),q)}function k(a,c){var d=e.defer();return q.transaction("rw",a,function(){c.call(b,q)}).then(function(){d.resolve()})["catch"](function(a){d.reject(a)}),d.promise}function l(a){var b=e.defer();return q.table(a).toArray(function(a){b.resolve(a)}),b.promise}function m(a,b,c){var d=e.defer();return q.table(a).where(b).equals(c).toArray(function(a){d.resolve(a)}),d.promise}function n(a,b){var c=e.defer();return q.table(a).put(g.deepClone(b)).then(function(a){c.resolve(a)}),c.promise}function o(a){var b=e.defer();return q.close(),a&&a.call(this,q),q.open().then(function(){b.resolve()}),b.promise}f.debug("NgDexie :: init");var p=a(),q=new Dexie(p.name);return p.debug&&f.warn("NgDexie :: debug mode enabled"),p.debug&&q["delete"]().then(function(){f.warn("debug mode :: Database deleted")}),d.call(this,q),q.open().then(function(){q.close(),q.open().then(function(){f.debug("NgDexie :: database is open")})}),q.on("blocked",function(){f.warn("database seems to be blocked")}),{getOptions:a,get:h,getByIndex:i,getDb:j,getTransaction:k,list:l,listByIndex:m,put:n,reopen:o}}]}angular.module("ngdexie",["ngdexie.core"]),angular.module("ngdexie.core",["ngdexie.utils"]),angular.module("ngdexie.core").provider("ngDexie",a)}(),function(){"use strict";function a(){function a(a){var b=Dexie.deepClone(a);return angular.isDefined(b.$$hashKey)&&delete b.$$hashKey,a}return{deepClone:a}}angular.module("ngdexie.utils",["ngdexie.core"]),angular.module("ngdexie.utils").factory("ngDexieUtils",a)}(),function(){"use strict";function a(a,b,c,d){function e(a,b){var c=d.getDb();angular.isArray(b)||(b=[b]),c.syncable.disconnect(a).then(function(){var d=0;angular.forEach(b,function(e){var f=c.table(e);c.transaction("rw",f,function(){f.clear()}).then(function(){d++,d===b.length&&c.syncable["delete"](a).then(function(){setTimeout(function(){c.syncable.connect("iSyncRestProtocol",a)},1500)})})})})}function f(a){var b=c.defer(),e=d.getDb();return angular.isDefined(e)&&e.isOpen()&&e.syncable.unsyncedChanges(a).then(function(a){b.resolve(a)}),b.promise}return d.getDb().syncable&&d.getDb().syncable.on("statusChanged",function(a,c){b.$apply(function(){b.$broadcast("ngDexieStatusChanged",{status:a,statusText:Dexie.Syncable.StatusTexts[a],url:c})})}),{resync:e,unsyncedChanges:f}}angular.module("ngdexie.sync",["ngdexie.core"]),angular.module("ngdexie.sync").factory("ngDexieSync",a),a.$inject=["$log","$rootScope","$q","ngDexie"]}(); \ No newline at end of file