UNPKG

cell

Version:

Single unit of I/O computations

1 lines 13.8 kB
!function(t,e,n){"function"==typeof define?define(e):"undefined"!=typeof module&&module.exports?module.exports=e():n[t]=e()}("IDBStore",function(){"use strict";var t=function(t){throw t},e={storeName:"Store",storePrefix:"IDBWrapper-",dbVersion:1,keyPath:"id",autoIncrement:!0,onStoreReady:function(){},onError:t,indexes:[]},n=function(t,n){"undefined"==typeof n&&"function"==typeof t&&(n=t),"[object Object]"!=Object.prototype.toString.call(t)&&(t={});for(var r in e)this[r]="undefined"!=typeof t[r]?t[r]:e[r];this.dbName=this.storePrefix+this.storeName,this.dbVersion=parseInt(this.dbVersion,10)||1,n&&(this.onStoreReady=n);var o="object"==typeof window?window:self;this.idb=o.indexedDB||o.webkitIndexedDB||o.mozIndexedDB,this.keyRange=o.IDBKeyRange||o.webkitIDBKeyRange||o.mozIDBKeyRange,this.features={hasAutoIncrement:!o.mozIndexedDB},this.consts={READ_ONLY:"readonly",READ_WRITE:"readwrite",VERSION_CHANGE:"versionchange",NEXT:"next",NEXT_NO_DUPLICATE:"nextunique",PREV:"prev",PREV_NO_DUPLICATE:"prevunique"},this.openDB()};n.prototype={constructor:n,version:"1.4.1",db:null,dbName:null,dbVersion:null,store:null,storeName:null,keyPath:null,autoIncrement:null,indexes:null,features:null,onStoreReady:null,onError:null,_insertIdCount:0,openDB:function(){var t=this.idb.open(this.dbName,this.dbVersion),e=!1;t.onerror=function(t){var e=!1;"error"in t.target?e="VersionError"==t.target.error.name:"errorCode"in t.target&&(e=12==t.target.errorCode),this.onError(e?new Error("The version number provided is lower than the existing one."):t)}.bind(this),t.onsuccess=function(t){if(!e){if(this.db)return void this.onStoreReady();if(this.db=t.target.result,"string"==typeof this.db.version)return void this.onError(new Error("The IndexedDB implementation in this browser is outdated. Please upgrade your browser."));if(!this.db.objectStoreNames.contains(this.storeName))return void this.onError(new Error("Something is wrong with the IndexedDB implementation in this browser. Please upgrade your browser."));var n=this.db.transaction([this.storeName],this.consts.READ_ONLY);this.store=n.objectStore(this.storeName);var r=Array.prototype.slice.call(this.getIndexList());this.indexes.forEach(function(t){var n=t.name;if(!n)return e=!0,void this.onError(new Error("Cannot create index: No index name given."));if(this.normalizeIndexData(t),this.hasIndex(n)){var o=this.store.index(n),i=this.indexComplies(o,t);i||(e=!0,this.onError(new Error('Cannot modify index "'+n+'" for current version. Please bump version number to '+(this.dbVersion+1)+"."))),r.splice(r.indexOf(n),1)}else e=!0,this.onError(new Error('Cannot create new index "'+n+'" for current version. Please bump version number to '+(this.dbVersion+1)+"."))},this),r.length&&(e=!0,this.onError(new Error('Cannot delete index(es) "'+r.toString()+'" for current version. Please bump version number to '+(this.dbVersion+1)+"."))),e||this.onStoreReady()}}.bind(this),t.onupgradeneeded=function(t){if(this.db=t.target.result,this.db.objectStoreNames.contains(this.storeName))this.store=t.target.transaction.objectStore(this.storeName);else{var n={autoIncrement:this.autoIncrement};null!==this.keyPath&&(n.keyPath=this.keyPath),this.store=this.db.createObjectStore(this.storeName,n)}var r=Array.prototype.slice.call(this.getIndexList());this.indexes.forEach(function(t){var n=t.name;if(n||(e=!0,this.onError(new Error("Cannot create index: No index name given."))),this.normalizeIndexData(t),this.hasIndex(n)){var o=this.store.index(n),i=this.indexComplies(o,t);i||(this.store.deleteIndex(n),this.store.createIndex(n,t.keyPath,{unique:t.unique,multiEntry:t.multiEntry})),r.splice(r.indexOf(n),1)}else this.store.createIndex(n,t.keyPath,{unique:t.unique,multiEntry:t.multiEntry})},this),r.length&&r.forEach(function(t){this.store.deleteIndex(t)},this)}.bind(this)},deleteDatabase:function(){this.idb.deleteDatabase&&this.idb.deleteDatabase(this.dbName)},put:function(e,n,o,i){null!==this.keyPath&&(i=o,o=n,n=e),i||(i=t),o||(o=r);var s,u=!1,a=null,c=this.db.transaction([this.storeName],this.consts.READ_WRITE);return c.oncomplete=function(){var t=u?o:i;t(a)},c.onabort=i,c.onerror=i,null!==this.keyPath?(this._addIdPropertyIfNeeded(n),s=c.objectStore(this.storeName).put(n)):s=c.objectStore(this.storeName).put(n,e),s.onsuccess=function(t){u=!0,a=t.target.result},s.onerror=i,c},get:function(e,n,o){o||(o=t),n||(n=r);var i=!1,s=null,u=this.db.transaction([this.storeName],this.consts.READ_ONLY);u.oncomplete=function(){var t=i?n:o;t(s)},u.onabort=o,u.onerror=o;var a=u.objectStore(this.storeName).get(e);return a.onsuccess=function(t){i=!0,s=t.target.result},a.onerror=o,u},remove:function(e,n,o){o||(o=t),n||(n=r);var i=!1,s=null,u=this.db.transaction([this.storeName],this.consts.READ_WRITE);u.oncomplete=function(){var t=i?n:o;t(s)},u.onabort=o,u.onerror=o;var a=u.objectStore(this.storeName)["delete"](e);return a.onsuccess=function(t){i=!0,s=t.target.result},a.onerror=o,u},batch:function(e,n,o){o||(o=t),n||(n=r),"[object Array]"!=Object.prototype.toString.call(e)&&o(new Error("dataArray argument must be of type Array."));var i=this.db.transaction([this.storeName],this.consts.READ_WRITE);i.oncomplete=function(){var t=a?n:o;t(a)},i.onabort=o,i.onerror=o;var s=e.length,u=!1,a=!1,c=function(){s--,0!==s||u||(u=!0,a=!0)};return e.forEach(function(t){var e=t.type,n=t.key,r=t.value,s=function(t){i.abort(),u||(u=!0,o(t,e,n))};if("remove"==e){var a=i.objectStore(this.storeName)["delete"](n);a.onsuccess=c,a.onerror=s}else if("put"==e){var l;null!==this.keyPath?(this._addIdPropertyIfNeeded(r),l=i.objectStore(this.storeName).put(r)):l=i.objectStore(this.storeName).put(r,n),l.onsuccess=c,l.onerror=s}},this),i},putBatch:function(t,e,n){var r=t.map(function(t){return{type:"put",value:t}});return this.batch(r,e,n)},removeBatch:function(t,e,n){var r=t.map(function(t){return{type:"remove",key:t}});return this.batch(r,e,n)},getBatch:function(e,n,o,i){o||(o=t),n||(n=r),i||(i="sparse"),"[object Array]"!=Object.prototype.toString.call(e)&&o(new Error("keyArray argument must be of type Array."));var s=this.db.transaction([this.storeName],this.consts.READ_ONLY);s.oncomplete=function(){var t=l?n:o;t(h)},s.onabort=o,s.onerror=o;var u=[],a=e.length,c=!1,l=!1,h=null,d=function(t){t.target.result||"dense"==i?u.push(t.target.result):"sparse"==i&&u.length++,a--,0===a&&(c=!0,l=!0,h=u)};return e.forEach(function(t){var e=function(t){c=!0,h=t,o(t),s.abort()},n=s.objectStore(this.storeName).get(t);n.onsuccess=d,n.onerror=e},this),s},getAll:function(e,n){n||(n=t),e||(e=r);var o=this.db.transaction([this.storeName],this.consts.READ_ONLY),i=o.objectStore(this.storeName);return i.getAll?this._getAllNative(o,i,e,n):this._getAllCursor(o,i,e,n),o},_getAllNative:function(t,e,n,r){var o=!1,i=null;t.oncomplete=function(){var t=o?n:r;t(i)},t.onabort=r,t.onerror=r;var s=e.getAll();s.onsuccess=function(t){o=!0,i=t.target.result},s.onerror=r},_getAllCursor:function(t,e,n,r){var o=[],i=!1,s=null;t.oncomplete=function(){var t=i?n:r;t(s)},t.onabort=r,t.onerror=r;var u=e.openCursor();u.onsuccess=function(t){var e=t.target.result;e?(o.push(e.value),e["continue"]()):(i=!0,s=o)},u.onError=r},clear:function(e,n){n||(n=t),e||(e=r);var o=!1,i=null,s=this.db.transaction([this.storeName],this.consts.READ_WRITE);s.oncomplete=function(){var t=o?e:n;t(i)},s.onabort=n,s.onerror=n;var u=s.objectStore(this.storeName).clear();return u.onsuccess=function(t){o=!0,i=t.target.result},u.onerror=n,s},_addIdPropertyIfNeeded:function(t){this.features.hasAutoIncrement||"undefined"!=typeof t[this.keyPath]||(t[this.keyPath]=this._insertIdCount++ +Date.now())},getIndexList:function(){return this.store.indexNames},hasIndex:function(t){return this.store.indexNames.contains(t)},normalizeIndexData:function(t){t.keyPath=t.keyPath||t.name,t.unique=!!t.unique,t.multiEntry=!!t.multiEntry},indexComplies:function(t,e){var n=["keyPath","unique","multiEntry"].every(function(n){if("multiEntry"==n&&void 0===t[n]&&e[n]===!1)return!0;if("keyPath"==n&&"[object Array]"==Object.prototype.toString.call(e[n])){var r=e.keyPath,o=t.keyPath;if("string"==typeof o)return r.toString()==o;if("function"!=typeof o.contains&&"function"!=typeof o.indexOf)return!1;if(o.length!==r.length)return!1;for(var i=0,s=r.length;s>i;i++)if(!(o.contains&&o.contains(r[i])||o.indexOf(-1!==r[i])))return!1;return!0}return e[n]==t[n]});return n},iterate:function(e,n){n=i({index:null,order:"ASC",autoContinue:!0,filterDuplicates:!1,keyRange:null,writeAccess:!1,onEnd:null,onError:t},n||{});var r="desc"==n.order.toLowerCase()?"PREV":"NEXT";n.filterDuplicates&&(r+="_NO_DUPLICATE");var o=!1,s=this.db.transaction([this.storeName],this.consts[n.writeAccess?"READ_WRITE":"READ_ONLY"]),u=s.objectStore(this.storeName);n.index&&(u=u.index(n.index)),s.oncomplete=function(){return o?void(n.onEnd?n.onEnd():e(null)):void n.onError(null)},s.onabort=n.onError,s.onerror=n.onError;var a=u.openCursor(n.keyRange,this.consts[r]);return a.onerror=n.onError,a.onsuccess=function(t){var r=t.target.result;r?(e(r.value,r,s),n.autoContinue&&r["continue"]()):o=!0},s},query:function(t,e){var n=[];return e=e||{},e.onEnd=function(){t(n)},this.iterate(function(t){n.push(t)},e)},count:function(e,n){n=i({index:null,keyRange:null},n||{});var r=n.onError||t,o=!1,s=null,u=this.db.transaction([this.storeName],this.consts.READ_ONLY);u.oncomplete=function(){var t=o?e:r;t(s)},u.onabort=r,u.onerror=r;var a=u.objectStore(this.storeName);n.index&&(a=a.index(n.index));var c=a.count(n.keyRange);return c.onsuccess=function(t){o=!0,s=t.target.result},c.onError=r,u},makeKeyRange:function(t){var e,n="undefined"!=typeof t.lower,r="undefined"!=typeof t.upper,o="undefined"!=typeof t.only;switch(!0){case o:e=this.keyRange.only(t.only);break;case n&&r:e=this.keyRange.bound(t.lower,t.upper,t.excludeLower,t.excludeUpper);break;case n:e=this.keyRange.lowerBound(t.lower,t.excludeLower);break;case r:e=this.keyRange.upperBound(t.upper,t.excludeUpper);break;default:throw new Error('Cannot create KeyRange. Provide one or both of "lower" or "upper" value, or an "only" value.')}return e}};var r=function(){},o={},i=function(t,e){var n,r;for(n in e)r=e[n],r!==o[n]&&r!==t[n]&&(t[n]=r);return t};return n.version=n.prototype.version,n},this),function(){var t,e,n,r,o,i=function(t,e){return function(){return t.apply(e,arguments)}};t=function(){function t(t){this.path=t,this.loaded=i(this.loaded,this),this.ready=i(this.ready,this),this.setup()}return t.prototype.ready=function(t){return this.isReady?t():this.onReady=t},t.prototype.loaded=function(){return this.isReady=!0,"function"==typeof this.onReady&&this.onReady(),this.onReady=null},t}(),("undefined"!=typeof process&&null!==process&&null!=(o=process.versions)?o.node:void 0)?(e=require("leveldb"),t.prototype.setup=function(t){var n=this;return e.open(this.path,{create_if_missing:!0},function(e,r){if(e)throw e;return n.store=r,n.loaded(),"function"==typeof t?t():void 0})},t.prototype.get=function(t,e){return this.store.get(t,e)},t.prototype.put=function(t,e,n){return this.store.put(t,e.toString(),n)},t.prototype.del=function(t,e){return this.store.del(t,e)},t.prototype.getAll=function(t){return this.store.iterator(function(e,n){var r;return e?t(e):(r={},n.forRange(function(e,n,o){return e?t(e):r[n]=o},function(){return t(null,r)}))})},t.prototype.clear=function(t){var n=this;return e.destroy(this.path,{},function(){return n.setup(t)})}):(t.prototype.setup=function(){return this.store=new IDBStore({keyPath:"id",autoIncrement:!1,onStoreReady:this.loaded})},r=function(t){return function(e){return t(null,e)}},n=function(t){return function(e){return t(e,null)}},t.prototype.get=function(t,e){return this.store.get(t,function(t){return e(null,null!=t?t.val:void 0)},n(e))},t.prototype.put=function(t,e,o){var i;return i={id:t,val:e},this.store.put(i,r(o),n(o))},t.prototype.del=function(t,e){return this.store.remove(t,r(e),n(e))},t.prototype.getAll=function(t){return this.store.getAll(function(e){var n,r,o,i;for(n={},o=0,i=e.length;i>o;o++)r=e[o],n[r.id]=r.val;return t(null,n)},n(t))},t.prototype.clear=function(t){return this.store.clear(function(e){return t(null,e)})}),null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=t:window.ILDB=t}.call(this),function(t){"function"==typeof define&&define.amd?define("cell",[],t):"object"==typeof exports?module.exports=t(require("ildb")):window.Cell=t(window.ILDB)}(function(t){var e,n,r=[],o={store:"cell.db"},i=function(n){var r=this;return n=n||{},this.options=o,Object.extend(this.options,n),e=new t(this.options.store),e.ready(function(){r.status.ready=!0,r._processQueue()}),this};return i.prototype={status:{ready:!1},_data:{},set:function(t){for(var r in t)this._data[r]=t[r],this.status.ready?e.put(r,t[r],function(t){t&&console.log(t)}):n("set",arguments)},get:function(t,r){this.status.ready?e.get(t,function(t,e){t&&console.log(t),r&&r(e)}):n("get",arguments)},remove:function(t){this.status.ready?e.del(t,function(t){t&&console.log(t)}):n("remove",arguments)},all:function(t){e.getAll(function(e,n){e&&console.log(e),t&&t(n)})},define:function(t,e){this[t]=e},extend:function(t){for(var e in t)this[e]=t[e]},check:function(t){t=t||{};var e=t.key||!1,n=t.value||!1,r=t.cb||function(){};e&&this.get(e,function(t){return t?n?r(t==n):void 0:r(!1)})},save:function(t){t=t||(new Date).getTime();var e="cell_"+t,n=new i({store:e});return this.all(function(t){n.set(t)}),t},load:function(t){if(t=t||!1){var e="cell_"+t,n=new i({store:e});n.all(function(t){this.set(t)})}},_processQueue:function(){for(var t in r){var e=r[t].action,n=r[t].args;this[e].apply(this,n)}}},n=function(t,e){r.push({action:t,args:e})},Object.extend=function(t,e){for(var n in e)e[n]&&e[n].constructor&&e[n].constructor===Object?(t[n]=t[n]||{},arguments.callee(t[n],e[n])):t[n]=e[n];return t},i});