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

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的Postgre SQL相关文章