function promisifyRequest(request){return new Promise(((resolve,reject)=>{request.oncomplete=request.onsuccess=()=>resolve(request.result),request.onabort=request.onerror=()=>reject(request.error)}))}function createStore(dbName,storeName){const request=indexedDB.open(dbName);request.onupgradeneeded=()=>request.result.createObjectStore(storeName);const dbp=promisifyRequest(request);return(txMode,callback)=>dbp.then((db=>callback(db.transaction(storeName,txMode).objectStore(storeName))))}let defaultGetStoreFunc;function defaultGetStore(){return defaultGetStoreFunc||(defaultGetStoreFunc=createStore("keyval-store","keyval")),defaultGetStoreFunc}function get(key,customStore=defaultGetStore()){return customStore("readonly",(store=>promisifyRequest(store.get(key))))}function set(key,value,customStore=defaultGetStore()){return customStore("readwrite",(store=>(store.put(value,key),promisifyRequest(store.transaction))))}function setMany(entries,customStore=defaultGetStore()){return customStore("readwrite",(store=>(entries.forEach((entry=>store.put(entry[1],entry[0]))),promisifyRequest(store.transaction))))}function getMany(keys,customStore=defaultGetStore()){return customStore("readonly",(store=>Promise.all(keys.map((key=>promisifyRequest(store.get(key)))))))}function update(key,updater,customStore=defaultGetStore()){return customStore("readwrite",(store=>new Promise(((resolve,reject)=>{store.get(key).onsuccess=function(){try{store.put(updater(this.result),key),resolve(promisifyRequest(store.transaction))}catch(err){reject(err)}}}))))}function del(key,customStore=defaultGetStore()){return customStore("readwrite",(store=>(store.delete(key),promisifyRequest(store.transaction))))}function delMany(keys,customStore=defaultGetStore()){return customStore("readwrite",(store=>(keys.forEach((key=>store.delete(key))),promisifyRequest(store.transaction))))}function clear(customStore=defaultGetStore()){return customStore("readwrite",(store=>(store.clear(),promisifyRequest(store.transaction))))}function eachCursor(store,callback){return store.openCursor().onsuccess=function(){this.result&&(callback(this.result),this.result.continue())},promisifyRequest(store.transaction)}function keys(customStore=defaultGetStore()){return customStore("readonly",(store=>{if(store.getAllKeys)return promisifyRequest(store.getAllKeys());const items=[];return eachCursor(store,(cursor=>items.push(cursor.key))).then((()=>items))}))}function values(customStore=defaultGetStore()){return customStore("readonly",(store=>{if(store.getAll)return promisifyRequest(store.getAll());const items=[];return eachCursor(store,(cursor=>items.push(cursor.value))).then((()=>items))}))}function entries(customStore=defaultGetStore()){return customStore("readonly",(store=>{if(store.getAll&&store.getAllKeys)return Promise.all([promisifyRequest(store.getAllKeys()),promisifyRequest(store.getAll())]).then((([keys,values])=>keys.map(((key,i)=>[key,values[i]]))));const items=[];return customStore("readonly",(store=>eachCursor(store,(cursor=>items.push([cursor.key,cursor.value]))).then((()=>items))))}))}export{clear,createStore,del,delMany,entries,get,getMany,keys,promisifyRequest,set,setMany,update,values};