Search code examples
javascriptorientdb

How to execute Orientdb server side function from Orientjs?


I am using Orientjs to access a Orientdb database from a nodejs script, but the server side function does not effect the database.

Server side script: This Javascript function addTxt() takes the argument author and text

var db = orient.getGraph();
db.command('sql','insert into Message (author, text) VALUES ("'+author+'", "'+text+'")');
return "1";

Query: This function has been tested in Orient Studio and the following query works:

SELECT addTxt("Testuser","foo")

Nodejs/Orientjs: When invoking this function from a nodejs script using Orientjs, it only returns

[ { '@type': 'd', addTxt: '1', '@rid': { cluster: -2, position: 1 } } ]

and the database remains untouched.

I have tried:

//some code

var OrientDB = require('orientjs');    
var server = OrientDB({
    host: 'localhost',
    port: 2424,
});

var db = server.use({
    name: 'database',
    username: 'admin',
    password: 'pass'

db.query('SELECT addTxt(:arg1, :arg2)', {
    params: {arg1:"Testuser",arg2:"foo"}
}).then(function (response){
    console.log(response);
});

Other queries from Orientjs works.

What am I doing wrong? Is there an other way to invoke a server side function?


Solution

  • You explicit returns "1"

    it is right that returns

    [ { '@type': 'd', addTxt: '1', '@rid': { cluster: -2, position: 1 } } ]
    

    try to directly explicit the commit in your function

    db.commit()