fix in passing originEntity from frontend to backend, some refactorings

This commit is contained in:
azivner 2018-08-16 23:00:04 +02:00
parent 385d97a9b3
commit 208771216e
14 changed files with 36 additions and 24 deletions

View File

@ -4,7 +4,7 @@ const Entity = require('./entity');
const dateUtils = require('../services/date_utils'); const dateUtils = require('../services/date_utils');
class ApiToken extends Entity { class ApiToken extends Entity {
static get tableName() { return "api_tokens"; } static get entityName() { return "api_tokens"; }
static get primaryKeyName() { return "apiTokenId"; } static get primaryKeyName() { return "apiTokenId"; }
static get hashedProperties() { return ["apiTokenId", "token", "dateCreated", "isDeleted"]; } static get hashedProperties() { return ["apiTokenId", "token", "dateCreated", "isDeleted"]; }

View File

@ -6,7 +6,7 @@ const dateUtils = require('../services/date_utils');
const sql = require('../services/sql'); const sql = require('../services/sql');
class Attribute extends Entity { class Attribute extends Entity {
static get tableName() { return "attributes"; } static get entityName() { return "attributes"; }
static get primaryKeyName() { return "attributeId"; } static get primaryKeyName() { return "attributeId"; }
static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "isDeleted", "dateCreated"]; } static get hashedProperties() { return ["attributeId", "noteId", "type", "name", "value", "isInheritable", "isDeleted", "dateCreated"]; }

View File

@ -6,7 +6,7 @@ const repository = require('../services/repository');
const sql = require('../services/sql'); const sql = require('../services/sql');
class Branch extends Entity { class Branch extends Entity {
static get tableName() { return "branches"; } static get entityName() { return "branches"; }
static get primaryKeyName() { return "branchId"; } static get primaryKeyName() { return "branchId"; }
// notePosition is not part of hash because it would produce a lot of updates in case of reordering // 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"]; } static get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "prefix"]; }

View File

@ -9,7 +9,7 @@ const ApiToken = require('../entities/api_token');
const Option = require('../entities/option'); const Option = require('../entities/option');
const repository = require('../services/repository'); const repository = require('../services/repository');
const TABLE_NAME_TO_ENTITY = { const ENTITY_NAME_TO_ENTITY = {
"attributes": Attribute, "attributes": Attribute,
"images": Image, "images": Image,
"note_images": NoteImage, "note_images": NoteImage,
@ -21,12 +21,12 @@ const TABLE_NAME_TO_ENTITY = {
"api_tokens": ApiToken "api_tokens": ApiToken
}; };
function getEntityFromTableName(tableName) { function getEntityFromEntityName(entityName) {
if (!(tableName in TABLE_NAME_TO_ENTITY)) { if (!(entityName in ENTITY_NAME_TO_ENTITY)) {
throw new Error(`Entity for table ${tableName} not found!`); throw new Error(`Entity for table ${entityName} not found!`);
} }
return TABLE_NAME_TO_ENTITY[tableName]; return ENTITY_NAME_TO_ENTITY[entityName];
} }
function createEntityFromRow(row) { function createEntityFromRow(row) {
@ -68,7 +68,7 @@ function createEntityFromRow(row) {
module.exports = { module.exports = {
createEntityFromRow, createEntityFromRow,
getEntityFromTableName getEntityFromEntityName
}; };
repository.setEntityConstructor(module.exports); repository.setEntityConstructor(module.exports);

View File

@ -4,7 +4,7 @@ const Entity = require('./entity');
const dateUtils = require('../services/date_utils'); const dateUtils = require('../services/date_utils');
class Image extends Entity { class Image extends Entity {
static get tableName() { return "images"; } static get entityName() { return "images"; }
static get primaryKeyName() { return "imageId"; } static get primaryKeyName() { return "imageId"; }
static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateCreated"]; } static get hashedProperties() { return ["imageId", "format", "checksum", "name", "isDeleted", "dateCreated"]; }

View File

@ -10,7 +10,7 @@ const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
class Note extends Entity { class Note extends Entity {
static get tableName() { return "notes"; } static get entityName() { return "notes"; }
static get primaryKeyName() { return "noteId"; } static get primaryKeyName() { return "noteId"; }
static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; } static get hashedProperties() { return ["noteId", "title", "content", "type", "isProtected", "isDeleted"]; }

View File

@ -5,7 +5,7 @@ const repository = require('../services/repository');
const dateUtils = require('../services/date_utils'); const dateUtils = require('../services/date_utils');
class NoteImage extends Entity { class NoteImage extends Entity {
static get tableName() { return "note_images"; } static get entityName() { return "note_images"; }
static get primaryKeyName() { return "noteImageId"; } static get primaryKeyName() { return "noteImageId"; }
static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateCreated"]; } static get hashedProperties() { return ["noteImageId", "noteId", "imageId", "isDeleted", "dateCreated"]; }

View File

@ -5,7 +5,7 @@ const protectedSessionService = require('../services/protected_session');
const repository = require('../services/repository'); const repository = require('../services/repository');
class NoteRevision extends Entity { class NoteRevision extends Entity {
static get tableName() { return "note_revisions"; } static get entityName() { return "note_revisions"; }
static get primaryKeyName() { return "noteRevisionId"; } static get primaryKeyName() { return "noteRevisionId"; }
static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "content", "isProtected", "dateModifiedFrom", "dateModifiedTo"]; } static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "content", "isProtected", "dateModifiedFrom", "dateModifiedTo"]; }

View File

@ -4,7 +4,7 @@ const Entity = require('./entity');
const dateUtils = require('../services/date_utils'); const dateUtils = require('../services/date_utils');
class Option extends Entity { class Option extends Entity {
static get tableName() { return "options"; } static get entityName() { return "options"; }
static get primaryKeyName() { return "name"; } static get primaryKeyName() { return "name"; }
static get hashedProperties() { return ["name", "value"]; } static get hashedProperties() { return ["name", "value"]; }

View File

@ -4,7 +4,7 @@ const Entity = require('./entity');
const dateUtils = require('../services/date_utils'); const dateUtils = require('../services/date_utils');
class RecentNote extends Entity { class RecentNote extends Entity {
static get tableName() { return "recent_notes"; } static get entityName() { return "recent_notes"; }
static get primaryKeyName() { return "branchId"; } static get primaryKeyName() { return "branchId"; }
static get hashedProperties() { return ["branchId", "notePath", "dateCreated", "isDeleted"]; } static get hashedProperties() { return ["branchId", "notePath", "dateCreated", "isDeleted"]; }

View File

@ -3,6 +3,7 @@ import server from './server.js';
import utils from './utils.js'; import utils from './utils.js';
import infoService from './info.js'; import infoService from './info.js';
import linkService from './link.js'; import linkService from './link.js';
import treeCache from './tree_cache.js';
function ScriptApi(startNote, currentNote, originEntity = null) { function ScriptApi(startNote, currentNote, originEntity = null) {
const $pluginButtons = $("#plugin-buttons"); const $pluginButtons = $("#plugin-buttons");
@ -67,7 +68,7 @@ function ScriptApi(startNote, currentNote, originEntity = null) {
params: prepareParams(params), params: prepareParams(params),
startNoteId: startNote.noteId, startNoteId: startNote.noteId,
currentNoteId: currentNote.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 originEntityId: originEntity ? originEntity.noteId : null
}); });
@ -78,6 +79,9 @@ function ScriptApi(startNote, currentNote, originEntity = null) {
startNote: startNote, startNote: startNote,
currentNote: currentNote, currentNote: currentNote,
originEntity: originEntity, 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, addButtonToToolbar,
activateNote, activateNote,
activateNewNote, activateNewNote,

View File

@ -6,7 +6,17 @@ import messagingService from "./messaging.js";
import server from "./server.js"; import server from "./server.js";
class TreeCache { class TreeCache {
constructor() {
this.init();
}
load(noteRows, branchRows, relations) { load(noteRows, branchRows, relations) {
this.init();
this.addResp(noteRows, branchRows, relations);
}
init() {
this.parents = {}; this.parents = {};
this.children = {}; this.children = {};
this.childParentToBranch = {}; this.childParentToBranch = {};
@ -16,8 +26,6 @@ class TreeCache {
/** @type {Object.<string, Branch>} */ /** @type {Object.<string, Branch>} */
this.branches = {}; this.branches = {};
this.addResp(noteRows, branchRows, relations);
} }
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); const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);
if (missingNoteIds.length > 0) { if (missingNoteIds.length > 0) {
@ -48,7 +56,7 @@ class TreeCache {
} }
return noteIds.map(noteId => { return noteIds.map(noteId => {
if (!this.notes[noteId]) { if (!this.notes[noteId] && !silentNotFoundError) {
messagingService.logError(`Can't find note ${noteId}`); messagingService.logError(`Can't find note ${noteId}`);
return null; return null;

View File

@ -17,7 +17,7 @@ const Option = require('../entities/option');
async function getHash(entityConstructor, whereBranch) { async function getHash(entityConstructor, whereBranch) {
// subselect is necessary to have correct ordering in GROUP_CONCAT // 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})`; + (whereBranch ? `WHERE ${whereBranch} ` : '') + `ORDER BY ${entityConstructor.primaryKeyName})`;
let contentToHash = await sql.getValue(query); let contentToHash = await sql.getValue(query);

View File

@ -15,9 +15,9 @@ async function getEntityFromName(entityName, entityId) {
return null; 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 = []) { async function getEntities(query, params = []) {
@ -57,7 +57,7 @@ async function getOption(name) {
} }
async function updateEntity(entity) { async function updateEntity(entity) {
const entityName = entity.constructor.tableName; const entityName = entity.constructor.entityName;
const primaryKeyName = entity.constructor.primaryKeyName; const primaryKeyName = entity.constructor.primaryKeyName;
const isNewEntity = !entity[primaryKeyName]; const isNewEntity = !entity[primaryKeyName];