mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
fix in passing originEntity from frontend to backend, some refactorings
This commit is contained in:
parent
385d97a9b3
commit
208771216e
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
@ -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);
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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"]; }
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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];
|
||||
|
Loading…
x
Reference in New Issue
Block a user