fix: repair all notebook references on sync/backup

This commit is contained in:
thecodrr
2021-11-12 11:47:03 +05:00
parent e608eaaedf
commit 64535cc61c
4 changed files with 14 additions and 2 deletions

View File

@@ -183,6 +183,7 @@ class Merger {
); );
await this._db.notes.repairReferences(); await this._db.notes.repairReferences();
await this._db.notebooks.repairReferences();
return true; return true;
} }
} }

View File

@@ -133,4 +133,14 @@ export default class Notebooks extends Collection {
await this._db.trash.add(notebookData); await this._db.trash.add(notebookData);
} }
} }
async repairReferences() {
for (let notebook of this.all) {
const _notebook = this.notebook(notebook.id);
for (let topic of notebook.topics) {
const _topic = _notebook.topics.topic(topic.id);
await _topic.add(...topic.notes);
}
}
}
} }

View File

@@ -142,6 +142,7 @@ export default class Backup {
if (await this._migrator.migrate(collections, (id) => data[id], version)) { if (await this._migrator.migrate(collections, (id) => data[id], version)) {
await this._db.notes.repairReferences(); await this._db.notes.repairReferences();
await this._db.notebooks.repairReferences();
} }
} }

View File

@@ -26,7 +26,7 @@ export default class Topic {
const topic = qclone(this._topic); const topic = qclone(this._topic);
for (let noteId of noteIds) { for (let noteId of noteIds) {
let note = this._db.notes.note(noteId); let note = this._db.notes.note(noteId);
if (this.has(noteId) || !note || note.data.deleted) continue; if (!note || note.data.deleted) continue;
let array = note.notebooks || []; let array = note.notebooks || [];
const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId); const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId);
@@ -50,7 +50,7 @@ export default class Topic {
notebooks: array, notebooks: array,
}); });
topic.notes.push(noteId); if (!this.has(noteId)) topic.notes.push(noteId);
} }
return await this._save(topic); return await this._save(topic);
} }