PowerOfIndexedDB/jscompiled/IndexedDB.js

3 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2024-02-08 22:12:49 +03:00
(()=>{class t{on(t,e){this._events.has(t)?this._events.get(t).push(e):this._events.set(t,[e])}addEventListener(t,e){this.on(t,e)}dispatchEvent(t){this.emitEvent(t.type,t)}emitEvent(t,...e){let s=this._events.get(t);if(s)for(let t of s)t(...e)}constructor(){this._events=new Map}}class e extends t{constructor(){super(),this.IsReady=!1,e.Engine||e.PolyfillCompalibity()}scope(t){if(this.IsReady)return t();this.addEventListener("load",()=>t())}async connect(){this.IsReady=!1,this.DBRequest=e.Engine.open(this.Model.name,this.Model.version||0),this.DBRequest.addEventListener("upgradeneeded",this.executeMigrate.bind(this)),new s(this.DBRequest).sync().then(async t=>{this.DB=t,this.IsReady=!0,this.dispatchEvent(new Event("load"))})}executeMigrate(){let t=this.DBRequest.result;for(let e of this.Model.tables){let s;for(let n of(s=t.objectStoreNames.contains(e.name)?this.DBRequest.transaction.objectStore(e.name):t.createObjectStore(e.name,{keyPath:e.key}),e.columns.forEach(t=>{s.indexNames.contains(t)||s.createIndex(t,t)}),Array.from(s.indexNames)))e.columns.includes(n)||s.deleteIndex(n)}for(let e of Array.from(t.objectStoreNames))0!=this.Model.tables.filter(t=>t.name==e).length||t.deleteObjectStore(e);this.dispatchEvent(new Event("migrate"))}migrate(t){this.Model=t}async add(t,e){if(!this.IsReady)return;let n=this.DB.transaction([t],"readwrite").objectStore(t).add(e);return await new s(n).sync()}async clear(t){if(!this.IsReady)return;let e=this.DB.transaction([t],"readwrite").objectStore(t).clear();return await new s(e).sync()}async count(t){if(!this.IsReady)return;let e=this.DB.transaction([t],"readwrite").objectStore(t).count();return await new s(e).sync()}async delete(t,e){if(!this.IsReady)return;let n=this.DB.transaction([t],"readwrite").objectStore(t).delete(e);return await new s(n).sync()}async get(t,e){if(!this.IsReady)return;let n=this.DB.transaction([t],"readwrite").objectStore(t).get(e);return await new s(n).sync()}async getFrom(t,e,n){if(!this.IsReady)return;let i=this.DB.transaction([t],"readwrite").objectStore(t).index(e).get(n);return await new s(i).sync()}async getAll(t){if(!this.IsReady)return;let e=this.DB.transaction([t],"readwrite").objectStore(t).getAll();return await new s(e).sync()}async save(t,e,s){this.IsReady&&(await this.get(t,e)?await this.put(t,s):await this.add(t,s))}async put(t,e){if(!this.IsReady)return;let n=this.DB.transaction([t],"readwrite").objectStore(t).put(e);return await new s(n).sync()}async each(t,e){let s=this.DB.transaction([t],"readonly").objectStore(t).openCursor();await new Promise(t=>{s.onsuccess=function(){if(null!==s.result){let n=s.result.value;try{e(n,()=>s.result.continue(),()=>t(void 0),s.result.primaryKey)}catch{t(void 0)}}else t(void 0)}})}async subfilter(t,e,s,n){let i=[],a=[];for(let n of(await this.each(t,async(t,s,n,a)=>{await e(t,n)&&i.push(a),s()}),i)){let e=this.get(t,n);a.push(await s(e))}return"function"==typeof n&&n(a),a}async countFilter(t,e,s=!1){let n=0;return await this.each(t,async(t,i,a)=>{await e(t,a)&&(n++,s&&a()),i()}),n}close(){this.IsReady&&(this.dispatchEvent(new Event("close")),this.DB.close(),this.IsReady=!1)}static PolyfillCompalibity(){"indexedDB"in window&&(e.Engine=window.indexedDB),"mozIndexedDB"in window&&(e.Engine=window.mozIndexedDB),"webkitIndexedDB"in window&&(e.Engine=window.webkitIndexedDB),"msIndexedDB"in window&&(e.Engine=window.msIndexedDB)}}class s{constructor(t){this.result={then:[],catch:[],finally:[]},this.events={then:[],catch:[],finally:[]},t.onerror=t=>{this.result.catch=[t.target,t],this.events.catch.forEach(e=>{e.call(t.target,t.target?.result)}),this.events.finally.forEach(e=>{e.call(t.target,t.target?.result)})},t.onsuccess=t=>{this.result.then=[t.target,t],this.events.then.forEach(e=>{e.call(t.target,t.target?.result)})}}sync(){return new Promise((t,e)=>{this.then(t),this.catch(e)})}then(t){this.result.then.length?t.call(this.result.then[0],this.result.then[1]):this.events.then.push(t)}catch(t){this.result.catch.length?t.call(this.result.catch[0],this.result.catch[1]):this.events.catch.push(t)}finally(t){this.result.finally.l
//# sourceMappingURL=IndexedDB.js.map