2018-01-27 12:40:48 +08:00
|
|
|
const log = require('./log');
|
2018-01-28 06:18:19 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const ScriptContext = require('./script_context');
|
2018-01-27 12:40:48 +08:00
|
|
|
|
2018-01-28 06:18:19 +08:00
|
|
|
async function executeScript(noteId, dataKey, script, params) {
|
2018-01-27 12:40:48 +08:00
|
|
|
log.info('Executing script: ' + script);
|
|
|
|
|
2018-01-28 06:18:19 +08:00
|
|
|
const ctx = new ScriptContext(noteId, dataKey);
|
2018-01-27 12:40:48 +08:00
|
|
|
|
|
|
|
const paramsStr = getParams(params);
|
|
|
|
|
2018-01-28 06:18:19 +08:00
|
|
|
let ret;
|
|
|
|
|
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
ret = await (function() { return eval(`(${script})(${paramsStr})`); }.call(ctx));
|
|
|
|
});
|
2018-01-27 12:40:48 +08:00
|
|
|
|
|
|
|
log.info('Execution result: ' + ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParams(params) {
|
|
|
|
return params.map(p => JSON.stringify(p)).join(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
executeScript
|
|
|
|
};
|