mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
feat: implement database backup
This commit is contained in:
34
packages/core/database/backup.js
Normal file
34
packages/core/database/backup.js
Normal file
@@ -0,0 +1,34 @@
|
||||
export default class Backup {
|
||||
/**
|
||||
*
|
||||
* @param {import("../api/index.js").default} db
|
||||
*/
|
||||
constructor(db) {
|
||||
this._db = db;
|
||||
}
|
||||
|
||||
async export(encrypt = false) {
|
||||
const keys = await this._db.context.getAllKeys();
|
||||
const db = Object.fromEntries(await this._db.context.readMulti(keys));
|
||||
if (encrypt) {
|
||||
const key = await this._db.user.key();
|
||||
return JSON.stringify(
|
||||
await this._db.context.encrypt(key, JSON.stringify(db))
|
||||
);
|
||||
}
|
||||
return JSON.stringify(db);
|
||||
}
|
||||
|
||||
async import(data) {
|
||||
let backup = JSON.parse(data);
|
||||
//check if we have encrypted data
|
||||
if (backup.salt && backup.iv) {
|
||||
const key = await this._db.user.key();
|
||||
backup = JSON.parse(await this._db.context.decrypt(key, backup));
|
||||
}
|
||||
for (let key in backup) {
|
||||
let value = backup[key];
|
||||
await this._db.context.write(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user