Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix in passing originEntity from frontend to backend, some refactorings
azivner208771216e81
diff --git a/src/entities/api_token.js b/src/entities/api_token.jsindex 90c5374b5a..e4051a4ddc 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.jsindex 4fbbdd396a..21bba9eefd 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.jsindex 9ff69e5ece..c8da8ede56 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 reorderingstatic get hashedProperties() { return ["branchId", "noteId", "parentNoteId", "isDeleted", "prefix"]; }diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.jsindex da6bb09deb..b798423e3c 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.jsindex d4137c8075..9e42ab1439 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.jsindex 9339c1fc72..3ce9288f51 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.jsindex 94df867027..cd271acfe6 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.jsindex b50fcd38a8..f4ff7a7ae1 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.jsindex 541226c21e..896e053f1f 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.jsindex 3f71e65450..7baca564c6 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.jsindex 0af3a1a85d..2b1ee22848 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 eventoriginEntityId: 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.jsindex 557d504e78..1f71bc3623 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.<string, Branch>} */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.jsindex 903bcbcf49..535fa324b7 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.jsindex f3f323f890..d723bca553 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];