mirror of
https://github.com/zadam/trilium.git
synced 2025-01-17 20:48:12 +08:00
changes for script execution (parameters etc.)
This commit is contained in:
parent
5981b9bc7b
commit
005480059a
5 changed files with 53 additions and 10 deletions
|
@ -29,12 +29,12 @@ const server = (function() {
|
|||
return await call('DELETE', url);
|
||||
}
|
||||
|
||||
async function exec(script) {
|
||||
async function exec(script, params = []) {
|
||||
if (typeof script === "function") {
|
||||
script = script.toString();
|
||||
}
|
||||
|
||||
return await post('script/exec', { script: script });
|
||||
return await post('script/exec', { script: script, params: params });
|
||||
}
|
||||
|
||||
let i = 1;
|
||||
|
|
|
@ -4,18 +4,14 @@ const express = require('express');
|
|||
const router = express.Router();
|
||||
const auth = require('../../services/auth');
|
||||
const wrap = require('express-promise-wrap').wrap;
|
||||
const log = require('../../services/log');
|
||||
const sql = require('../../services/sql');
|
||||
const notes = require('../../services/notes');
|
||||
const protected_session = require('../../services/protected_session');
|
||||
const attributes = require('../../services/attributes');
|
||||
const script = require('../../services/script');
|
||||
|
||||
router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => {
|
||||
log.info('Executing script: ' + req.body.script);
|
||||
|
||||
const ret = await eval("(" + req.body.script + ")()");
|
||||
|
||||
log.info('Execution result: ' + ret);
|
||||
const ret = await script.executeScript(req, req.body.script, req.body.params);
|
||||
|
||||
res.send(ret);
|
||||
}));
|
||||
|
|
|
@ -3,16 +3,30 @@
|
|||
const sql = require('./sql');
|
||||
const utils = require('./utils');
|
||||
const sync_table = require('./sync_table');
|
||||
const protected_session = require('./protected_session');
|
||||
|
||||
async function getNoteAttributeMap(noteId) {
|
||||
return await sql.getMap(`SELECT name, value FROM attributes WHERE note_id = ?`, [noteId]);
|
||||
}
|
||||
|
||||
async function getNoteIdWithAttribute(name, value) {
|
||||
return await sql.getFirstValue(`SELECT DISTINCT notes.note_id FROM notes JOIN attributes USING(note_id)
|
||||
return await sql.getFirstValue(`SELECT notes.note_id FROM notes JOIN attributes USING(note_id)
|
||||
WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
|
||||
}
|
||||
|
||||
async function getNoteWithAttribute(dataKey, name, value) {
|
||||
const note = await sql.getFirst(`SELECT notes.* FROM notes JOIN attributes USING(note_id)
|
||||
WHERE notes.is_deleted = 0 AND attributes.name = ? AND attributes.value = ?`, [name, value]);
|
||||
|
||||
if (!note) {
|
||||
return note;
|
||||
}
|
||||
|
||||
protected_session.decryptNote(dataKey, note);
|
||||
|
||||
return note;
|
||||
}
|
||||
|
||||
async function getNoteIdsWithAttribute(name) {
|
||||
return await sql.getFirstColumn(`SELECT DISTINCT notes.note_id FROM notes JOIN attributes USING(note_id)
|
||||
WHERE notes.is_deleted = 0 AND attributes.name = ?`, [name]);
|
||||
|
@ -37,6 +51,7 @@ async function createAttribute(noteId, name, value = null, sourceId = null) {
|
|||
module.exports = {
|
||||
getNoteAttributeMap,
|
||||
getNoteIdWithAttribute,
|
||||
getNoteWithAttribute,
|
||||
getNoteIdsWithAttribute,
|
||||
createAttribute
|
||||
};
|
|
@ -81,7 +81,11 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {
|
|||
return monthNoteId;
|
||||
}
|
||||
|
||||
async function getDateNoteId(dateTimeStr, rootNoteId) {
|
||||
async function getDateNoteId(dateTimeStr, rootNoteId = null) {
|
||||
if (!rootNoteId) {
|
||||
rootNoteId = await getRootNoteId();
|
||||
}
|
||||
|
||||
const dateStr = dateTimeStr.substr(0, 10);
|
||||
const dayNumber = dateTimeStr.substr(8, 2);
|
||||
|
||||
|
|
28
services/script.js
Normal file
28
services/script.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
const log = require('./log');
|
||||
const protected_session = require('./protected_session');
|
||||
|
||||
async function executeScript(dataKey, script, params) {
|
||||
log.info('Executing script: ' + script);
|
||||
|
||||
const ctx = {
|
||||
dataKey: protected_session.getDataKey(dataKey)
|
||||
};
|
||||
|
||||
params.unshift(ctx);
|
||||
|
||||
const paramsStr = getParams(params);
|
||||
|
||||
const ret = await eval(`(${script})(${paramsStr})`);
|
||||
|
||||
log.info('Execution result: ' + ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
function getParams(params) {
|
||||
return params.map(p => JSON.stringify(p)).join(",");
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
executeScript
|
||||
};
|
Loading…
Reference in a new issue