Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: notebooks aren't imported from backup

thecodrr48e682eb2c86
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
index fcf1c8280..22693360c 100644
--- a/packages/core/collections/notebooks.js
+++ b/packages/core/collections/notebooks.js
@@ -130,4 +130,16 @@ export default class Notebooks extends Collection {
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);
+ }
+ }
+ }
+ }
}
diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js
index 5292ee1d2..ba45eb1cb 100644
--- a/packages/core/database/backup.js
+++ b/packages/core/database/backup.js
@@ -114,6 +114,9 @@ export default class Backup {
"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 = [
{
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) {
@@ -174,3 +179,23 @@ function filterData(data) {
skippedKeys.forEach((key) => delete data[key]);
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;
+ }
+ }
+}