becca conversion WIP

This commit is contained in:
zadam 2021-05-02 12:02:32 +02:00
parent 1af10d48a2
commit 77eac8f764
4 changed files with 17 additions and 32 deletions

View File

@ -75,24 +75,6 @@ function BackendScriptApi(currentNote, apiParams) {
*/
this.getAttribute = becca.getAttribute;
/**
* Retrieves first entity from the SQL's result set.
*
* @method
* @param {string} SQL query
* @param {Array.<?>} array of params
* @returns {Entity|null}
*/
this.getEntity = repository.getEntity;
/**
* @method
* @param {string} SQL query
* @param {Array.<?>} array of params
* @returns {Entity[]}
*/
this.getEntities = repository.getEntities;
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

View File

@ -79,6 +79,21 @@ class Becca {
getOption(name) {
return this.options[name];
}
getEntityFromName(entityName, entityId) {
if (!entityName || !entityId) {
return null;
}
const camelCaseEntityName = entityName.toLowerCase().replace(/(_[a-z])/g,
group =>
group
.toUpperCase()
.replace('_', '')
);
return this[camelCaseEntityName][entityId];
}
}
const becca = new Becca();

View File

@ -6,16 +6,6 @@ const eventService = require('./events');
const cls = require('./cls');
const entityConstructor = require('../entities/entity_constructor');
function getEntityFromName(entityName, entityId) {
if (!entityName || !entityId) {
return null;
}
const constructor = entityConstructor.getEntityFromEntityName(entityName);
return getEntity(`SELECT * FROM ${constructor.entityName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]);
}
function getEntities(query, params = []) {
const rows = sql.getRows(query, params);
@ -151,9 +141,6 @@ function updateEntity(entity) {
}
module.exports = {
getEntityFromName,
getEntities,
getEntity,
getNote,
updateEntity
};

View File

@ -2,6 +2,7 @@ const ScriptContext = require('./script_context');
const repository = require('./repository');
const cls = require('./cls');
const log = require('./log');
const becca = require("./becca/becca.js");
async function executeNote(note, apiParams) {
if (!note.isJavaScript() || note.getScriptEnv() !== 'backend' || !note.isContentAvailable) {
@ -54,7 +55,7 @@ async function executeBundle(bundle, apiParams = {}) {
async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
const startNote = becca.getNote(startNoteId);
const currentNote = becca.getNote(currentNoteId);
const originEntity = repository.getEntityFromName(originEntityName, originEntityId);
const originEntity = becca.getEntityFromName(originEntityName, originEntityId);
currentNote.content = `return (${script}\r\n)(${getParams(params)})`;
currentNote.type = 'code';