你可以“存储”吗?在DB中的javascript,然后在以后执行它? [英] Can you "store" javascript in a DB, and then execute it later time?

查看:52
本文介绍了你可以“存储”吗?在DB中的javascript,然后在以后执行它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请关注安全性或窗外的任何问题,因为它是一个内部应用程序)

(please throw any concern for security or anything out the window, as its a internal application)

基本上我想做的是存储一个javascript函数db:

basically What i want to do is store a javascript function in a db:

 function x(name) {
   return name + 1;
 }

然后在以后获取数据库行

and then at a later time fetch the DB row

(这里是伪代码)

 x = db.get('function').where('id').equals(1);

 console.log(x('bob'));
 //should print "bob +1 "

所以这是我想象的场景:

so here is the scenario i am envisioning:

基本上,我得到一个JSON对象,并且根据某些标准,我想对该json进行一些转换,并输出新转换的json。为了主要的应用程序,我不想硬编码转换逻辑,而是它将是动态的(动态的,不同的开发人员将在运行时提供它)

basically, i am getting a JSON object, and depending on certain criteria, i want to do some transformation to that json, and output the new transformed json. for the sake of the "main" app, i dont want to hard code the transformation logic, instead it will be dynamic (dynamic in the sense that a different developer will provide it at run time)

所以DB可能包含:

ID   |    javascript 
====================
1    |    (some js code)
2    |    (same func, different code)

我想要做的是执行存储在DB中的JS代码,根据我的选择输入。

what i want to do is execute that JS code stored in the DB, with an input of my choosing.

如果它更容易,函数名称将是标准的...即我们可以假设保存在数据库中的javascript将全部遵循:

if it makes it easier, the function name will be standard.. i.e we can assume that the javascript saved in the DB will all follow:

function transform(input) {
  /* below this line logic will change

* end diff logic/
 return output
}


推荐答案

你可以使用 <$ c $执行任意JS的任意字符串c> eval()功能,它将返回被评估的结果。

You can execute any arbitrary string of JS using the eval() function, which will return the result of whatever was evaluated.

所以就你的目的而言,你在哪里想要分配一个变量来保存字符串中的(eval'ed)函数,你可以这样做:

So for your purposes, where you want to assign a variable to hold the (eval'ed) function that was in a string, you can do this:

// retrieve string from DB somehow
var functionString = "(function whatever(name) { return name + 1; })";
var x = eval(functionString);
console.log(x("bob")); // logs "bob1"

请注意,我已将函数包装在括号中的字符串中,因为这使得它成为一个函数表达式,然后可以将其赋值给变量 x

Note that I've wrapped the function in the string in parentheses, because that makes it a function expression which can then be assigned to the variable x.


我不想硬编码转换逻辑,相反它会是动态(在不同的开发人员将在运行时提供动态的动态)

在我看来,另一种方法更好的方法是将所有转换函数放入一个单独的JavaScript模块,然后根据标准 require() .org / api / modules.htmlrel =nofollow> Node.js模块加载系统。这样,转换逻辑在另一个文件中保持独立,可以根据需要由不同的开发人员单独维护,使用数据库操作和 eval()

Another approach, in my opinion a better approach, is to put all of your transformation functions into a separate JavaScript module that you then require() as per the standard Node.js module loading system. That way the transformation logic is kept separate in another file that can be maintained separately by a different developer as needed, without mucking around with DB operations and eval():

// transforms.js
module.exports = {
  "1" : function plus1(name) { return name + 1; },
  "2" : function square(x) { return x * x; },
  "3" : function half(x) { return x / 2; }
  // etc.
};

// in main JS file
var transforms = require("./transforms.js");

var a = transforms["2"];
var b = transforms["3"];
console.log(a(b(20))); // 100
console.log(transforms["1"]("bob")); // "bob1"

如上所示,每个动态转换函数都是定义为对象的属性,其中属性名称是您将在数据库中使用的键。

As you can see above, each of the "dynamic" transformation functions has been defined as a property of the object where the property names are the keys that you were going to use in your database.

StackOverflow代码片段似乎不处理模块,但如果您展开以下代码段,则可以看到上述代码的等效代码:

StackOverflow code snippets don't seem to handle modules, but if you expand the following snippet you can see an equivalent of the above code working:

// in "transform.js"
var transforms = {
  "1" : function plus1(name) { return name + 1; },
  "2" : function square(x) { return x * x; },
  "3" : function half(x) { return x / 2; }
  // etc.
};

// in main JS file
var a = transforms ["2"];
var b = transforms ["3"];
console.log(a(b(20))); // 100
console.log(transforms ["1"]("bob")); // "bob1"

这篇关于你可以“存储”吗?在DB中的javascript,然后在以后执行它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆