Can plv8 JavaScript language extension call 3rd party libraries?

后端 未结 2 1679
有刺的猬
有刺的猬 2020-12-19 05:17

In Postgresql, I want to call 3rd party libraries like moment.js or AWS lambda JS Client to invoke serverless functions from within the DB. I don\'t see any docs or example

相关标签:
2条回答
  • 2020-12-19 05:59

    I have two hints pointing in the NO direction:

    1. You can use PLV8 in Amazon RDS PosgreSQL. RDS doesn't allow any language which is not trusted. As explained in PostgreSQL documentation:

      TRUSTED

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

      If PLV8 could use libraries, those would (most probably) allow for performing operations such as downloading data via HTTP, or checking the file system, which would contravene this restriction (and possibly, put RDS system at hacking risk).

    2. Presentation PLV8 - The PostgreSQL web side by Lucio Grenzi.

      Slide #10:

      PLV8: a trusted language

        [...]

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

    A possible alternative

    I have used the PLPERLu (u meaning untrusted) language. Using that language, you can use libraries. Your libraries should be in the standard locations for the PERL installation being used by PostgreSQL (as defined when you CREATE LANGUAGE).

    0 讨论(0)
  • 2020-12-19 06:18

    The plv8 language is trusted so there is no way to load anything from the file system. However you can load modules from the database.

    Create a table with source code of a module and load it using select and eval(). A simple example to illustrate the idea:

    create table js_modules (
        name text primary key,
        source text
    );
    
    insert into js_modules values
    ('test', 'function test() { return "this is a test"; }' );
    

    Load the module from js_modules in your function:

    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) 
    

    You can find a more elaborate example with an elegant require() function in this post: A Deep Dive into PL/v8.. Its is based on plv8.start_proc (see also a short example here).

    0 讨论(0)
提交回复
热议问题