Search code examples
javascriptjavafunctionapiorientdb

calling JS function from orientdb through java API


I'm trying to call a custom JS function stored in OrientDB, from Java, through the OrientDB Java API.

I can use the function inside studio, and it works as expected.

This is the code used to connect to the db and get the function:

OrientGraphFactory factory = new OrientGraphFactory(SERVER_URL, "user", "pass");
OrientGraph txGraph = factory.getTx();
OFunction functionObject = txGraph.getRawGraph().getMetadata()
.getFunctionLibrary().getFunction("functionName");

The problem is that the functionObject returned is null, and inspecting the getFunctionLibrary() result, I can see the list of functions is empty.

Any idea what I'm doing wrong?

Using the official Documentation example, gives the same null result.

LE: For anyone stumbling on this, the full working code is:

OrientGraphFactory factory = new OrientGraphFactory(SERVER_URL, "user", "pass");
OrientGraph txGraph = factory.getTx();
Double response = (Double) txGraph.command(new OCommandFunction("functionName")).execute(functionParameter);

where response is the result the function gives and functionParameteris a parameter I'm passing to the function.


Solution

  • This will work for java functions only (and example in doc is correct because it retrieves java function). In order to execute js/sql/whatever function use OCommandFunction. Something like this:

    txGraph.command(new OCommandFunction("functionName")).execute()
    

    Possibly it could work directly too (if you don't need to convert result to graph objects)

    new OCommandFunction("functionName").execute()
    

    But in this case db instance must be attached to thread (db connection object created before function call)