Skip to content

Build your personal knowledge base with Trilium Notes

support for updating entities etc.

azivner35c7b5417629
diff --git a/src/entities/attribute.js b/src/entities/attribute.js
index 0504a56964..022b344a7a 100644
--- a/src/entities/attribute.js
+++ b/src/entities/attribute.js
@@ -3,6 +3,8 @@
const Entity = require('./entity');
class Attribute extends Entity {
+ static get tableName() { return "attributes"; }
+
async getNote() {
return this.repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
}
diff --git a/src/entities/entity.js b/src/entities/entity.js
index 71e6dc1ea9..2c44cc945a 100644
--- a/src/entities/entity.js
+++ b/src/entities/entity.js
@@ -4,7 +4,7 @@ const utils = require('../services/utils');
class Entity {
constructor(repository, row) {
- utils.assertArguments(repository, row)
+ utils.assertArguments(repository, row);
this.repository = repository;
diff --git a/src/entities/note.js b/src/entities/note.js
index 6409630e1a..661b9e3e72 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -1,11 +1,18 @@
"use strict";
const Entity = require('./entity');
+const protected_session = require('../services/protected_session');
class Note extends Entity {
+ static get tableName() { return "notes"; }
+
constructor(repository, row) {
super(repository, row);
+ if (this.isProtected) {
+ protected_session.decryptNote(this.dataKey, this);
+ }
+
if (this.isJson()) {
this.jsonContent = JSON.parse(this.content);
}
@@ -30,6 +37,14 @@ class Note extends Entity {
async getTrees() {
return this.repository.getEntities("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
}
+
+ beforeSaving() {
+ this.content = JSON.stringify(this.jsonContent, null, 4);
+
+ if (this.isProtected) {
+ protected_session.encryptNote(this.dataKey, this);
+ }
+ }
}
module.exports = Note;
\ No newline at end of file
diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js
index 0e8c71f32b..09a888d489 100644
--- a/src/entities/note_revision.js
+++ b/src/entities/note_revision.js
@@ -3,6 +3,8 @@
const Entity = require('./entity');
class NoteRevision extends Entity {
+ static get tableName() { return "note_revisions"; }
+
async getNote() {
return this.repository.getEntity("SELECT * FROM notes WHERE noteId = ?", [this.noteId]);
}
diff --git a/src/entities/note_tree.js b/src/entities/note_tree.js
index 94dee18340..7bdce0b4cb 100644
--- a/src/entities/note_tree.js
+++ b/src/entities/note_tree.js
@@ -3,6 +3,8 @@
const Entity = require('./entity');
class NoteTree extends Entity {
+ static get tableName() { return "note_tree"; }
+
async getNote() {
return this.repository.getEntity("SELECT * FROM note_tree WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
}
diff --git a/src/services/repository.js b/src/services/repository.js
index b13a8c54bd..829ea7cdd3 100644
--- a/src/services/repository.js
+++ b/src/services/repository.js
@@ -57,6 +57,20 @@ class Repository {
return entity;
}
+
+ async updateEntity(entity) {
+ if (entity.beforeSaving) {
+ entity.beforeSaving();
+ }
+
+ const clone = {...entity};
+
+ delete clone.dataKey;
+ delete clone.jsonContent;
+ delete clone.repository;
+
+ await sql.replace(entity.constructor.tableName, entity);
+ }
}
module.exports = Repository;
\ No newline at end of file