From 208771216e818aab09b834c66a8b2a355b478db2 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 16 Aug 2018 23:00:04 +0200 Subject: [PATCH] fix in passing originEntity from frontend to backend, some refactorings --- src/entities/api_token.js | 2 +- src/entities/attribute.js | 2 +- src/entities/branch.js | 2 +- src/entities/entity_constructor.js | 12 ++++++------ src/entities/image.js | 2 +- src/entities/note.js | 2 +- src/entities/note_image.js | 2 +- src/entities/note_revision.js | 2 +- src/entities/option.js | 2 +- src/entities/recent_note.js | 2 +- src/public/javascripts/services/script_api.js | 6 +++++- src/public/javascripts/services/tree_cache.js | 16 ++++++++++++---- src/services/content_hash.js | 2 +- src/services/repository.js | 6 +++--- 14 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/entities/api_token.js b/src/entities/api_token.js index 90c5374b5..e4051a4dd 100644 --- a/src/entities/api_token.js +++ b/src/entities/api_token.js @@ -4,7 +4,7 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); class ApiToken extends Entity { - static get tableName() { return "api_tokens"; } + static get entityName() { return "api_tokens"; } static get primaryKeyName() { return "apiTokenId"; } static get hashedProperties() { return ["apiTokenId", "token", "dateCreated", "isDeleted"]; } diff --git a/src/entities/attribute.js b/src/entities/attribute.js index 4fbbdd396..21bba9eef 100644 --- a/src/entities/attribute.js +++ b/src/entities/attribute.js @@ -6,7 +6,7 @@ const dateUtils = require('../services/date_utils'); const sql = require('../services/sql'); class Attribute extends Entity { - static get tableName() { return "attributes"; } + static get entityName() { return "attributes"; } static get primaryKeyName() { return "attributeId"; } static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "isDeleted", "dateCreated"]; } diff --git a/src/entities/branch.js b/src/entities/branch.js index 9ff69e5ec..c8da8ede5 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -6,7 +6,7 @@ const repository = require('../services/repository'); const sql = require('../services/sql'); class Branch extends Entity { - static get tableName() { return "branches"; } + static get entityName() { 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", "isDeleted", "prefix"]; } diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js index da6bb09de..b798423e3 100644 --- a/src/entities/entity_constructor.js +++ b/src/entities/entity_constructor.js @@ -9,7 +9,7 @@ const ApiToken = require('../entities/api_token'); const Option = require('../entities/option'); const repository = require('../services/repository'); -const TABLE_NAME_TO_ENTITY = { +const ENTITY_NAME_TO_ENTITY = { "attributes": Attribute, "images": Image, "note_images": NoteImage, @@ -21,12 +21,12 @@ const TABLE_NAME_TO_ENTITY = { "api_tokens": ApiToken }; -function getEntityFromTableName(tableName) { - if (!(tableName in TABLE_NAME_TO_ENTITY)) { - throw new Error(`Entity for table ${tableName} not found!`); +function getEntityFromEntityName(entityName) { + if (!(entityName in ENTITY_NAME_TO_ENTITY)) { + throw new Error(`Entity for table ${entityName} not found!`); } - return TABLE_NAME_TO_ENTITY[tableName]; + return ENTITY_NAME_TO_ENTITY[entityName]; } function createEntityFromRow(row) { @@ -68,7 +68,7 @@ function createEntityFromRow(row) { module.exports = { createEntityFromRow, - getEntityFromTableName + getEntityFromEntityName }; repository.setEntityConstructor(module.exports); diff --git a/src/entities/image.js b/src/entities/image.js index d4137c807..9e42ab143 100644 --- a/src/entities/image.js +++ b/src/entities/image.js @@ -4,7 +4,7 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); class Image extends Entity { - static get tableName() { return "images"; } + static get entityName() { return "images"; } static get primaryKeyName() { return "imageId"; } static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateCreated"]; } diff --git a/src/entities/note.js b/src/entities/note.js index 9339c1fc7..3ce9288f5 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -10,7 +10,7 @@ const LABEL = 'label'; const RELATION = 'relation'; class Note extends Entity { - static get tableName() { return "notes"; } + static get entityName() { return "notes"; } static get primaryKeyName() { return "noteId"; } static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; } diff --git a/src/entities/note_image.js b/src/entities/note_image.js index 94df86702..cd271acfe 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -5,7 +5,7 @@ const repository = require('../services/repository'); const dateUtils = require('../services/date_utils'); class NoteImage extends Entity { - static get tableName() { return "note_images"; } + static get entityName() { return "note_images"; } static get primaryKeyName() { return "noteImageId"; } static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateCreated"]; } diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js index b50fcd38a..f4ff7a7ae 100644 --- a/src/entities/note_revision.js +++ b/src/entities/note_revision.js @@ -5,7 +5,7 @@ const protectedSessionService = require('../services/protected_session'); const repository = require('../services/repository'); class NoteRevision extends Entity { - static get tableName() { return "note_revisions"; } + static get entityName() { return "note_revisions"; } static get primaryKeyName() { return "noteRevisionId"; } static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "content", "isProtected", "dateModifiedFrom", "dateModifiedTo"]; } diff --git a/src/entities/option.js b/src/entities/option.js index 541226c21..896e053f1 100644 --- a/src/entities/option.js +++ b/src/entities/option.js @@ -4,7 +4,7 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); class Option extends Entity { - static get tableName() { return "options"; } + static get entityName() { return "options"; } static get primaryKeyName() { return "name"; } static get hashedProperties() { return ["name", "value"]; } diff --git a/src/entities/recent_note.js b/src/entities/recent_note.js index 3f71e6545..7baca564c 100644 --- a/src/entities/recent_note.js +++ b/src/entities/recent_note.js @@ -4,7 +4,7 @@ const Entity = require('./entity'); const dateUtils = require('../services/date_utils'); class RecentNote extends Entity { - static get tableName() { return "recent_notes"; } + static get entityName() { return "recent_notes"; } static get primaryKeyName() { return "branchId"; } static get hashedProperties() { return ["branchId", "notePath", "dateCreated", "isDeleted"]; } diff --git a/src/public/javascripts/services/script_api.js b/src/public/javascripts/services/script_api.js index 0af3a1a85..2b1ee2284 100644 --- a/src/public/javascripts/services/script_api.js +++ b/src/public/javascripts/services/script_api.js @@ -3,6 +3,7 @@ import server from './server.js'; import utils from './utils.js'; import infoService from './info.js'; import linkService from './link.js'; +import treeCache from './tree_cache.js'; function ScriptApi(startNote, currentNote, originEntity = null) { const $pluginButtons = $("#plugin-buttons"); @@ -67,7 +68,7 @@ function ScriptApi(startNote, currentNote, originEntity = null) { params: prepareParams(params), startNoteId: startNote.noteId, currentNoteId: currentNote.noteId, - originEntityName: originEntity ? originEntity.constructor.tableName : null, + originEntityName: "notes", // currently there's no other entity on frontend which can trigger event originEntityId: originEntity ? originEntity.noteId : null }); @@ -78,6 +79,9 @@ function ScriptApi(startNote, currentNote, originEntity = null) { startNote: startNote, currentNote: currentNote, originEntity: originEntity, + // needs to have the longform, can't be shortened! + // used also to load many rows to cache before further code starts using them + getNotes: async (noteIds, silentNotFoundError = false) => await treeCache.getNotes(noteIds, silentNotFoundError), addButtonToToolbar, activateNote, activateNewNote, diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.js index 557d504e7..1f71bc362 100644 --- a/src/public/javascripts/services/tree_cache.js +++ b/src/public/javascripts/services/tree_cache.js @@ -6,7 +6,17 @@ import messagingService from "./messaging.js"; import server from "./server.js"; class TreeCache { + constructor() { + this.init(); + } + load(noteRows, branchRows, relations) { + this.init(); + + this.addResp(noteRows, branchRows, relations); + } + + init() { this.parents = {}; this.children = {}; this.childParentToBranch = {}; @@ -16,8 +26,6 @@ class TreeCache { /** @type {Object.} */ this.branches = {}; - - this.addResp(noteRows, branchRows, relations); } addResp(noteRows, branchRows, relations) { @@ -38,7 +46,7 @@ class TreeCache { } } - async getNotes(noteIds) { + async getNotes(noteIds, silentNotFoundError = false) { const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined); if (missingNoteIds.length > 0) { @@ -48,7 +56,7 @@ class TreeCache { } return noteIds.map(noteId => { - if (!this.notes[noteId]) { + if (!this.notes[noteId] && !silentNotFoundError) { messagingService.logError(`Can't find note ${noteId}`); return null; diff --git a/src/services/content_hash.js b/src/services/content_hash.js index 903bcbcf4..535fa324b 100644 --- a/src/services/content_hash.js +++ b/src/services/content_hash.js @@ -17,7 +17,7 @@ const Option = require('../entities/option'); async function getHash(entityConstructor, whereBranch) { // subselect is necessary to have correct ordering in GROUP_CONCAT - const query = `SELECT GROUP_CONCAT(hash) FROM (SELECT hash FROM ${entityConstructor.tableName} ` + const query = `SELECT GROUP_CONCAT(hash) FROM (SELECT hash FROM ${entityConstructor.entityName} ` + (whereBranch ? `WHERE ${whereBranch} ` : '') + `ORDER BY ${entityConstructor.primaryKeyName})`; let contentToHash = await sql.getValue(query); diff --git a/src/services/repository.js b/src/services/repository.js index f3f323f89..d723bca55 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -15,9 +15,9 @@ async function getEntityFromName(entityName, entityId) { return null; } - const constructor = entityConstructor.getEntityFromTableName(entityName); + const constructor = entityConstructor.getEntityFromEntityName(entityName); - return await getEntity(`SELECT * FROM ${constructor.tableName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]); + return await getEntity(`SELECT * FROM ${constructor.entityName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]); } async function getEntities(query, params = []) { @@ -57,7 +57,7 @@ async function getOption(name) { } async function updateEntity(entity) { - const entityName = entity.constructor.tableName; + const entityName = entity.constructor.entityName; const primaryKeyName = entity.constructor.primaryKeyName; const isNewEntity = !entity[primaryKeyName];