support for updating entities etc.

This commit is contained in:
azivner 2018-01-29 23:35:36 -05:00
parent 6fa6891496
commit 35c7b54176
6 changed files with 36 additions and 1 deletions

View File

@ -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]);
}

View File

@ -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;

View File

@ -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;

View File

@ -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]);
}

View File

@ -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]);
}

View File

@ -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;