UNPKG

@blakewatson/datastore

Version:

Save data to IndexedDB using localStorage APIs.

3 lines (2 loc) 3.73 kB
var _DataStoreExports=(()=>{var l=Object.defineProperty;var N=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var p=(c,e)=>{for(var o in e)l(c,o,{get:e[o],enumerable:!0})},E=(c,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of f(e))!S.call(c,r)&&r!==o&&l(c,r,{get:()=>e[r],enumerable:!(t=N(e,r))||t.enumerable});return c};var v=c=>E(l({},"__esModule",{value:!0}),c);var y={};p(y,{default:()=>d});var D="Default DB",w="data",d=class c{dbName=null;storeName=null;static setupDb(e){let{name:o,version:t=1,storesToCreate:r=w,onUpgradeNeeded:n=null}=e;return new Promise((s,a)=>{let i=indexedDB.open(o,t);i.onerror=u=>{console.error("Error opening database:",u.target.error),a(new Error("Error opening database"))},i.onsuccess=u=>{s(i.result)},i.onupgradeneeded=u=>{let m=i.result,g=m.objectStoreNames,b=[];if(Array.isArray(r))r.forEach(h=>{g.contains(h)||(m.createObjectStore(h),b.push(new c(o,h)))});else{if(g.contains(r))return;m.createObjectStore(r),b.push(new c(o,r))}n&&n(m,b)}})}constructor(e=D,o=w){this.dbName=e,this.storeName=o}getDb(){return new Promise((e,o)=>{let t=indexedDB.open(this.dbName,this.version);t.onerror=r=>{console.error("Error opening database:",r.target.error),o(new Error("Error opening database"))},t.onsuccess=r=>{e(t.result)},t.onupgradeneeded=r=>{console.log("Upgrade needed");let n=t.result;n.objectStoreNames.contains(this.storeName)||n.createObjectStore(this.storeName)}})}async getItem(e){let o=await this.getDb();if(typeof e=="number"&&(e=e.toString()),typeof e!="string")throw new Error("Key must be a string");return new Promise((t,r)=>{if(!this.storeName){r(new Error("Store name not set"));return}let n=o.transaction(this.storeName,"readonly");n.onerror=i=>r(n.error);let a=n.objectStore(this.storeName).get(e);a.onerror=i=>r(a.error),a.onsuccess=()=>{t(a.result),o.close()}})}async setItem(e,o){let t=await this.getDb();if(typeof e=="number"&&(e=e.toString()),typeof e!="string")throw new Error("Key must be a string");return new Promise((r,n)=>{if(!this.storeName){n(new Error("Store name not set"));return}let s=t.transaction(this.storeName,"readwrite");s.onerror=u=>n(s.error);let i=s.objectStore(this.storeName).put(o,e);i.onerror=u=>n(i.error),i.onsuccess=()=>{t.close(),r()}})}async removeItem(e){let o=await this.getDb();if(typeof e=="number"&&(e=e.toString()),typeof e!="string")throw new Error("Key must be a string");return new Promise((t,r)=>{if(!this.storeName){r(new Error("Store name not set"));return}let n=o.transaction(this.storeName,"readwrite");n.onerror=i=>r(n.error);let a=n.objectStore(this.storeName).delete(e);a.onerror=i=>r(a.error),a.onsuccess=()=>{o.close(),t()}})}async keys(){let e=await this.getDb();return new Promise((o,t)=>{if(!this.storeName){t(new Error("Store name not set"));return}let r=e.transaction(this.storeName,"readonly");r.onerror=a=>t(r.error);let s=r.objectStore(this.storeName).getAllKeys();s.onerror=a=>t(s.error),s.onsuccess=()=>{o(s.result),e.close()}})}async count(){let e=await this.getDb();return new Promise((o,t)=>{if(!this.storeName){t(new Error("Store name not set"));return}let r=e.transaction(this.storeName,"readonly");r.onerror=a=>t(r.error);let s=r.objectStore(this.storeName).count();s.onerror=a=>t(s.error),s.onsuccess=()=>{o(s.result),e.close()}})}async length(){return this.count()}async clear(){let e=await this.getDb();return new Promise((o,t)=>{if(!this.storeName){t(new Error("Store name not set"));return}let r=e.transaction(this.storeName,"readwrite");r.onerror=a=>t(r.error);let s=r.objectStore(this.storeName).clear();s.onerror=a=>t(s.error),s.onsuccess=()=>{e.close(),o()}})}};return v(y);})(); self.DataStore = _DataStoreExports.default;