头图

Use of JavaScript in PG requires the use of plv8 extension

Need to open the extension

create extension plv8;

Create function template

create or replace function function_name() 
returns void as $$
    // V8 Javascript
    // 在这里写 Javascript Code
$$ language plv8;

Run if you execute

select function_name();

plv8 helloworld example

create or replace function hello_world(name text) 
returns text as $$

    let output = `Hello, ${name}!`;
    return output;

$$ language plv8;

Execute sql use plv8.execute function

create or replace function update_user(id bigint, first_name text) 
returns smallint as $$

    var num_affected = plv8.execute(
        'update profiles set first_name = $1 where id = $2', 
        [first_name, id]
    );

    return num_affected;
$$ language plv8;

links


lidashuang
6.7k 声望165 粉丝

$ Ruby/Elixir/Golang