idb-js
Version:
Simple to use indexdb
2 lines (1 loc) • 7.85 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Idb=t()}(this,function(){"use strict";function f(e){return function(){var u=e.apply(this,arguments);return new Promise(function(i,o){return function t(e,n){try{var r=u[e](n),a=r.value}catch(e){return void o(e)}if(!r.done)return Promise.resolve(a).then(function(e){t("next",e)},function(e){t("throw",e)});i(a)}("next")})}}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var e=function(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e};function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}e(t,[{key:"add",value:function(e){this.deps.push(e)}},{key:"notify",value:function(){for(var e=0;e<this.deps.length;e++)this.deps[e]();this.deps.length=0}}]);var a=t;function t(){n(this,t),this.deps=[]}var d="idb-js";function i(e){throw new Error(d+":"+(0<arguments.length&&void 0!==e?e:""))}function o(e){return"[object Array]"===Object.prototype.toString.call(e)}var u=window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,s=(window.IDBTransaction||window.webkitIDBTransaction||window.msIDBTransaction,window.IDBKeyRange||window.webkitIDBKeyRange||window.msIDBKeyRange,e(c,[{key:"open",value:function(e){var n=this,t=function(){},r=function(){};e&&(t=e.success||t,r=e.error||r),0!=this.tables.length||this._status?"function"!=typeof t?i("open中success必须是一个function类型"):((e=u.open(this.dbName,this.version)).onerror=function(e){r(e.currentTarget.error.message)},e.onsuccess=function(e){n.db=e.target.result,t(n.db),n._dep_.notify()},e.onupgradeneeded=function(e){n.idb=e.target.result;for(var t=0;t<n.tables.length;t++)n.__create_table(n.idb,n.tables[t])}):i("打开前要先用add_table添加表")}},{key:"add_table",value:function(){var e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};this._status=!1,this.tables.push(e)}},{key:"transaction",value:function(e){var t=this,n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"readwrite";return this.__action(function(){return t.db.transaction(e,n).objectStore(e)})}},{key:"getDB",value:function(){var e=this;return this.__action(function(){return e.db})}},{key:"query",value:function(e){var o=this,t=e.tableName,e=e.condition,u=void 0===e?function(){return!0}:e;return this.__action(function(){return o.__create_transaction(t,function(i){return new Promise((n=f(regeneratorRuntime.mark(function e(n,t){var r,a;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:try{r=[],a=0,o.__openCursor(i,function(){var t=f(regeneratorRuntime.mark(function e(t){return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t)return e.next=3,u(t.value,a);e.next=9;break;case 3:if(!e.sent){e.next=5;break}r.push(t.value);case 5:t.continue(),a++,e.next=10;break;case 9:n(r);case 10:case"end":return e.stop()}},e,o)}));return function(e){return t.apply(this,arguments)}}())}catch(e){t(e)}case 1:case"end":return e.stop()}},e,o)})),function(e,t){return n.apply(this,arguments)}));var n},"readonly")})}},{key:"add",value:function(e){var t=this,n=e.tableName,r=e.data;if(o(r)||(e=r,"[object Object]"===Object.prototype.toString.call(e)))return this.__action(function(){return t.__create_transaction(n,function(t){o(r)?r.forEach(function(e){return t.add(e)}):t.add(r)},"readwrite")});i("in insert,data type is Object or Array")}},{key:"delete",value:function(e){var o=this,t=e.tableName,e=e.condition,u=void 0===e?function(){return!0}:e;return this.__action(function(){return o.__create_transaction(t,function(n){return new Promise((r=f(regeneratorRuntime.mark(function e(r,t){var a,i;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:try{a=[],i=0,o.__openCursor(n,function(){var t=f(regeneratorRuntime.mark(function e(t){var n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t)return n=t.value,e.next=4,u(n,i);e.next=11;break;case 4:if(!e.sent){e.next=7;break}t.delete(n),a.push(n);case 7:t.continue(),i++,e.next=12;break;case 11:r(a);case 12:case"end":return e.stop()}},e,o)}));return function(e){return t.apply(this,arguments)}}())}catch(e){t(e)}case 1:case"end":return e.stop()}},e,o)})),function(e,t){return r.apply(this,arguments)}));var r},"readwrite")})}},{key:"update",value:function(e){var o=this,t=e.tableName,u=e.handler,e=e.condition,c=void 0===e?function(){return!0}:e;return this.__action(function(){return o.__create_transaction(t,function(n){return new Promise((r=f(regeneratorRuntime.mark(function e(r,t){var a,i;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:try{a=[],i=0,o.__openCursor(n,function(){var t=f(regeneratorRuntime.mark(function e(t){var n;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t)return n=t.value,e.next=4,c(n,i);e.next=13;break;case 4:if(e.sent)return e.next=7,u(n);e.next=9;break;case 7:t.update(n),a.push(n);case 9:t.continue(),i++,e.next=14;break;case 13:r(a);case 14:case"end":return e.stop()}},e,o)}));return function(e){return t.apply(this,arguments)}}())}catch(e){t(e)}case 1:case"end":return e.stop()}},e,o)})),function(e,t){return r.apply(this,arguments)}));var r},"readwrite")})}},{key:"__openCursor",value:function(e,t){e.openCursor().onsuccess=function(e){e=e.target.result;t(e)}}},{key:"__create_transaction",value:function(o,u){var n,c=this,s=2<arguments.length&&void 0!==arguments[2]?arguments[2]:"readwrite";return new Promise((n=f(regeneratorRuntime.mark(function e(t,n){var r,a,i;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(o&&s){e.next=3;break}throw n(),new Error("in __create_transaction,tableName and mode is required");case 3:return r=c.db.transaction(o,s),a=r.objectStore(o),i=null,e.prev=6,e.next=9,u(a);case 9:i=e.sent,e.next=15;break;case 12:e.prev=12,e.t0=e.catch(6),n(e.t0);case 15:r.oncomplete=function(e){t({data:i,event:e})},r.onerror=function(e){n({name:d,msg:e.target.error,event:e})};case 17:case"end":return e.stop()}},e,c,[[6,12]])})),function(e,t){return n.apply(this,arguments)}))}},{key:"__action",value:function(a){var i=this;return new Promise(function(n,r){e=f(regeneratorRuntime.mark(function e(){var t;return regeneratorRuntime.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,a();case 3:t=e.sent,n(t),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),r(e.t0);case 10:case"end":return e.stop()}},e,i,[[0,7]])}));var e,t=function(){return e.apply(this,arguments)};i.db?t():i._dep_.add(t)})}},{key:"__create_table",value:function(t,e){var n=e.tableName,r=e.option,e=e.indexs,e=void 0===e?[]:e;if(!t.objectStoreNames.contains(n)){var a=t.createObjectStore(n,r),i=!0,t=!1,n=void 0;try{for(var o,u=e[Symbol.iterator]();!(i=(o=u.next()).done);i=!0){var c=o.value;this.__create_index(a,c)}}catch(e){t=!0,n=e}finally{try{!i&&u.return&&u.return()}finally{if(t)throw n}}}}},{key:"__create_index",value:function(e,t){var n=t.key,t=t.option;e.createIndex(n,n,t)}}]),c);function c(e){var t=e.dbName,e=e.version;n(this,c),this.dbName=t,this.version=e,this.db=null,this.idb=null,this.tables=[],this._status=!1,this._dep_=new a}return function(e){var n=e.dbName,t=void 0===(t=e.version)?(new Date).getTime():t,e=void 0===(e=e.tables)?[]:e,r=new s({dbName:n,version:t}),a=!0,t=!1,i=void 0;try{for(var o,u=e[Symbol.iterator]();!(a=(o=u.next()).done);a=!0){var c=o.value;r.add_table(c)}}catch(e){t=!0,i=e}finally{try{!a&&u.return&&u.return()}finally{if(t)throw i}}return new Promise(function(e,t){r.open({success:function(){!function(e){console.log(d+":"+(0<arguments.length&&void 0!==e?e:""))}("数据库 "+n+" 已经打开"),e(r)},error:function(e){t(e)}})})}});