UNPKG

web-tinydb

Version:

easy to use multi-table indexeddb lib

16 lines (13 loc) 9.85 kB
var DB=function(e){"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 t=function(){return(t=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},n=function(){function e(e){this.mode="readwrite";var t=e.db;this.name=e.name,this.db=t}return e.of=function(t){return new e(t)},e.prototype.setMode=function(e){return this.mode=e,this},e.prototype.getMode=function(){return this.mode},e.prototype.transaction=function(e){return this.db.transaction([e],this.getMode())},e.prototype.requestStore=function(){var e=this.name;return this.transaction(e).objectStore(e)},e.prototype.insert=function(e){var t=this;return new Promise((function(n,r){var o=t.requestStore().add(e),i=function(){n({msg:"add one record successfully!",status:!0}),o.removeEventListener("success",i),o.removeEventListener("error",u)},u=function(){r({msg:"add one record failed!",status:!1,activedRequest:o})};o.addEventListener("success",i),o.addEventListener("error",u)}))},e.prototype.update=function(e,n){var r=this;return new Promise((function(o,i){r.getByIndex(e).then((function(e){e.length||(console.warn("not found this record"),o([]));for(var u=function(e){var u=r.requestStore(),s=t(t({},e),n),c=u.put(s);c.onsuccess=function(){o({msg:"update successfully!",status:!0})},c.onerror=function(){i({msg:"update failed!",status:!1,activedRequest:c})}},s=0,c=e;s<c.length;s++){u(c[s])}}))}))},e.prototype.getByPrimaryKey=function(e){var t=this;return new Promise((function(n,r){var o=t.requestStore().get(e);o.onsuccess=function(){n(o.result)},o.onerror=function(){r({msg:"not found this primary key!",status:!1,activedRequest:o})}}))},e.prototype.getByIndex=function(e){var t=this;if(!e||!e.value)return Promise.reject({msg:"must have one index!"});var n=e.index,r=e.value;return new Promise((function(e,o){var i=t.requestStore().index(n).getAll(r);i.onsuccess=function(){i.result.length?e(i.result):(console.warn("not find record!"),e([]))},i.onerror=function(){o({msg:"not found this index!",status:!1,activedRequest:i})}}))},e.prototype.getAll=function(){var e=this;return new Promise((function(t,n){var r=e.requestStore().getAll();r.onsuccess=function(){t(r.result)},r.onerror=function(){n(r.result)}}))},e.prototype.limit=function(e){var t=this,n=e.start||0,r=e.length;if(!e||!e.length)throw console.error("please set length");return new Promise((function(e,o){var i=[],u=t.requestStore().openCursor();u.onsuccess=function(){var t=u.result;t?(t.key>n&&r&&(i.push(t.value),r--),0!==r?t.continue():e(i)):e(i)},u.onerror=function(){o(u.error)}}))},e.prototype.some=function(e){var t=this,n=e.index,r=e.lower,o=e.upper;return new Promise((function(e,i){var u=[],s=t.requestStore().index(n),c=IDBKeyRange.bound(r,o),a=s.openCursor(c);a.onsuccess=function(){var t=a.result;t?(u.push(t.value),t.continue()):e(u)},a.onerror=function(){i(a.error)}}))},e.prototype.deleteRecord=function(e){var t=this;return new Promise((function(n,r){if(!e)throw new Error("must be one index or option");return"object"==typeof e?t.deleteRecordByOption(e):t.deleteRecordByPrimaryKey(e)}))},e.prototype.deleteRecordByOption=function(e){var t=this;return new Promise((function(n,r){t.getByIndex(e).then((function(e){if(!e.length)return console.warn("not find this record"),!1;for(var o=0,i=e;o<i.length;o++){var u=i[o],s=t.requestStore(),c=s.delete(u[s.keyPath]);c.onsuccess=function(){n({msg:"delete successfully!",status:!0})},c.onerror=function(){r({msg:"delete failed!",status:!1})}}}))}))},e.prototype.deleteRecordByPrimaryKey=function(e){var t=this;return new Promise((function(n,r){t.getByPrimaryKey(e).then((function(e){if(!e)return console.warn("not find this record"),!1;var o=t.requestStore(),i=o.delete(e[o.keyPath]);i.onsuccess=function(){n({msg:"delete successfully!",status:!0})},i.onerror=function(){r({msg:"delete failed!",status:!1})}}))}))},e.prototype.clear=function(){var e=this;return new Promise((function(t,n){var r=e.requestStore().clear();r.onsuccess=function(){t({msg:"clear successfully!",status:!0})},r.onerror=function(){n({msg:"clear failed!",status:!1,activedRequest:r})}}))},e.prototype.destroyed=function(){this.db=void 0,this.name=""},e}(),r=("undefined"==typeof window?global:window).indexedDB,o=function(){function e(){}return e.of=function(){return new e},e.prototype.setup=function(e){return this.dbName=e.dbName,this},e.prototype.getVersion=function(){return this.version?this.version:1},e.prototype.setVersion=function(e){return this.version=e,this},e.prototype.upgrade=function(e,t){this.setVersion(e.version),this.db=e;for(var n=0,r=t;n<r.length;n++){var o=r[n];if(!e.objectStoreNames.contains(o.name)){var i=e.createObjectStore(o.name,{keyPath:o.primaryKey,autoIncrement:!!o.autoIncrement});if(o.indexs&&0!==o.indexs.length)for(var u=0,s=o.indexs;u<s.length;u++){var c=s[u];this.createIndex(i,{index:c.index,relativeIndex:c.relativeIndex,unique:c.unique})}}}},e.prototype.createIndex=function(e,t){e.createIndex(t.index,t.relativeIndex,{unique:t.unique})},e.prototype.createTable=function(e){var t=this;void 0===e&&(e=void 0);var n=this.dbName;if(void 0===e)return console.error("database table config must a list");var o=r.open(n,this.getVersion()),i=function(){t.upgrade(o.result,e),o.removeEventListener("upgradeneeded",i)};return o.addEventListener("upgradeneeded",i),new Promise((function(e,n){t.connect(o).then((function(e){var t=function(n){e.close(),e.removeEventListener("versionchange",t)};e.addEventListener("versionchange",t)}))}))},e.prototype.deleteDatabase=function(e){var t=r.deleteDatabase(e);return new Promise((function(e,n){t.onsuccess=function(){e({msg:"Database deleted successfully",status:!0})},t.onerror=function(){n({msg:"Database deleted failed",status:!0,activedRequest:t})}}))},e.prototype.connect=function(e,t){return new Promise((function(n,r){var o=function(){e.removeEventListener("success",i),e.removeEventListener("error",u)},i=function(){t&&t.successfully&&t.successfully(e),n(e.result),o()},u=function(){t&&t.error&&t.error(e),r(e.error),o()};e.addEventListener("success",i),e.addEventListener("error",u),e.addEventListener("blocked",(function(){t&&t.blocked&&t.blocked(e)}))}))},e.prototype.insert=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).insert(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e.prototype.updateRecord=function(e,t,o){var i=this;return new Promise((function(u,s){var c=r.open(i.dbName,i.getVersion());i.connect(c).then((function(r){n.of({name:e,db:r}).update(t,o).then((function(e){return u(e)})).catch((function(e){return s(e)}))}))}))},e.prototype.getAll=function(e){var t=this;return new Promise((function(o,i){var u=r.open(t.dbName,t.getVersion());t.connect(u).then((function(t){n.of({name:e,db:t}).getAll().then((function(e){return o(e)})).catch((function(e){return i(e)}))}))}))},e.prototype.getByPrimaryKey=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).getByPrimaryKey(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e.prototype.getByIndex=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).getByIndex(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e.prototype.deleteRecord=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).deleteRecord(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e.prototype.deleteTable=function(e){var t=this;return new Promise((function(n,o){var i=r.open(t.dbName,t.getVersion());i.onupgradeneeded=function(n){var r=i.result;t.db=r,t.setVersion(r.version),n.oldVersion<t.getVersion()&&r.deleteObjectStore(e)},i.onsuccess=function(){n({msg:" deleted table successfully!"})},i.onerror=function(){o({msg:" deleted table failed!"})}}))},e.prototype.some=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).some(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e.prototype.clearTableRecord=function(e){var t=this;return new Promise((function(o,i){var u=r.open(t.dbName,t.getVersion());t.connect(u).then((function(t){n.of({name:e,db:t}).clear().then((function(e){return o(e)})).catch((function(e){return i(e)}))}))}))},e.prototype.getDataWithLimits=function(e,t){var o=this;return new Promise((function(i,u){var s=r.open(o.dbName,o.getVersion());o.connect(s).then((function(r){n.of({name:e,db:r}).limit(t).then((function(e){return i(e)})).catch((function(e){return u(e)}))}))}))},e}();return window.TinyDB||(window.TinyDB=o),e.TinyDB=o,Object.defineProperty(e,"__esModule",{value:!0}),e}({});