-
Notifications
You must be signed in to change notification settings - Fork 44
/
angular2-indexeddb.min.js
2 lines (2 loc) · 3.96 KB
/
angular2-indexeddb.min.js
1
2
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var AngularIndexedDB=function(){function a(a,b){this.utils=new Utils,this.dbWrapper=new DbWrapper(a,b)}return a.prototype.openDatabase=function(a,b){var c=this,d=this;return new Promise(function(e,f){c.dbWrapper.dbVersion=a;var g=c.utils.indexedDB.open(c.dbWrapper.dbName,a);g.onsuccess=function(a){d.dbWrapper.db=g.result,e()},g.onerror=function(a){f((a.target.errorCode,a.target.errorCode+" ("+a.target.error+")"))},"function"==typeof b&&(g.onupgradeneeded=function(a){b(a,d.dbWrapper.db)})})},a.prototype.getByKey=function(a,b){var c=this;return new Promise(function(d,e){c.dbWrapper.validateBeforeTransaction(a,e);var f,g=c.dbWrapper.createTransaction({storeName:a,dbMode:"readonly",error:function(a){e(a)},complete:function(a){}}),h=g.objectStore(a);f=h.get(b),f.onsuccess=function(a){d(a.target.result)}})},a.prototype.getAll=function(a,b,c){var d=this;return new Promise(function(e,f){d.dbWrapper.validateBeforeTransaction(a,f);var g,h=d.dbWrapper.createTransaction({storeName:a,dbMode:"readonly",error:function(a){f(a)},complete:function(a){}}),i=h.objectStore(a),j=[];if(c){var k=i.index(c.indexName),l="desc"===c.order?"prev":"next";g=k.openCursor(b,l)}else g=i.openCursor(b);g.onerror=function(a){f(a)},g.onsuccess=function(a){var b=a.target.result;b?(j.push(b.value),b.continue()):e(j)}})},a.prototype.add=function(a,b,c){var d=this;return new Promise(function(e,f){d.dbWrapper.validateBeforeTransaction(a,f),d.dbWrapper.createTransaction({storeName:a,dbMode:"readwrite",error:function(a){f(a)},complete:function(a){e({key:c,value:b})}}).objectStore(a).add(b,c).onsuccess=function(a){c=a.target.result}})},a.prototype.update=function(a,b,c){var d=this;return new Promise(function(e,f){d.dbWrapper.validateBeforeTransaction(a,f),d.dbWrapper.createTransaction({storeName:a,dbMode:"readwrite",error:function(a){f(a)},complete:function(a){e(b)},abort:function(a){f(a)}}).objectStore(a).put(b,c)})},a.prototype.delete=function(a,b){var c=this;return new Promise(function(d,e){c.dbWrapper.validateBeforeTransaction(a,e),c.dbWrapper.createTransaction({storeName:a,dbMode:"readwrite",error:function(a){e(a)},complete:function(a){d()},abort:function(a){e(a)}}).objectStore(a).delete(b)})},a.prototype.openCursor=function(a,b,c){var d=this;return new Promise(function(e,f){d.dbWrapper.validateBeforeTransaction(a,f),d.dbWrapper.createTransaction({storeName:a,dbMode:"readonly",error:function(a){f(a)},complete:function(a){e()},abort:function(a){f(a)}}).objectStore(a).openCursor(c).onsuccess=function(a){b(a),e()}})},a.prototype.clear=function(a){var b=this;return new Promise(function(c,d){b.dbWrapper.validateBeforeTransaction(a,d),b.dbWrapper.createTransaction({storeName:a,dbMode:"readwrite",error:function(a){d(a)},complete:function(a){c()},abort:function(a){d(a)}}).objectStore(a).clear(),c()})},a.prototype.getByIndex=function(a,b,c){var d=this;return new Promise(function(e,f){d.dbWrapper.validateBeforeTransaction(a,f),d.dbWrapper.createTransaction({storeName:a,dbMode:"readonly",error:function(a){f(a)},abort:function(a){f(a)},complete:function(a){}}).objectStore(a).index(b).get(c).onsuccess=function(a){e(a.target.result)}})},a}();exports.AngularIndexedDB=AngularIndexedDB;var Utils=function(){function a(){this.indexedDB=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB}return a}();exports.Utils=Utils;var DbWrapper=function(){function a(a,b){this.dbName=a,this.dbVersion=b||1,this.db=null}return a.prototype.validateStoreName=function(a){return this.db.objectStoreNames.contains(a)},a.prototype.validateBeforeTransaction=function(a,b){this.db||b("You need to use the openDatabase function to create a database before you query it!"),this.validateStoreName(a)||b("objectStore does not exists: "+a)},a.prototype.createTransaction=function(a){var b=this.db.transaction(a.storeName,a.dbMode);return b.onerror=a.error,b.oncomplete=a.complete,b.onabort=a.abort,b},a}();exports.DbWrapper=DbWrapper;
//# sourceMappingURL=angular2-indexeddb.min.js.map