123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- const log$e = new Logger("db");
- class BaseTable {
- constructor(e, t) {
- E(this, "db");
- E(this, "isCreatingTable", !1);
- E(this, "hasCleared", !1);
- this.dbName = e,
- this.dbVersion = t
- }
- async clearDataBase(e) {
- return this.hasCleared || (e && (this.hasCleared = !0),
- !window.indexedDB.databases) ? Promise.resolve() : new Promise((t,r)=>{
- const n = window.indexedDB.deleteDatabase(this.dbName);
- n.onsuccess = ()=>{
- t()
- }
- ,
- n.onerror = r
- }
- )
- }
- tableName() {
- throw new Error("Derived class have to override 'tableName', and set a proper table name!")
- }
- keyPath() {
- throw new Error("Derived class have to override 'keyPath', and set a proper index name!")
- }
- index() {
- throw new Error("Derived class have to override 'index', and set a proper index name!")
- }
- async checkAndOpenDatabase() {
- return this.db ? Promise.resolve(this.db) : new Promise((e,t)=>{
- const n = setTimeout(()=>{
- log$e.info("wait db to open for", 200),
- this.db ? e(this.db) : e(this.checkAndOpenDatabase()),
- clearTimeout(n)
- }
- , 200);
- this.openDatabase(this.dbName, this.dbVersion || 1, ()=>{
- this.db && !this.isCreatingTable && e(this.db),
- log$e.info(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`),
- clearTimeout(n)
- }
- , ()=>{
- t(new Error("Failed to open database!")),
- clearTimeout(n)
- }
- , ()=>{
- this.db && e(this.db),
- clearTimeout(n),
- log$e.info(`successCallback called, this.db: ${!!this.db}, this.isCreatingStore: ${this.isCreatingTable}`)
- }
- )
- }
- )
- }
- openDatabase(e, t, r, n, o) {
- if (this.isCreatingTable)
- return;
- this.isCreatingTable = !0,
- log$e.info(e, t);
- const a = window.indexedDB.open(e, t)
- , s = this.tableName();
- a.onsuccess = l=>{
- this.db = a.result,
- log$e.info(`IndexedDb ${e} is opened.`),
- this.db.objectStoreNames.contains(s) && (this.isCreatingTable = !1),
- r && r(l)
- }
- ,
- a.onerror = l=>{
- var u;
- log$e.error("Failed to open database", (u = l == null ? void 0 : l.srcElement) == null ? void 0 : u.error),
- this.isCreatingTable = !1,
- n && n(l),
- this.clearDataBase(!0)
- }
- ,
- a.onupgradeneeded = l=>{
- const u = l.target.result
- , c = this.index();
- log$e.info(`Creating table ${s}.`);
- let h = u.objectStoreNames.contains(s);
- if (h)
- h = u.transaction([s], "readwrite").objectStore(s);
- else {
- const f = this.keyPath();
- h = u.createObjectStore(s, {
- keyPath: f
- })
- }
- c.map(f=>{
- h.createIndex(f, f, {
- unique: !1
- })
- }
- ),
- this.isCreatingTable = !1,
- log$e.info(`Table ${s} opened`),
- o && o(l)
- }
- }
- async add(e) {
- const t = this.tableName()
- , o = (await this.checkAndOpenDatabase()).transaction([t], "readwrite").objectStore(t).add(e);
- return new Promise(function(a, s) {
- o.onsuccess = l=>{
- a(l)
- }
- ,
- o.onerror = l=>{
- var u;
- log$e.error((u = l.srcElement) == null ? void 0 : u.error),
- s(l)
- }
- }
- )
- }
- async put(e) {
- const t = this.tableName()
- , o = (await this.checkAndOpenDatabase()).transaction([t], "readwrite").objectStore(t).put(e);
- return new Promise(function(a, s) {
- o.onsuccess = l=>{
- a(l)
- }
- ,
- o.onerror = l=>{
- var u;
- log$e.error("db put error", (u = l.srcElement) == null ? void 0 : u.error),
- s(l)
- }
- }
- )
- }
- delete(e, t, r) {
- const n = this.tableName();
- this.checkAndOpenDatabase().then(o=>{
- const s = o.transaction([n], "readwrite").objectStore(n).delete(e);
- s.onsuccess = t,
- s.onerror = r
- }
- )
- }
- update() {
- this.checkAndOpenDatabase().then(e=>{}
- )
- }
- async getAllKeys() {
- const e = this.tableName()
- , t = await this.checkAndOpenDatabase();
- return new Promise((r,n)=>{
- const a = t.transaction([e], "readonly").objectStore(e).getAllKeys();
- a.onsuccess = s=>{
- r(s.target.result)
- }
- ,
- a.onerror = s=>{
- log$e.error("db getAllKeys error", s),
- n(s)
- }
- }
- )
- }
- async query(e, t) {
- const r = this.tableName()
- , n = await this.checkAndOpenDatabase();
- return new Promise((o,a)=>{
- const u = n.transaction([r], "readonly").objectStore(r).index(e).get(t);
- u.onsuccess = function(c) {
- var f;
- const h = (f = c == null ? void 0 : c.target) == null ? void 0 : f.result;
- o && o(h)
- }
- ,
- u.onerror = c=>{
- log$e.error("db query error", c),
- a(c)
- }
- }
- )
- }
- async sleep(e) {
- return new Promise(t=>{
- setTimeout(()=>{
- t("")
- }
- , e)
- }
- )
- }
- }
|