@jacobmarshall/kv
Version:
A super simple key value store backed by IndexedDB
1 lines • 1.16 kB
JavaScript
!function(){"use strict";function n(n){function e(){return i||(i=new Promise(function(e,r){var o=indexedDB.open(n,1);o.onerror=function(){r(o.error)},o.onupgradeneeded=function(){o.result.createObjectStore(t)},o.onsuccess=function(){e(o.result)}})),i}function u(n,r){return e().then(function(e){return new Promise(function(o,u){var i=e.transaction(t,n);i.oncomplete=function(){o()},i.onerror=function(){u(i.error)},r(i.objectStore(t))})})}var i;return{get:function(n){var e;return u(r,function(t){e=t.get(n)}).then(function(){return e.result})},set:function(n,e){return u(o,function(t){t.put(e,n)})},remove:function(n){return u(o,function(e){e.delete(n)})},clear:function(){return u(o,function(n){n.clear()})},keys:function(){var n=[];return u(r,function(e){(e.openKeyCursor||e.openCursor).call(e).onsuccess=function(){this.result&&(n.push(this.result.key),this.result.continue())}}).then(function(){return n})}}}var e="kv",t="kv",r="readonly",o="readwrite",u=n(e);for(var i in u)u.hasOwnProperty(i)&&(n[i]=u[i]);"undefined"!=typeof module&&module.exports?module.exports=n:"function"==typeof define&&define.amd?define("kv",[],function(){return n}):self.kv=n}();