angular-11-indexeddb
Version:
A powerful and flexible Angular 11 library for managing client-side storage using IndexedDB with support for cache expiration and CRUD operations.
2 lines • 7.85 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("angular-11-indexeddb",["exports","@angular/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["angular-11-indexeddb"]={},e.ng.core)}(this,function(e,t){"use strict";function n(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,Object.freeze(t)}var r=n(t);function i(e,t,n,r){return new(n||(n=Promise))(function(i,o){function a(e){try{s(r.next(e))}catch(e){o(e)}}function c(e){try{s(r.throw(e))}catch(e){o(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(a,c)}s((r=r.apply(e,t||[])).next())})}function o(e,t){var n,r,i,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},a=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return a.next=c(0),a.throw=c(1),a.return=c(2),"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(s){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(o=0)),o;)try{if(n=1,r&&(i=2&c[0]?r.return:c[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,c[1])).done)return i;switch(r=0,i&&(c=[2&c[0],i.value]),c[0]){case 0:case 1:i=c;break;case 4:return o.label++,{value:c[1],done:!1};case 5:o.label++,r=c[1],c=[0];continue;case 7:c=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==c[0]&&2!==c[0])){o=0;continue}if(3===c[0]&&(!i||c[1]>i[0]&&c[1]<i[3])){o.label=c[1];break}if(6===c[0]&&o.label<i[1]){o.label=i[1],i=c;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(c);break}i[2]&&o.ops.pop(),o.trys.pop();continue}c=t.call(e,o)}catch(e){c=[6,e],r=0}finally{n=i=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}}Object.create;Object.create;"function"==typeof SuppressedError&&SuppressedError;var a=new t.InjectionToken("DB_NAME"),c=new t.InjectionToken("STORE_NAME"),s=new t.InjectionToken("CACHED_TIME"),u=function(e,t){this.cachedTime=null,this.id=e,this.value=t,this.savedTime=(new Date).toISOString()},l=function(){function e(e,t,n){this.dbName=e,this.storeName=t,this.cachedTime=n,this.dbInitialized=this.initDB()}return e.prototype.whenInitialized=function(){return i(this,void 0,void 0,function(){return o(this,function(e){return[2,this.dbInitialized]})})},e.prototype.initDB=function(){var e=this;return new Promise(function(t,n){var r=indexedDB.open(e.dbName);r.onupgradeneeded=function(t){var n=t.target.result;n.objectStoreNames.contains(e.storeName)||(n.createObjectStore(e.storeName,{keyPath:"id"}),console.info("Object store '"+e.storeName+"' created."))},r.onsuccess=function(e){var n=e.target.result;t(n)},r.onerror=function(e){console.error("Error opening IndexedDB:",e),n(e.target.error)}})},e.prototype.saveData=function(e,t,n){return void 0===n&&(n=null),i(this,void 0,void 0,function(){var r,i,a,c;return o(this,function(o){switch(o.label){case 0:return[4,this.whenInitialized()];case 1:return o.sent(),[4,this.whenInitialized()];case 2:return r=o.sent(),i=r.transaction(this.storeName,"readwrite"),a=i.objectStore(this.storeName),(c=new u(e,t)).cachedTime=n,[2,new Promise(function(e,t){var n=a.put(c);n.onsuccess=function(){e()},n.onerror=function(e){t("Error saving data: "+e)}})]}})})},e.prototype.getData=function(e){return i(this,void 0,void 0,function(){var t,n,r,a=this;return o(this,function(c){switch(c.label){case 0:return[4,this.whenInitialized()];case 1:return c.sent(),[4,this.whenInitialized()];case 2:return t=c.sent(),n=t.transaction(this.storeName,"readonly"),r=n.objectStore(this.storeName),[2,new Promise(function(t,n){var c=r.get(e);c.onsuccess=function(){return i(a,void 0,void 0,function(){var e,n;return o(this,function(r){switch(r.label){case 0:return void 0!==c.result?[3,1]:(t(null),[3,6]);case 1:return e=c.result.cachedTime,n=!1,null!==e?[3,3]:[4,this.checkIfExpired(c.result.savedTime,this.cachedTime)];case 2:return n=r.sent(),[3,5];case 3:return[4,this.checkIfExpired(c.result.savedTime,e)];case 4:n=r.sent(),r.label=5;case 5:t(n?null:c.result.value),r.label=6;case 6:return[2]}})})},c.onerror=function(e){n("Error retrieving data: "+e)}})]}})})},e.prototype.removeData=function(e){return i(this,void 0,void 0,function(){var t,n,r;return o(this,function(i){switch(i.label){case 0:return[4,this.whenInitialized()];case 1:return i.sent(),[4,this.whenInitialized()];case 2:return t=i.sent(),n=t.transaction(this.storeName,"readwrite"),r=n.objectStore(this.storeName),[2,new Promise(function(t,n){var i=r.delete(e);i.onsuccess=function(){t()},i.onerror=function(e){n("Error removing data: "+e)}})]}})})},e.prototype.updateData=function(e,t){return i(this,void 0,void 0,function(){var n,r,i,a;return o(this,function(o){switch(o.label){case 0:return[4,this.whenInitialized()];case 1:return o.sent(),[4,this.whenInitialized()];case 2:return n=o.sent(),r=n.transaction(this.storeName,"readwrite"),i=r.objectStore(this.storeName),a=new u(e,t),[2,new Promise(function(e,t){var n=i.put(a);n.onsuccess=function(){e()},n.onerror=function(e){t("Error saving data: "+e)}})]}})})},e.prototype.getAll=function(){return i(this,void 0,void 0,function(){var e,t,n,r=this;return o(this,function(a){switch(a.label){case 0:return[4,this.whenInitialized()];case 1:return a.sent(),[4,this.whenInitialized()];case 2:return e=a.sent(),t=e.transaction(this.storeName,"readonly"),n=t.objectStore(this.storeName),[2,new Promise(function(e,t){var a=n.getAll();a.onsuccess=function(){return i(r,void 0,void 0,function(){var t,n,r=this;return o(this,function(c){switch(c.label){case 0:return void 0!==(t=a.result)&&0!==t.length?[3,1]:(e([]),[3,3]);case 1:return[4,Promise.all(t.map(function(e){return i(r,void 0,void 0,function(){var t,n;return o(this,function(r){switch(r.label){case 0:return t=!1,null!==(n=e.cachedTime)?[3,2]:[4,this.checkIfExpired(e.savedTime,this.cachedTime)];case 1:return t=r.sent(),[3,4];case 2:return[4,this.checkIfExpired(e.savedTime,n)];case 3:t=r.sent(),r.label=4;case 4:return[2,t?null:e.value]}})})}))];case 2:n=c.sent(),e(n.filter(function(e){return null!==e})),c.label=3;case 3:return[2]}})})},a.onerror=function(e){t("Error retrieving all data from IndexedDB: "+e)}})]}})})},e.prototype.clearStore=function(){return i(this,void 0,void 0,function(){var e,t,n;return o(this,function(r){switch(r.label){case 0:return[4,this.whenInitialized()];case 1:return r.sent(),[4,this.whenInitialized()];case 2:return e=r.sent(),t=e.transaction(this.storeName,"readwrite"),n=t.objectStore(this.storeName),[2,new Promise(function(e,t){var r=n.clear();r.onsuccess=function(){e()},r.onerror=function(e){t("Error clearing the store: "+e)}})]}})})},e.prototype.getCachedTime=function(){return this.cachedTime},e.prototype.setCachedTime=function(e){this.cachedTime=e},e.prototype.checkIfExpired=function(e,t){return i(this,void 0,void 0,function(){var n,r;return o(this,function(i){return 0===t?[2,!1]:(n=(new Date).getTime(),r=new Date(e).getTime(),[2,n-r>t])})})},e}();l.ɵprov=r.ɵɵdefineInjectable({factory:function(){return new l(r.ɵɵinject(a),r.ɵɵinject(c),r.ɵɵinject(s))},token:l,providedIn:"root"}),l.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}],l.ctorParameters=function(){return[{type:String,decorators:[{type:t.Inject,args:[a]}]},{type:String,decorators:[{type:t.Inject,args:[c]}]},{type:Number,decorators:[{type:t.Inject,args:[s]}]}]},e.CACHED_TIME=s,e.DB_NAME=a,e.DataToStore=u,e.IndexedDbHandler=l,e.STORE_NAME=c,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=angular-11-indexeddb.umd.min.js.map