2

I'm trying to use plv8 to write postgres functions. Now I would like to use the pgcrypt library in a way like this:

CREATE OR REPLACE FUNCTION v01.myfunction(arg json) 
RETURNS json AS
$BODY$
  var res;
  res = obj.crypt(arg.password, res);
  plv8.elog(NOTICE, res);
  ...

$BODY$
LANGUAGE plv8;

Where crypt comes with the pgcrypt library and is find in another schema. But if running I get the error:

ERROR:  ReferenceError: obj is not defined

Any idea? Thanks!

Rainer
  • 1,067
  • 2
  • 14
  • 29

2 Answers2

4

It's possible to call other PLV8 functions in other schemas, yes.

You're not calling the function right though. Use

var myFunction = plv8.find_function("schemaName.functionName")

var result = myFunction();

AFAIK you can only call other PLV8 functions this way, not say PLPGSQL.

You can use plv8.execute("select someFunctionInAnotherLanguage()")

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
  • thanks, I tried this as well but got an error as well. Obviously because pgcrypt delivers no plv8 functions. So in my case it doesn't help. – Rainer Jun 03 '15 at 17:56
  • 1
    So I should ask: It's possible to call plpgsql functions from within plv8 functions? – Rainer Jun 03 '15 at 17:56
3

Ok, I found it:

var pp = plv8.execute("SELECT obj.crypt($1,$2)",[arg.password, cpw]);

easy :-)

Rainer
  • 1,067
  • 2
  • 14
  • 29