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

Can plv8 JavaScript language extension call 3rd party libraries?

在 Postgresql 中,我想调用第 3 方库,如 moment.js 或 AWS lambda JS 客户端,以从数据库中调用无服务器函数。我没有看到任何文档或示例如何执行此操作: https://github.com/plv8/plv8/blob/master/README.md

这可能吗?我在哪里可以找到如何 'import' 或 'require' 附加库的示例?

我有两个 提示 指向 NO 方向:

  1. You can use PLV8 in Amazon RDS PosgreSQL。 RDS 不允许任何不受信任 的语言。如 PostgreSQL 文档中所述:

    TRUSTED

    TRUSTED specifies that the language does not grant access to data that the user would not otherwise have.

    如果 PLV8 可以使用库,那么这些库将(很可能)允许执行诸如通过 HTTP 下载数据或检查文件系统等操作,这将违反此限制(并可能使 RDS 系统面临被黑客攻击的风险) .

  2. 演示文稿 PLV8 - The PostgreSQL web side 作者:Lucio Grenzi。

    幻灯片#10:

    PLV8: a trusted language

      [...]

    • no way to load external processing modules from the file system

一个可能的选择

我使用了PLPERLuu意思是不受信任)的语言。使用该语言,您可以 use 个库。您的库应该位于 PostgreSQL 使用的 PERL 安装的标准位置(如您 CREATE LANGUAGE 时所定义)。

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

使用模块的源代码创建一个 table 并使用 selecteval() 加载它。一个简单的例子来说明这个想法:

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

insert into js_modules values
('test', 'function test() { return "this is a test"; }' );

在您的函数中从 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) 

您可以在这个 post 中找到一个带有优雅 require() 函数的更详细的示例:A Deep Dive into PL/v8.. Its is based on plv8.start_proc (see also a )。