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-31 11:44:46 +08:00
|
|
|
async function executeScript(dataKey, script, params) {
|
2018-01-27 12:40:48 +08:00
|
|
|
log.info('Executing script: ' + script);
|
|
|
|
|
2018-01-31 11:44:46 +08:00
|
|
|
const ctx = new ScriptContext(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
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getParams(params) {
|
2018-02-18 22:53:36 +08:00
|
|
|
return params.map(p => {
|
|
|
|
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
|
|
|
|
return p.substr(13);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return JSON.stringify(p);
|
|
|
|
}
|
|
|
|
}).join(",");
|
2018-01-27 12:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
executeScript
|
|
|
|
};
|