fix: filter out unnecessary keys when creating backups

This commit is contained in:
thecodrr
2021-07-06 12:11:12 +05:00
parent 7473f63006
commit 33a3fa7968

View File

@@ -6,7 +6,8 @@ import {
} from "../common.js"; } from "../common.js";
import SparkMD5 from "spark-md5"; import SparkMD5 from "spark-md5";
const invalidKeys = ["user", "t", "lastBackupTime"]; const invalidKeys = ["user", "t", "v", "lastBackupTime", "lastSynced"];
const invalidIndices = ["tags", "colors"];
const validTypes = ["mobile", "web", "node"]; const validTypes = ["mobile", "web", "node"];
export default class Backup { export default class Backup {
/** /**
@@ -34,11 +35,11 @@ export default class Backup {
if (!validTypes.some((t) => t === type)) if (!validTypes.some((t) => t === type))
throw new Error("Invalid type. It must be one of 'mobile' or 'web'."); throw new Error("Invalid type. It must be one of 'mobile' or 'web'.");
const keys = (await this._db.context.getAllKeys()).filter( let keys = await this._db.context.getAllKeys();
(key) => !invalidKeys.some((t) => t === key) let data = filterData(
Object.fromEntries(await this._db.context.readMulti(keys))
); );
let data = Object.fromEntries(await this._db.context.readMulti(keys));
let hash = {}; let hash = {};
if (encrypt) { if (encrypt) {
@@ -114,10 +115,6 @@ export default class Backup {
); );
const collections = [ const collections = [
{
index: data["notes"],
dbCollection: this._db.notes,
},
{ {
index: data["notebooks"], index: data["notebooks"],
dbCollection: this._db.notebooks, dbCollection: this._db.notebooks,
@@ -130,6 +127,10 @@ export default class Backup {
index: data["content"], index: data["content"],
dbCollection: this._db.content, dbCollection: this._db.content,
}, },
{
index: data["notes"],
dbCollection: this._db.notes,
},
{ {
index: ["settings"], index: ["settings"],
dbCollection: this._db.settings, dbCollection: this._db.settings,
@@ -161,3 +162,15 @@ export default class Backup {
} }
} }
} }
function filterData(data) {
let skippedKeys = [...invalidKeys, ...invalidIndices];
invalidIndices.forEach((key) => {
const index = data[key];
if (!index) return;
skippedKeys.push(...index);
});
skippedKeys.forEach((key) => delete data[key]);
return data;
}