Skip to content

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

core: make resolve function sync

ammarahm-ed7ac8b478ed3f
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 3497a6c14..1518e3123 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -106,6 +106,7 @@ class Database {
case "notes": {
await this.monographs.init();
await this.trash.init();
+ await this.relations.cleanup();
break;
}
}
diff --git a/packages/core/collections/relations.js b/packages/core/collections/relations.js
index 488e25e14..8f1c1ac85 100644
--- a/packages/core/collections/relations.js
+++ b/packages/core/collections/relations.js
@@ -120,8 +120,10 @@ export default class Relations extends Collection {
* @param {Relation[]} relations
* @param {"from" | "to"} resolveType
* @private
+ *
+ * @returns {Relation[]}
*/
- async resolve(relations, resolveType) {
+ resolve(relations, resolveType) {
const items = [];
for (const relation of relations) {
const reference = resolveType === "from" ? relation.from : relation.to;
@@ -136,10 +138,30 @@ export default class Relations extends Collection {
break;
}
if (item) items.push(item);
- else await this.remove(relation.id);
}
return items;
}
+
+ async cleanup() {
+ const relations = this._collection.getItems();
+ for (const relation of relations) {
+ const references = [relation.to, relation.from];
+ for (let reference of references) {
+ const exists = false;
+ switch (reference.type) {
+ case "reminder":
+ exists = this._db.reminders.exists(reference.id);
+ break;
+ case "note":
+ exists =
+ this._db.notes.exists(reference.id) ||
+ this._db.trash.exists(reference.id);
+ break;
+ }
+ if (!exists) await this.remove(relation.id);
+ }
+ }
+ }
}
/**
diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.js
index f1518cdcf..603186fdd 100644
--- a/packages/core/collections/trash.js
+++ b/packages/core/collections/trash.js
@@ -123,6 +123,14 @@ export default class Trash {
return !contentId || this._db.content.exists(contentId);
} else return true;
}
+
+ /**
+ *
+ * @param {string} id
+ */
+ exists(id) {
+ return this.all.findIndex((item) => item.id === id) > -1;
+ }
}
function collectionNameFromItem(item) {