Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
use .markAsDeleted() instead of setting .isDeleted manually
zadamc035627f0a0e
diff --git a/src/entities/note.js b/src/entities/note.jsindex f6a4951c59..f24f67bf5b 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -543,8 +543,7 @@ class Note extends Entity {for (const attribute of attributes) {if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) {- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();this.invalidateAttributeCache();}diff --git a/src/public/app/widgets/search_actions/execute_script.js b/src/public/app/widgets/search_actions/execute_script.jsindex 16ea0a9b91..e84d3b394a 100644--- a/src/public/app/widgets/search_actions/execute_script.js+++ b/src/public/app/widgets/search_actions/execute_script.js@@ -24,7 +24,7 @@ const TPL = `More complex example would be deleting all matched note's attributes:- <pre>for (const attr of note.getOwnedAttributes) { attr.isDeleted = true; attr.save(); }</pre>+ <pre>for (const attr of note.getOwnedAttributes) { attr.markAsDeleted(); }</pre></div></div>diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.jsindex 0744af6754..144174c510 100644--- a/src/routes/api/attributes.js+++ b/src/routes/api/attributes.js@@ -36,8 +36,7 @@ function updateNoteAttribute(req) {newAttribute.save();}- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();return {attributeId: newAttribute ? newAttribute.attributeId : null@@ -61,7 +60,7 @@ function updateNoteAttribute(req) {}else {// relations should never have empty target- attribute.isDeleted = true;+ attribute.markAsDeleted();}attribute.save();@@ -108,7 +107,7 @@ function deleteNoteAttribute(req) {return [400, `Attribute ${attributeId} is not owned by ${noteId}`];}- attribute.markAttributeAsDeleted();+ attribute.markAsDeleted();}}@@ -174,8 +173,7 @@ function updateNoteAttributes(req) {// all the remaining existing attributes are not defined anymore and should be deletedfor (const toDeleteAttr of existingAttrs) {if (!toDeleteAttr.isAutoLink()) {- toDeleteAttr.isDeleted = true;- toDeleteAttr.save();+ toDeleteAttr.markAsDeleted();}}}@@ -221,8 +219,7 @@ function deleteRelation(req) {let attribute = repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId, name, targetNoteId]);if (attribute) {- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();}}diff --git a/src/routes/api/branches.js b/src/routes/api/branches.jsindex e95eb3062c..8235722466 100644--- a/src/routes/api/branches.js+++ b/src/routes/api/branches.js@@ -44,7 +44,7 @@ function moveBranchToParent(req) {const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos);newBranch.save();- branchToMove.isDeleted = true;+ branchToMove.markAsDeleted();branchToMove.save();return { success: true };@@ -85,8 +85,7 @@ function moveBranchBeforeNote(req) {const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, beforeBranch.notePosition);newBranch.save();- branchToMove.isDeleted = true;- branchToMove.save();+ branchToMove.markAsDeleted();}return { success: true };@@ -121,8 +120,7 @@ function moveBranchAfterNote(req) {const newBranch = branchToMove.createClone(afterNote.parentNoteId, movedNotePosition);newBranch.save();- branchToMove.isDeleted = true;- branchToMove.save();+ branchToMove.markAsDeleted();}return { success: true };diff --git a/src/routes/api/search.js b/src/routes/api/search.jsindex 315985fee7..efe06f3f24 100644--- a/src/routes/api/search.js+++ b/src/routes/api/search.js@@ -59,22 +59,19 @@ async function searchFromNote(req) {const ACTION_HANDLERS = {deleteNote: (action, note) => {- note.isDeleted = true;- note.save();+ note.markAsDeleted();},deleteNoteRevisions: (action, note) => {noteRevisionService.eraseNoteRevisions(note.getNoteRevisions().map(rev => rev.noteRevisionId));},deleteLabel: (action, note) => {for (const label of note.getOwnedLabels(action.labelName)) {- label.isDeleted = true;- label.save();+ label.markAsDeleted();}},deleteRelation: (action, note) => {for (const relation of note.getOwnedRelations(action.relationName)) {- relation.isDeleted = true;- relation.save();+ relation.markAsDeleted();}},renameLabel: (action, note) => {diff --git a/src/services/becca/entities/attribute.js b/src/services/becca/entities/attribute.jsindex ba9be9117f..b581235a11 100644--- a/src/services/becca/entities/attribute.js+++ b/src/services/becca/entities/attribute.js@@ -120,8 +120,7 @@ class Attribute extends AbstractEntity {position: this.position,value: this.value,isInheritable: this.isInheritable,- utcDateModified: dateUtils.utcNowDateTime(),- isDeleted: false+ utcDateModified: dateUtils.utcNowDateTime()};}@@ -143,10 +142,6 @@ class Attribute extends AbstractEntity {this.isInheritable = false;}- if (!this.isDeleted) {- this.isDeleted = false;- }-super.beforeSaving();}@@ -158,13 +153,12 @@ class Attribute extends AbstractEntity {value: value,position: this.position,isInheritable: isInheritable,- isDeleted: false,utcDateModified: this.utcDateModified});}- markAttributeAsDeleted() {- sql.execute("UPDATE attributes SET isDeleted = 1 WHERE attributeId = ?", [this.attributeId]);+ markAsDeleted(deleteId = null) {+ sql.execute("UPDATE attributes SET isDeleted = 1, deleteId = ? WHERE attributeId = ?", [deleteId, this.attributeId]);// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)}diff --git a/src/services/becca/entities/branch.js b/src/services/becca/entities/branch.jsindex 0bfe2376d2..5b1c9b3ea3 100644--- a/src/services/becca/entities/branch.js+++ b/src/services/becca/entities/branch.js@@ -99,6 +99,7 @@ class Branch extends AbstractEntity {}beforeSaving() {+ // TODO can be refactored into beccaif (this.notePosition === undefined || this.notePosition === null) {const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [this.parentNoteId]);this.notePosition = maxNotePos === null ? 0 : maxNotePos + 10;@@ -110,6 +111,13 @@ class Branch extends AbstractEntity {super.beforeSaving();}++ markAsDeleted(deleteId = null) {+ sql.execute("UPDATE branches SET isDeleted = 1, deleteId = ? WHERE branchId = ?",+ [deleteId, this.branchId]);++ // FIXME: this needs to be published into entity_changes (for sync and becca cleanup)+ }}module.exports = Branch;diff --git a/src/services/becca/entities/note.js b/src/services/becca/entities/note.jsindex 81ddd201a1..565e5c7591 100644--- a/src/services/becca/entities/note.js+++ b/src/services/becca/entities/note.js@@ -864,8 +864,8 @@ class Note extends AbstractEntity {this.utcDateModified = dateUtils.utcNowDateTime();}- markAsDeleted() {- sql.execute("UPDATE notes SET isDeleted = 1 WHERE noteId = ?", [this.noteId]);+ markAsDeleted(deleteId = null) {+ sql.execute("UPDATE notes SET isDeleted = 1, deleteId = ? WHERE noteId = ?", [deleteId, this.noteId]);// FIXME: this needs to be published into entity_changes (for sync and becca cleanup)}diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.jsindex c55c14c575..3b60f8d9b5 100644--- a/src/services/consistency_checks.js+++ b/src/services/consistency_checks.js@@ -103,8 +103,7 @@ class ConsistencyChecks {({branchId, noteId}) => {if (this.autoFix) {const branch = becca.getBranch(branchId);- branch.isDeleted = true;- branch.save();+ branch.markAsDeleted();logFix(`Branch ${branchId} has been deleted since it references missing note ${noteId}`);} else {@@ -140,8 +139,7 @@ class ConsistencyChecks {({attributeId, noteId}) => {if (this.autoFix) {const attribute = becca.getAttribute(attributeId);- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();logFix(`Attribute ${attributeId} has been deleted since it references missing source note ${noteId}`);} else {@@ -159,8 +157,7 @@ class ConsistencyChecks {({attributeId, noteId}) => {if (this.autoFix) {const attribute = becca.getAttribute(attributeId);- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();logFix(`Relation ${attributeId} has been deleted since it references missing note ${noteId}`)} else {@@ -185,8 +182,7 @@ class ConsistencyChecks {({branchId, noteId}) => {if (this.autoFix) {const branch = becca.getBranch(branchId);- branch.isDeleted = true;- branch.save();+ branch.markAsDeleted();logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`);} else {@@ -204,8 +200,7 @@ class ConsistencyChecks {`, ({branchId, parentNoteId}) => {if (this.autoFix) {const branch = becca.getBranch(branchId);- branch.isDeleted = true;- branch.save();+ branch.markAsDeleted();logFix(`Branch ${branchId} has been deleted since associated parent note ${parentNoteId} is deleted.`);} else {@@ -258,8 +253,7 @@ class ConsistencyChecks {// delete all but the first branchfor (const branch of branches.slice(1)) {- branch.isDeleted = true;- branch.save();+ branch.markAsDeleted();logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`);}@@ -388,8 +382,7 @@ class ConsistencyChecks {({attributeId}) => {if (this.autoFix) {const relation = becca.getAttribute(attributeId);- relation.isDeleted = true;- relation.save();+ relation.markAsDeleted();logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`);} else {@@ -426,8 +419,7 @@ class ConsistencyChecks {({attributeId, noteId}) => {if (this.autoFix) {const attribute = becca.getAttribute(attributeId);- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`);} else {@@ -446,8 +438,7 @@ class ConsistencyChecks {({attributeId, targetNoteId}) => {if (this.autoFix) {const attribute = becca.getAttribute(attributeId);- attribute.isDeleted = true;- attribute.save();+ attribute.markAsDeleted();logFix(`Removed attribute ${attributeId} because target note ${targetNoteId} is also deleted.`);} else {diff --git a/src/services/handlers.js b/src/services/handlers.jsindex 829d89d8d2..e41b1b2a54 100644--- a/src/services/handlers.js+++ b/src/services/handlers.js@@ -154,8 +154,7 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>note.invalidateAttributeCache();targetNote.invalidateAttributeCache();- relation.isDeleted = true;- relation.save();+ relation.markAsDeleted();}}});diff --git a/src/services/notes.js b/src/services/notes.jsindex a6ee511a9f..a9e9789cbb 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -532,9 +532,7 @@ function deleteBranch(branch, deleteId, taskContext) {throw new Error("Can't delete root branch/note");}- branch.isDeleted = true;- branch.deleteId = deleteId;- branch.save();+ branch.markAsDeleted(deleteId);const note = branch.getNote();const notDeletedBranches = note.getBranches();@@ -546,22 +544,16 @@ function deleteBranch(branch, deleteId, taskContext) {// first delete children and then parent - this will show up better in recent changes- note.isDeleted = true;- note.deleteId = deleteId;- note.save();+ note.markAsDeleted(deleteId);log.info("Deleting note " + note.noteId);for (const attribute of note.getOwnedAttributes()) {- attribute.isDeleted = true;- attribute.deleteId = deleteId;- attribute.save();+ attribute.markAsDeleted(deleteId);}for (const relation of note.getTargetRelations()) {- relation.isDeleted = true;- relation.deleteId = deleteId;- relation.save();+ relation.markAsDeleted(deleteId);}return true;diff --git a/src/services/tree.js b/src/services/tree.jsindex a0cecf7f75..a5f1be02b6 100644--- a/src/services/tree.js+++ b/src/services/tree.js@@ -196,14 +196,13 @@ function setNoteToParent(noteId, prefix, parentNoteId) {if (branch) {if (!parentNoteId) {- branch.isDeleted = true;+ branch.markAsDeleted();}else {branch.parentNoteId = parentNoteId;branch.prefix = prefix;+ branch.save();}-- branch.save();}else if (parentNoteId) {const note = becca.getNote(noteId);