From 35c7b5417629566e2c079d22bea27672d2974d3c Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 29 Jan 2018 23:35:36 -0500 Subject: [PATCH] support for updating entities etc. --- src/entities/attribute.js | 2 ++ src/entities/entity.js | 2 +- src/entities/note.js | 15 +++++++++++++++ src/entities/note_revision.js | 2 ++ src/entities/note_tree.js | 2 ++ src/services/repository.js | 14 ++++++++++++++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/entities/attribute.js b/src/entities/attribute.js index 0504a5696..022b344a7 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 71e6dc1ea..2c44cc945 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 6409630e1..661b9e3e7 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 0e8c71f32..09a888d48 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 94dee1834..7bdce0b4c 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 b13a8c54b..829ea7cdd 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