123456789101112131415 |
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).GoDB=t()}(this,(function(){"use strict";
- /*! *****************************************************************************
- Copyright (c) Microsoft Corporation.
- Permission to use, copy, modify, and/or distribute this software for any
- purpose with or without fee is hereby granted.
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- PERFORMANCE OF THIS SOFTWARE.
- ***************************************************************************** */var e=function(){return(e=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};function t(e,t,n,o){return new(n||(n=Promise))((function(r,a){function i(e){try{c(o.next(e))}catch(e){a(e)}}function s(e){try{c(o.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(i,s)}c((o=o.apply(e,t||[])).next())}))}function n(e,t){var n,o,r,a,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return a={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(a){return function(s){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,o&&(r=2&a[0]?o.return:a[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,a[1])).done)return r;switch(o=0,r&&(a=[2&a[0],r.value]),a[0]){case 0:case 1:r=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,o=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(r=i.trys,(r=r.length>0&&r[r.length-1])||6!==a[0]&&2!==a[0])){i=0;continue}if(3===a[0]&&(!r||a[1]>r[0]&&a[1]<r[3])){i.label=a[1];break}if(6===a[0]&&i.label<r[1]){i.label=r[1],r=a;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(a);break}r[2]&&i.ops.pop(),i.trys.pop();continue}a=t.call(e,i)}catch(e){a=[6,e],o=0}finally{n=r=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}var o=function(){function o(e,t,n){this.godb=e,this.name=t,this.schema=n||null}return o.prototype.get=function(e){var t=this;return new Promise((function(n,o){t.godb.getDB((function(r){try{var a=r.transaction(t.name,"readonly").objectStore(t.name),i=function(e){var t=e.target.result;n(t)},s=function(e){var t=e.target.error;o(t)};if("object"==typeof e){var c=Object.keys(e)[0],u=e[c];(l="id"===c?a.get(u):a.index(c).get(u)).onsuccess=i,l.onerror=s}else if("number"==typeof e){var l;(l=a.get(e)).onsuccess=i,l.onerror=s}else o(new Error("Table.get() failed: invalid criteria"))}catch(e){o(e)}}))}))},o.prototype.getAll=function(e){var t=this;return new Promise((function(n,o){t.godb.getDB((function(r){try{var a=r.transaction(t.name,"readonly").objectStore(t.name),i=e?a.getAll(null,e):a.getAll();i.onsuccess=function(e){var t=e.target.result;n(t)},i.onerror=function(e){var t=e.target.error;o(t)}}catch(e){o(e)}}))}))},o.prototype.add=function(t){var n=this;return new Promise((function(o,r){n.godb.getDB((function(a){try{var i=a.transaction(n.name,"readwrite").objectStore(n.name).add(t);i.onsuccess=function(n){var r=n.target.result;o(e(e({},t),{id:r}))},i.onerror=function(e){var t=e.target.error;r(t)}}catch(e){r(e)}}))}))},o.prototype.addMany=function(e){var o=this;return new Promise((function(r,a){return t(o,void 0,void 0,(function(){var t,o,i,s,c,u;return n(this,(function(n){switch(n.label){case 0:if(!Array.isArray(e))return[3,5];t=[],o=0,i=e,n.label=1;case 1:return o<i.length?(s=i[o],u=(c=t).push,[4,this.add(s)]):[3,4];case 2:u.apply(c,[n.sent()]),n.label=3;case 3:return o++,[3,1];case 4:return r(t),[3,6];case 5:a(new Error("Table.addMany() failed: input data should be an array")),n.label=6;case 6:return[2]}}))}))}))},o.prototype.put=function(t){var n=this;return new Promise((function(o,r){n.godb.getDB((function(a){if(!t||"object"!=typeof t)return r(new Error("Table.put() failed: data should be an object"));if(!t.id)return r(new Error("Table.put() failed: id is required in data"));try{var i=a.transaction(n.name,"readwrite").objectStore(n.name).put(t);i.onsuccess=function(n){var r=n.target.result;o(e(e({},t),{id:r}))},i.onerror=function(e){var t=e.target.error;r(t)}}catch(e){r(e)}}))}))},o.prototype.update=function(){},o.prototype.delete=function(e){var t=this;return new Promise((function(n,o){t.godb.getDB((function(r){try{t.get(e).then((function(e){var a=r.transaction(t.name,"readwrite").objectStore(t.name).delete(e.id);a.onsuccess=function(){n()},a.onerror=function(e){var t=e.target.error;o(t)}}))}catch(e){o(e)}}))}))},o.prototype.find=function(e){var t=this;return new Promise((function(n,o){t.godb.getDB((function(r){try{r.transaction(t.name,"readonly").objectStore(t.name).openCursor().onsuccess=function(t){var o=t.target.result;if(o){if(e(o.value))return n(o.value);o.continue()}else n(null)}}catch(e){o(e)}}))}))},o.prototype.findAll=function(e){var t=this;return new Promise((function(n,o){t.godb.getDB((function(r){try{var a=r.transaction(t.name,"readonly").objectStore(t.name),i=[];a.openCursor().onsuccess=function(t){var o=t.target.result;o?(e(o.value)&&i.push(o.value),o.continue()):n(i)}}catch(e){o(e)}}))}))},o.prototype.where=function(){},o.prototype.consoleTable=function(t){var n=this;return void 0===t&&(t=1e3),new Promise((function(o,r){n.godb.getDB((function(a){try{t>1e3&&(console.warn("Table.consoleTable() expects a limit no more than 1000"),t=1e3);var i=0,s={};a.transaction(n.name,"readonly").objectStore(n.name).openCursor().onsuccess=function(r){var a=r.target.result;a&&i<t?(i+=1,s[a.key]=e({},a.value),delete s[a.key].id,a.continue()):(console.log("Data in Table['"+n.name+"'] of Database['"+n.godb.name+"'], limit "+t+":"),console.table(s),o())}}catch(e){r(e)}}))}))},o}(),r=window,a=r.indexedDB||r.webkitIndexedDB||r.mozIndexedDB||r.msIndexedDB;return function(){function e(e,t,n){if(this.version=0,this.tables={},this._callbackQueue=[],this.name=e,n){var o=n.version;o&&(this.version=o)}this.updateSchema(t),this.getDB()}return e.prototype.table=function(e,t){return this.tables[e]?t&&(this.tables[e]=new o(this,e,t),this._shouldUpgrade()&&this.updateSchema()):(this.tables[e]=new o(this,e,t),this.updateSchema()),this.tables[e]},e.prototype.updateSchema=function(e){if(e)for(var t in this.tables={},e)this.tables[t]=new o(this,t,e[t]);this.idb&&(console.log("Updating Schema of Database['"+this.name+"']"),this.idb.close(),this.idb=null,this._openDB(++this.version,!0))},e.prototype.close=function(){this.idb?(this.idb.close(),this.idb=null,this._connection=null,this._closed=!0,console.log("A connection to Database['"+this.name+"'] is closed")):console.warn("Unable to close Database['"+this.name+"']: it is not opened yet")},e.prototype.drop=function(){var e=this;return new Promise((function(t,n){var o=e.name;e.close();var r=a.deleteDatabase(o);r.onsuccess=function(n){e.version=0,console.log("Database['"+o+"'] is successfully dropped"),e.onClosed&&("function"==typeof e.onClosed?e.onClosed():console.warn("'onClosed' should be a function, not "+typeof e.onClosed),e.onClosed=null),t(n)},r.onerror=function(e){var t=e.target.error,r=t.name,a=t.message;console.warn("Unable to drop Database['"+o+"'] \n- "+r+": "+a),n(r+": "+a)}}))},e.prototype.getDBState=function(){return this._closed?"closed":this.idb?"opened":this._connection?"connecting":"init"},e.prototype.getDB=function(e){this.idb?e&&"function"==typeof e&&e(this.idb):this._closed?console.warn("Database['"+this.name+"'] is closed, operations will not be executed"):this._connection?e&&"function"==typeof e&&this._callbackQueue.push(e):this.version?this._openDB(this.version):this._openDB()},e.prototype._openDB=function(e,t){var n=this,r=this.name,i=this.tables;this._connection=e?a.open(r,e):a.open(r),this._connection.onsuccess=function(e){var a=n._connection.result||e.target.result;n.version=a.version,n.idb||(n.idb=a,console.log("Database['"+r+"'] with version ("+a.version+") is exisiting"));for(var i=0,s=n.idb.objectStoreNames;i<s.length;i++){var c=s[i];n.tables[c]||(n.tables[c]=new o(n,c))}n._shouldUpgrade()&&!t?n.updateSchema():(console.log("A connection to Database['"+r+"'] is opening"),n._callbackQueue.length&&(n._callbackQueue.forEach((function(e){return e(n.idb)})),n._callbackQueue=[]),n.onOpened&&("function"==typeof n.onOpened?n.onOpened(n.idb):console.warn("'onOpened' should be a function, not "+typeof n.onOpened),n.onOpened=null))},this._connection.onupgradeneeded=function(e){var t=e.oldVersion,o=e.newVersion,a=e.target,s=a.transaction;for(var c in n.idb=a.result,n.version=o,0===t&&console.log("Creating Database['"+r+"'] with version ("+o+")"),i)n._updateObjectStore(c,i[c].schema,s);0!==t&&console.log("Database['"+r+"'] version changed from ("+t+") to ("+o+")")},this._connection.onerror=function(e){var t=e.target.error,n=t.name,o=t.message;throw Error("Failed to open Database['"+r+"']\n- "+n+": "+o)},this._connection.onblocked=function(e){var t=e,n=t.newVersion,o=t.oldVersion;console.warn("Database['"+r+"'] is opening somewhere with version ("+o+"),","thus new opening request to version ("+n+") was blocked.")}},e.prototype._shouldUpgrade=function(){var e=this.idb;if(!e)return!1;var t=this.tables;for(var n in t){if(!e.objectStoreNames.contains(n))return!0;var o=e.transaction(n,"readonly"),r=o.objectStore(n),a=r.indexNames,i=r.keyPath,s=r.autoIncrement;if("id"!==i||!s)throw this.close(),Error("The current existing objectStore '"+n+"' in IndexedDB '"+this.name+"' has a "+(s?"":"non-")+"autoIncrement keyPath `"+i+"`, while GoDB requires an autoIncrement keyPath `id`, thus GoDB can not open Database['"+this.name+"']");for(var c in t[n].schema)if(!a.contains(c))return o.abort(),!0;o.abort()}return!1},e.prototype._updateObjectStore=function(e,t,n){var o=this.idb;if(o){var r=function(n,r,a){"id"!==n&&(a&&r.deleteIndex(n),r.createIndex(n,n,{unique:!!t[n].unique,multiEntry:!!t[n].multiEntry}),console.log((a?"Updating":"Creating")+" Index['"+n+"']","in Table['"+e+"'], Database['"+o.name+"']"))};if(o.objectStoreNames.contains(e)){var a=(s=n.objectStore(e)).indexNames;for(var i in this.tables[e].schema)r(i,s,a.contains(i))}else{var s=o.createObjectStore(e,{keyPath:"id",autoIncrement:!0});for(var i in console.log("Creating Table['"+e+"'] in Database['"+o.name+"']"),t)r(i,s)}}},e}()}));
|