UNPKG

@blakewatson/datastore

Version:

Save data to IndexedDB using localStorage APIs.

2 lines (1 loc) 3.22 kB
var w="Default DB",g="data",b=class h{dbName=null;storeName=null;static setupDb(e){let{name:s,version:t=1,storesToCreate:r=g,onUpgradeNeeded:n=null}=e;return new Promise((o,a)=>{let i=indexedDB.open(s,t);i.onerror=c=>{console.error("Error opening database:",c.target.error),a(new Error("Error opening database"))},i.onsuccess=c=>{o(i.result)},i.onupgradeneeded=c=>{let u=i.result,l=u.objectStoreNames,m=[];if(Array.isArray(r))r.forEach(d=>{l.contains(d)||(u.createObjectStore(d),m.push(new h(s,d)))});else{if(l.contains(r))return;u.createObjectStore(r),m.push(new h(s,r))}n&&n(u,m)}})}constructor(e=w,s=g){this.dbName=e,this.storeName=s}getDb(){return new Promise((e,s)=>{let t=indexedDB.open(this.dbName,this.version);t.onerror=r=>{console.error("Error opening database:",r.target.error),s(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 s=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=s.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),s.close()}})}async setItem(e,s){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 o=t.transaction(this.storeName,"readwrite");o.onerror=c=>n(o.error);let i=o.objectStore(this.storeName).put(s,e);i.onerror=c=>n(i.error),i.onsuccess=()=>{t.close(),r()}})}async removeItem(e){let s=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=s.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=()=>{s.close(),t()}})}async keys(){let e=await this.getDb();return new Promise((s,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 o=r.objectStore(this.storeName).getAllKeys();o.onerror=a=>t(o.error),o.onsuccess=()=>{s(o.result),e.close()}})}async count(){let e=await this.getDb();return new Promise((s,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 o=r.objectStore(this.storeName).count();o.onerror=a=>t(o.error),o.onsuccess=()=>{s(o.result),e.close()}})}async length(){return this.count()}async clear(){let e=await this.getDb();return new Promise((s,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 o=r.objectStore(this.storeName).clear();o.onerror=a=>t(o.error),o.onsuccess=()=>{e.close(),s()}})}};export{b as default};