importDb feature in wallet core

This commit is contained in:
Sebastian 2022-01-13 01:30:26 -03:00
parent 9f6e398884
commit 5eeb00e158
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -1873,3 +1873,30 @@ export function exportDb(db: IDBDatabase): Promise<any> {
}
});
}
export interface DatabaseDump {
name: string,
stores: { [s: string]: any },
version: string,
}
export function importDb(db: IDBDatabase, dump: DatabaseDump): Promise<any> {
return new Promise((resolve, reject) => {
const tx = db.transaction(Array.from(db.objectStoreNames), "readwrite");
tx.addEventListener("complete", () => {
tx.commit();
resolve(db);
});
for (let i = 0; i < db.objectStoreNames.length; i++) {
const name = db.objectStoreNames[i];
const storeDump = dump.stores[name];
if (!storeDump) continue;
Object.keys(storeDump).forEach(async key => {
const value = storeDump[key]
if (!value) return;
tx.objectStore(name).put(value)
})
}
});
}