diff --git a/package-lock.json b/package-lock.json index 667d400..0da42f8 100755 --- a/package-lock.json +++ b/package-lock.json @@ -3541,9 +3541,9 @@ } }, "idb": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/idb/-/idb-3.0.2.tgz", - "integrity": "sha512-+FLa/0sTXqyux0o6C+i2lOR0VoS60LU/jzUo5xjfY6+7sEEgy4Gz1O7yFBXvjd7N0NyIGWIRg8DcQSLEG+VSPw==" + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/idb/-/idb-4.0.3.tgz", + "integrity": "sha512-moRlNNe0Gsvp4jAwz5cJ7scjyNTVA/cESKGCobULaljfaKZ970y8NDNCseHdMY+YxNXH58Z1V+7tTyf0GZyKqw==" }, "ieee754": { "version": "1.1.13", diff --git a/package.json b/package.json index 241dd74..c46e791 100755 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "@hibas123/utils": "^2.1.0", "aes-js": "^3.1.2", "feather-icons": "^4.22.1", - "idb": "3.0.2", + "idb": "^4.0.3", "js-sha256": "^0.9.0", "lodash.clonedeep": "^4.5.0", "secure-file-wrapper": "git+https://git.stamm.me/OpenServer/OSSecureFileWrapper.git", diff --git a/src/helper/indexeddb.ts b/src/helper/indexeddb.ts index d3b9ba3..c728a82 100755 --- a/src/helper/indexeddb.ts +++ b/src/helper/indexeddb.ts @@ -1,14 +1,12 @@ import { Lock } from "@hibas123/utils"; -import { DB, openDb, Transaction } from "idb"; +import { openDB, IDBPDatabase, IDBPTransaction, } from "idb"; export default class IDB { initLock = new Lock(); - db: DB; + db: IDBPDatabase; constructor(database: string, private stores: string[]) { - let lock = this.initLock.getLock(); - lock.then(async l => { - + this.initLock.getLock().then(async l => { let v = localStorage.getItem(database + "_version"); let version = 0; if (v) version = Number(v) @@ -21,12 +19,15 @@ export default class IDB { localStorage.setItem(database + "_version", version.toString()); localStorage.setItem(database + "_stores", stores.join(",")); - this.db = await openDb(database, version, db => { - console.log("IndexedDB need update") - stores.forEach(store => { - if (!db.objectStoreNames.contains(store)) - db.createObjectStore(store); - }) + this.db = await openDB(database, version, { + upgrade: (db) => { + console.log("IndexedDB need update") + const check = name => (db.objectStoreNames as DOMStringList).contains ? (db.objectStoreNames as DOMStringList).contains(name) : db.objectStoreNames.find(e => e === name); + stores.forEach(store => { + if (!check(store)) + db.createObjectStore(store); + }) + }, }) console.log("Got DATABASE", this.db) l.release(); @@ -48,42 +49,42 @@ export default class IDB { return { name: name, transaction: () => { - return this.db.transaction(name, "readwrite") + return this.db.transaction(name, "readwrite") as IDBPTransaction; }, - get: async (key: string, transaction?: Transaction): Promise => { + get: async (key: string, transaction?: IDBPTransaction): Promise => { (await this.initLock.getLock()).release() return (transaction || this.db.transaction(name)) .objectStore(name).get(key); }, - getAll: async (transaction?: Transaction): Promise => { + getAll: async (transaction?: IDBPTransaction): Promise => { (await this.initLock.getLock()).release() return (transaction || this.db.transaction(name)) .objectStore(name).getAll(); }, - set: async (key: string, val: T, transaction?: Transaction) => { + set: async (key: string, val: T, transaction?: IDBPTransaction) => { (await this.initLock.getLock()).release() const tx = (transaction || this.db.transaction(name, "readwrite")); tx.objectStore(name).put(val, key); - return tx.complete; + return tx.done; }, - delete: async (key: string, transaction?: Transaction) => { + delete: async (key: string, transaction?: IDBPTransaction) => { (await this.initLock.getLock()).release() const tx = (transaction || this.db.transaction(name, "readwrite")); tx.objectStore(name).delete(key); - return tx.complete; + return tx.done; }, - clear: async (transaction?: Transaction) => { + clear: async (transaction?: IDBPTransaction) => { (await this.initLock.getLock()).release() const tx = (transaction || this.db.transaction(name, "readwrite")); tx.objectStore(name).clear(); - return tx.complete; + return tx.done; }, - keys: async (transaction?: Transaction): Promise => { + keys: async (transaction?: IDBPTransaction): Promise => { (await this.initLock.getLock()).release() const tx = (transaction || this.db.transaction(name)); const keys: string[] = []; @@ -91,13 +92,15 @@ export default class IDB { // This would be store.getAllKeys(), but it isn't supported by Edge or Safari. // openKeyCursor isn't supported by Safari, so we fall back - (store.iterateKeyCursor || store.iterateCursor).call(store, (cursor: any) => { - if (!cursor) return; - keys.push(cursor.key); - cursor.continue(); - }); + store.getAllKeys() - return tx.complete.then(() => keys); + // (store.iterateKeyCursor || store.iterateCursor).call(store, (cursor: any) => { + // if (!cursor) return; + // keys.push(cursor.key); + // cursor.continue(); + // }); + + return tx.done.then(() => keys); } } } diff --git a/src/notes.ts b/src/notes.ts index ce38ac0..ef5681b 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -1,7 +1,7 @@ import SecureFile, { IFile } from "@hibas123/secure-file-wrapper"; import { Lock, Observable } from "@hibas123/utils"; import * as aesjs from "aes-js"; -import { Transaction } from "idb"; +import { IDBPTransaction } from "idb"; import { sha256 } from "js-sha256"; import * as uuidv4 from "uuid/v4"; import * as config from "../config.json"; @@ -480,7 +480,7 @@ class NotesProvider { return Decoder.decode(this._decrypt(msg, this.generalEncryption)) } - async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, date: Date, transaction?: Transaction) { + async addop(note_id: string, type: OpLogType, values: { value: Uint8Array, preview: Uint8Array }, date: Date, transaction?: IDBPTransaction) { let tx = transaction || this.oplogDB.transaction(); let oplog = await this.oplogDB.get(note_id, tx); if (!oplog) oplog = { logs: [], id: note_id };