Skip to content

Build your personal knowledge base with Trilium Notes

erasing rows of deleted entities

zadam6d7b9e0db385
diff --git a/src/services/sync.js b/src/services/sync.js
index 2e6bb06f7d..d6c616ca47 100644
--- a/src/services/sync.js
+++ b/src/services/sync.js
@@ -268,24 +268,12 @@ async function syncRequest(syncContext, method, requestPath, body) {
return await utils.timeLimit(request.exec(opts), timeout);
}
-const primaryKeys = {
- "notes": "noteId",
- "note_contents": "noteId",
- "branches": "branchId",
- "note_revisions": "noteRevisionId",
- "note_revision_contents": "noteRevisionId",
- "recent_notes": "noteId",
- "api_tokens": "apiTokenId",
- "options": "name",
- "attributes": "attributeId"
-};
-
function getEntityChangeRow(entityName, entityId) {
if (entityName === 'note_reordering') {
return sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [entityId]);
}
else {
- const primaryKey = primaryKeys[entityName];
+ const primaryKey = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;
if (!primaryKey) {
throw new Error("Unknown entity " + entityName);
diff --git a/src/services/sync_update.js b/src/services/sync_update.js
index 66271f0e7a..d10e7b32bc 100644
--- a/src/services/sync_update.js
+++ b/src/services/sync_update.js
@@ -1,11 +1,12 @@
const sql = require('./sql');
const entityChangesService = require('./entity_changes.js');
const eventService = require('./events');
+const entityConstructor = require('../entities/entity_constructor');
function updateEntity(entityChange, entity, sourceId) {
// can be undefined for options with isSynced=false
if (!entity) {
- return false;
+ return;
}
const updated = entityChange.entityName === 'note_reordering'
@@ -14,22 +15,32 @@ function updateEntity(entityChange, entity, sourceId) {
// currently making exception for protected notes and note revisions because here
// the title and content are not available decrypted as listeners would expect
- if (updated && !entity.isProtected) {
+ if (updated && !entity.isProtected && !entityChange.isErased) {
eventService.emit(eventService.ENTITY_SYNCED, {
entityName: entityChange.entityName,
entity
});
}
-
- return updated;
}
function updateNormalEntity(entityChange, entity, sourceId) {
- const {utcDateChanged, hash} = sql.getRow(`
- SELECT utcDateChanged, hash
+ const {utcDateChanged, hash, isErased} = sql.getRow(`
+ SELECT utcDateChanged, hash, isErased
FROM entity_changes
WHERE entityName = ? AND entityId = ?`, [entityChange.entityName, entityChange.entityId]);
+ if (!isErased && entityChange.isErased) {
+ sql.transactional(() => {
+ const primaryKey = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;
+
+ sql.execute(`DELETE FROM ${entityChange.entityName} WHERE ${primaryKey} = ?`, entityChange.entityId);
+
+ entityChangesService.addEntityChange(entityChange, sourceId);
+ });
+
+ return true;
+ }
+
if (utcDateChanged < entityChange.utcDateChanged
|| hash !== entityChange.hash // sync error, we should still update
) {