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
attributes are now updated only if their type and name does not change, in case of relation not even value must change. If they do, the old attribute is deleted and new is created instead
zadam88213c1bbded
diff --git a/src/entities/attribute.js b/src/entities/attribute.jsindex e37c59fbab..411ed704bb 100644--- a/src/entities/attribute.js+++ b/src/entities/attribute.js@@ -116,6 +116,20 @@ class Attribute extends Entity {delete pojo.isOwned;delete pojo.__note;}++ createClone(type, name, value) {+ return new Attribute({+ noteId: this.noteId,+ type: type,+ name: name,+ value: value,+ position: this.position,+ isInheritable: this.isInheritable,+ isDeleted: false,+ utcDateCreated: this.utcDateCreated,+ utcDateModified: this.utcDateModified+ });+ }}module.exports = Attribute;\ No newline at end of filediff --git a/src/entities/branch.js b/src/entities/branch.jsindex b0b1f32e41..a4022f88a1 100644--- a/src/entities/branch.js+++ b/src/entities/branch.js@@ -71,7 +71,7 @@ class Branch extends Entity {delete pojo.origParentNoteId;}- getClone(parentNoteId, notePosition) {+ createClone(parentNoteId, notePosition) {return new Branch({noteId: this.noteId,parentNoteId: parentNoteId,diff --git a/src/routes/api/attributes.js b/src/routes/api/attributes.jsindex ac751dab17..b705e1181a 100644--- a/src/routes/api/attributes.js+++ b/src/routes/api/attributes.js@@ -18,6 +18,27 @@ async function updateNoteAttribute(req) {let attribute;if (body.attributeId) {attribute = await repository.getAttribute(body.attributeId);++ if (attribute.noteId !== noteId) {+ return [400, `Attribute ${body.attributeId} is not owned by ${noteId}`];+ }++ if (body.type !== attribute.type+ || body.name !== attribute.name+ || (body.type === 'relation' && body.value !== attribute.value)) {++ if (body.type !== 'relation' || !!body.value.trim()) {+ const newAttribute = attribute.createClone(body.type, body.name, body.value);+ await newAttribute.save();+ }++ attribute.isDeleted = true;+ await attribute.save();++ return {+ attributeId: attribute.attributeId+ };+ }}else {if (body.type === 'relation' && !body.value.trim()) {@@ -30,10 +51,6 @@ async function updateNoteAttribute(req) {attribute.type = body.type;}- if (attribute.noteId !== noteId) {- return [400, `Attribute ${body.attributeId} is not owned by ${noteId}`];- }-if (body.value.trim()) {attribute.value = body.value;}@@ -78,6 +95,21 @@ async function updateNoteAttributes(req) {if (attributeEntity.noteId !== noteId) {return [400, `Attribute ${attributeEntity.noteId} is not owned by ${noteId}`];}++ if (attribute.type !== attributeEntity.type+ || attribute.name !== attributeEntity.name+ || (attribute.type === 'relation' && attribute.value !== attributeEntity.value)) {++ if (attribute.type !== 'relation' || !!attribute.value.trim()) {+ const newAttribute = attribute.createClone(attribute.type, attribute.name, attribute.value);+ await newAttribute.save();+ }++ attributeEntity.isDeleted = true;+ await attributeEntity.save();++ continue;+ }}else {// if it was "created" and then immediatelly deleted, we just don't create it at all