fix: notebooks aren't imported from backup

This commit is contained in:
thecodrr
2021-08-16 11:03:31 +05:00
parent 89df937a48
commit 48e682eb2c
2 changed files with 38 additions and 1 deletions

View File

@@ -130,4 +130,16 @@ export default class Notebooks extends Collection {
await this._db.trash.add(notebookData); await this._db.trash.add(notebookData);
} }
} }
cleanup() {
for (let notebook of this.all) {
for (let topic of notebook.topics) {
const clonedIds = topic.notes.slice();
topic.notes = [];
for (let noteId of clonedIds) {
if (!!this._db.notes.note(noteId)) topic.notes.push(noteId);
}
}
}
}
} }

View File

@@ -114,6 +114,9 @@ export default class Backup {
"This backup was made from a newer version of Notesnook. Cannot migrate." "This backup was made from a newer version of Notesnook. Cannot migrate."
); );
// we have to reindex to make sure we handle all the items
// properly.
reindex(data);
const collections = [ const collections = [
{ {
index: data["notebooks"], index: data["notebooks"],
@@ -138,7 +141,9 @@ export default class Backup {
}, },
]; ];
await this._migrator.migrate(collections, (id) => data[id], version); if (await this._migrator.migrate(collections, (id) => data[id], version)) {
this._db.notebooks.cleanup();
}
} }
_validate(backup) { _validate(backup) {
@@ -174,3 +179,23 @@ function filterData(data) {
skippedKeys.forEach((key) => delete data[key]); skippedKeys.forEach((key) => delete data[key]);
return data; return data;
} }
function reindex(data) {
for (let key in data) {
const item = data[key];
switch (item.type) {
case "notebook":
if (!data["notebooks"]) data["notebooks"] = [];
data["notebooks"].push(item.id);
break;
case "note":
if (!data["notes"]) data["notes"] = [];
data["notes"].push(item.id);
break;
case "content":
if (!data["content"]) data["content"] = [];
data["content"].push(item.id);
break;
}
}
}