diff --git a/src/entities/attribute.js b/src/entities/attribute.js index f8a8dbd42..c478ffaf8 100644 --- a/src/entities/attribute.js +++ b/src/entities/attribute.js @@ -8,7 +8,7 @@ const sql = require('../services/sql'); class Attribute extends Entity { static get tableName() { return "attributes"; } static get primaryKeyName() { return "attributeId"; } - static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "dateModified", "dateCreated"]; } + static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "dateCreated"]; } constructor(row) { super(row); @@ -66,9 +66,11 @@ class Attribute extends Entity { this.dateCreated = dateUtils.nowDate(); } - this.dateModified = dateUtils.nowDate(); - super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/entities/branch.js b/src/entities/branch.js index 5e04eeb7e..9ff69e5ec 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -9,7 +9,7 @@ class Branch extends Entity { static get tableName() { return "branches"; } static get primaryKeyName() { return "branchId"; } // notePosition is not part of hash because it would produce a lot of updates in case of reordering - static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "dateModified", "isDeleted", "prefix"]; } + static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "prefix"]; } async getNote() { return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); @@ -29,9 +29,11 @@ class Branch extends Entity { this.dateCreated = dateUtils.nowDate(); } - this.dateModified = dateUtils.nowDate(); - super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/entities/entity.js b/src/entities/entity.js index 18200118d..74da23cc6 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -18,13 +18,21 @@ class Entity { this[this.constructor.primaryKeyName] = utils.newEntityId(); } + const origHash = this.hash; + + this.hash = this.generateHash(); + + this.isChanged = origHash !== this.hash; + } + + generateHash() { let contentToHash = ""; for (const propertyName of this.constructor.hashedProperties) { contentToHash += "|" + this[propertyName]; } - this["hash"] = utils.hash(contentToHash).substr(0, 10); + return utils.hash(contentToHash).substr(0, 10); } async save() { diff --git a/src/entities/image.js b/src/entities/image.js index 8470bb976..d4137c807 100644 --- a/src/entities/image.js +++ b/src/entities/image.js @@ -6,7 +6,7 @@ const dateUtils = require('../services/date_utils'); class Image extends Entity { static get tableName() { return "images"; } static get primaryKeyName() { return "imageId"; } - static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateModified", "dateCreated"]; } + static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateCreated"]; } beforeSaving() { if (!this.isDeleted) { @@ -17,9 +17,11 @@ class Image extends Entity { this.dateCreated = dateUtils.nowDate(); } - this.dateModified = dateUtils.nowDate(); - super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/entities/note.js b/src/entities/note.js index bb08326f9..3a27c6656 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -8,7 +8,7 @@ const dateUtils = require('../services/date_utils'); class Note extends Entity { static get tableName() { return "notes"; } static get primaryKeyName() { return "noteId"; } - static get hashedProperties() { return ["noteId", "title", "content", "type", "dateModified", "isProtected", "isDeleted"]; } + static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; } constructor(row) { super(row); @@ -186,8 +186,6 @@ class Note extends Entity { } beforeSaving() { - super.beforeSaving(); - if (this.isJson() && this.jsonContent) { this.content = JSON.stringify(this.jsonContent, null, '\t'); } @@ -204,7 +202,11 @@ class Note extends Entity { this.dateCreated = dateUtils.nowDate(); } - this.dateModified = dateUtils.nowDate(); + super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/entities/note_image.js b/src/entities/note_image.js index 0dac7aa27..94df86702 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -7,7 +7,7 @@ const dateUtils = require('../services/date_utils'); class NoteImage extends Entity { static get tableName() { return "note_images"; } static get primaryKeyName() { return "noteImageId"; } - static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateModified", "dateCreated"]; } + static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateCreated"]; } async getNote() { return await repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]); @@ -26,9 +26,11 @@ class NoteImage extends Entity { this.dateCreated = dateUtils.nowDate(); } - this.dateModified = dateUtils.nowDate(); - super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/entities/option.js b/src/entities/option.js index 7176bfbd9..541226c21 100644 --- a/src/entities/option.js +++ b/src/entities/option.js @@ -15,9 +15,11 @@ class Option extends Entity { } beforeSaving() { - this.dateModified = dateUtils.nowDate(); - super.beforeSaving(); + + if (this.isChanged) { + this.dateModified = dateUtils.nowDate(); + } } } diff --git a/src/services/repository.js b/src/services/repository.js index ade2aad8b..1353b4af8 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -60,6 +60,7 @@ async function updateEntity(entity) { delete clone.jsonContent; delete clone.isOwned; + delete clone.isChanged; for (const key in clone) { // !isBuffer is for images and attachments @@ -73,7 +74,7 @@ async function updateEntity(entity) { const primaryKey = entity[entity.constructor.primaryKeyName]; - if (entity.constructor.tableName !== 'options' || entity.isSynced) { + if (entity.isChanged && (entity.constructor.tableName !== 'options' || entity.isSynced)) { await syncTableService.addEntitySync(entity.constructor.tableName, primaryKey); } });