From efc678d3cdba9a653839ece447e2b6958a85ffcb Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 17 Mar 2023 14:40:13 +0500 Subject: [PATCH] core: `removeFromAllNotebooks` should unlink notebook relations too --- packages/core/collections/notes.js | 3 ++- packages/core/collections/relations.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js index 66f649db9..b067456d2 100644 --- a/packages/core/collections/notes.js +++ b/packages/core/collections/notes.js @@ -374,7 +374,7 @@ export default class Notes extends Collection { async removeFromAllNotebooks(...noteIds) { for (const noteId of noteIds) { const note = this.note(noteId); - if (!note || note.deleted || !note.notebooks) { + if (!note || note.deleted) { continue; } @@ -382,6 +382,7 @@ export default class Notes extends Collection { id: noteId, notebooks: [] }); + await this._db.relations.unlinkAll(note.data, "notebook"); } this.topicReferences.rebuild(); } diff --git a/packages/core/collections/relations.js b/packages/core/collections/relations.js index e12f91c21..9dec64252 100644 --- a/packages/core/collections/relations.js +++ b/packages/core/collections/relations.js @@ -142,6 +142,19 @@ export default class Relations extends Collection { await this.remove(relation.id); } + /** + * + * @param {ItemReference} from + * @param {string} type + */ + async unlinkAll(to, type) { + for (const relation of this.all.filter( + (a) => compareItemReference(a.to, to) && a.from.type === type + )) { + await this.remove(relation.id); + } + } + /** * @param {Relation[]} relations * @param {"from" | "to"} resolveType