plv8 JavaScript语言扩展可以调用第三方库吗?

Jas*_*son 5 postgresql plv8

在Postgresql中,我想调用第三方库,如moment.js或AWS lambda JS Client,以从DB中调用无服务器功能.我没有看到任何文档或示例如何操作:https: //github.com/plv8/plv8/blob/master/README.md

这是可能的吗?在哪里可以找到如何"导入"或"需要"其他库的示例?

kli*_*lin 7

plv8语言是可信的,因此无法从文件系统加载任何内容.但是,您可以从数据库加载模块.

创建一个包含模块源代码的表,并使用select和加载它eval().一个简单的例子来说明这个想法:

create table js_modules (
    name text primary key,
    source text
);

insert into js_modules values
('test', 'function test() { return "this is a test"; }' );
Run Code Online (Sandbox Code Playgroud)

js_modules您的函数中加载模块:

create or replace function my_function()
returns text language plv8 as $$
//  load module 'test' from the table js_modules
    var res = plv8.execute("select source from js_modules where name = 'test'");
    eval(res[0].source);
//  now the function test() is defined
    return test();
$$;

select my_function();

CREATE FUNCTION
  my_function   
----------------
 this is a test
(1 row) 
Run Code Online (Sandbox Code Playgroud)

require()在这篇文章中你可以找到一个更精细的例子和一个优雅的功能:深入了解PL/v8.

  • 丑陋的解决方案:使用 `select source` 和 `eval()` 我们失去了 V8 的所有性能......没有办法“预编译”吗? (2认同)