Updaing to IDB 4x

This commit is contained in:
Fabian 2019-07-02 18:34:41 +02:00
parent 93b272ecb5
commit c3f9529feb
4 changed files with 36 additions and 33 deletions

6
package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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 => (<any>db.objectStoreNames as DOMStringList).contains ? (<any>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 <any>this.db.transaction(name, "readwrite") as IDBPTransaction<unknown, string[]>;
},
get: async (key: string, transaction?: Transaction): Promise<T> => {
get: async (key: string, transaction?: IDBPTransaction<unknown, string[]>): Promise<T> => {
(await this.initLock.getLock()).release()
return (transaction || this.db.transaction(name))
.objectStore(name).get(key);
},
getAll: async (transaction?: Transaction): Promise<T[]> => {
getAll: async (transaction?: IDBPTransaction<unknown, string[]>): Promise<T[]> => {
(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<unknown, string[]>) => {
(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<unknown, string[]>) => {
(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<unknown, string[]>) => {
(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<string[]> => {
keys: async (transaction?: IDBPTransaction<unknown, string[]>): Promise<string[]> => {
(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);
}
}
}

View File

@ -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<unknown, string[]>) {
let tx = transaction || this.oplogDB.transaction();
let oplog = await this.oplogDB.get(note_id, tx);
if (!oplog) oplog = { logs: [], id: note_id };