UNPKG

lokijs

Version:

Fast document oriented javascript in-memory database

1 lines 5.26 kB
var lokiIndexedAdapter=function(){function t(t){this.app="loki","undefined"!=typeof t&&(this.app=t),this.catalog=null,this.checkAvailability()||console.error("indexedDB does not seem to be supported for your environment")}function o(t){this.db=null,this.initializeLokiCatalog(t)}return t.prototype.checkAvailability=function(){return window&&window.indexedDB?!0:!1},t.prototype.loadDatabase=function(t,e){var n=this.app,r=this;return null===this.catalog?void(this.catalog=new o(function(o){r.catalog=o,r.loadDatabase(t,e)})):void this.catalog.getAppKey(n,t,function(t){if("function"==typeof e){if(0===t.id)return console.warn("loki indexeddb adapter could not find database"),void e(null);e(t.val)}else console.log(t.val)})},t.prototype.loadKey=t.prototype.loadDatabase,t.prototype.saveDatabase=function(t,e,n){var r=this.app,a=this;return null===this.catalog?void(this.catalog=new o(function(o){a.catalog=o,o.setAppKey(r,t,e,n)})):void this.catalog.setAppKey(r,t,e,n)},t.prototype.saveKey=t.prototype.saveDatabase,t.prototype.deleteDatabase=function(t){var e=this.app,n=this;return null===this.catalog?void(this.catalog=new o(function(o){n.catalog=o,n.deleteDatabase(t)})):void this.catalog.getAppKey(e,t,function(t){var o=t.id;0!==o&&n.catalog.deleteAppKey(o)})},t.prototype.deleteKey=t.prototype.deleteDatabase,t.prototype.getDatabaseList=function(t){var e=this.app,n=this;return null===this.catalog?void(this.catalog=new o(function(o){n.catalog=o,n.getDatabaseList(t)})):void this.catalog.getAppKeys(e,function(o){for(var e=[],n=0;n<o.length;n++)e.push(o[n].key);"function"==typeof t?t(e):e.forEach(function(t){console.log(t)})})},t.prototype.getKeyList=t.prototype.getDatabaseList,t.prototype.getCatalogSummary=function(t){var e=(this.app,this);return null===this.catalog?void(this.catalog=new o(function(o){e.catalog=o,e.getCatalogSummary(t)})):void this.catalog.getAllKeys(function(o){for(var e,n,r,a,i,c=[],s=0;s<o.length;s++)e=o[s],r=e.app||"",a=e.key||"",i=e.val||"",n=2*r.length+2*a.length+i.length+1,c.push({app:e.app,key:e.key,size:n});"function"==typeof t?t(c):c.forEach(function(t){console.log(t)})})},o.prototype.initializeLokiCatalog=function(t){var o=indexedDB.open("LokiCatalog",1),e=this;o.onupgradeneeded=function(t){var o=t.target.result;if(o.objectStoreNames.contains("LokiAKV")&&o.deleteObjectStore("LokiAKV"),!o.objectStoreNames.contains("LokiAKV")){var e=o.createObjectStore("LokiAKV",{keyPath:"id",autoIncrement:!0});e.createIndex("app","app",{unique:!1}),e.createIndex("key","key",{unique:!1}),e.createIndex("appkey","appkey",{unique:!0})}},o.onsuccess=function(o){e.db=o.target.result,"function"==typeof t&&t(e)},o.onerror=function(t){throw t}},o.prototype.getAppKey=function(t,o,e){var n=this.db.transaction(["LokiAKV"],"readonly"),r=n.objectStore("LokiAKV"),a=r.index("appkey"),i=t+","+o,c=a.get(i);c.onsuccess=function(t){return function(o){var e=o.target.result;"undefined"==typeof e&&(e={id:0,success:!1}),"function"==typeof t?t(e):console.log(e)}}(e),c.onerror=function(t){return function(o){if("function"!=typeof t)throw o;t({id:0,success:!1})}}(e)},o.prototype.getAppKeyById=function(t,o,e){var n=this.db.transaction(["LokiAKV"],"readonly"),r=n.objectStore("LokiAKV"),a=r.get(t);a.onsuccess=function(t,o){return function(e){"function"==typeof o?o(e.target.result,t):console.log(e.target.result)}}(e,o)},o.prototype.setAppKey=function(t,o,e,n){var r=this.db.transaction(["LokiAKV"],"readwrite"),a=r.objectStore("LokiAKV"),i=a.index("appkey"),c=t+","+o,s=i.get(c);s.onsuccess=function(r){var i=r.target.result;null==i?i={app:t,key:o,appkey:t+","+o,val:e}:i.val=e;var c=a.put(i);c.onerror=function(t){return function(){"function"==typeof t?t({success:!1}):(console.error("LokiCatalog.setAppKey (set) onerror"),console.error(s.error))}}(n),c.onsuccess=function(t){return function(){"function"==typeof t&&t({success:!0})}}(n)},s.onerror=function(t){return function(){"function"==typeof t?t({success:!1}):(console.error("LokiCatalog.setAppKey (get) onerror"),console.error(s.error))}}(n)},o.prototype.deleteAppKey=function(t,o){var e=this.db.transaction(["LokiAKV"],"readwrite"),n=e.objectStore("LokiAKV"),r=n.delete(t);r.onsuccess=function(t){return function(){"function"==typeof t&&t({success:!0})}}(o),r.onerror=function(t){return function(){"function"==typeof t?t(!1):(console.error("LokiCatalog.deleteAppKey raised onerror"),console.error(r.error))}}(o)},o.prototype.getAppKeys=function(t,o){var e=this.db.transaction(["LokiAKV"],"readonly"),n=e.objectStore("LokiAKV"),r=n.index("app"),a=IDBKeyRange.only(t),i=r.openCursor(a),c=[];i.onsuccess=function(t,o){return function(e){var n=e.target.result;if(n){var r=n.value;t.push(r),n.continue()}else"function"==typeof o?o(t):console.log(t)}}(c,o),i.onerror=function(t){return function(o){"function"==typeof t?t(null):(console.error("LokiCatalog.getAppKeys raised onerror"),console.error(o))}}(o)},o.prototype.getAllKeys=function(t){var o=this.db.transaction(["LokiAKV"],"readonly"),e=o.objectStore("LokiAKV"),n=e.openCursor(),r=[];n.onsuccess=function(t,o){return function(e){var n=e.target.result;if(n){var r=n.value;t.push(r),n.continue()}else"function"==typeof o?o(t):console.log(t)}}(r,t),n.onerror=function(t){return function(){"function"==typeof t&&t(null)}}(t)},t}();