微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

解决方法

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"; }' );

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

您可以在这文章中找到一个更精细的示例,其中包含一个优雅的require()函数A Deep Dive into PL/v8.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐