How to execute JS based queries natively outside of Foxx?

166 views
Skip to first unread message

Suhas Chatekar

unread,
Nov 25, 2015, 8:48:13 AM11/25/15
to ArangoDB
Disclaimer - I am asking this out of curiosity. There is nothing that is blocking me at the moment. 

Arangosh has a switch that lets you specify a JS files from which to execute queries. I have confirmed with Claudius Weinberger that this makes use of Arango HTTP API behind the scenes. Same is true for ArangoJs (NodeJs client). Is there a way to get JS based queries to execute natively given ArangoDb supports JS natively?

Thanks
Suhas

Jan

unread,
Nov 25, 2015, 8:53:13 AM11/25/15
to ArangoDB
Hi,

It's possible to send a JavaScript snippet to the server for execution there.

For example, the following code can be executed in the arangosh to send the contents of a script file the server and return the script's return value:

arangosh> arango.POST("/_admin/execute?returnAsJSON=true", require("fs").read("test.js"));


An example content for the "test.js" script could be:

return (function () {
  var db = require("org/arangodb").db;

  db._drop("test");
  db._create("test");
  for (var i = 0; i < 100; ++i) {
    db.test.insert({value:i});
  }

  return db.test.toArray();
})();

Note that the code in the script will be executed server-side, and some variables predefined in arangosh such as "db" and "print" will not be available there.
If required, these variables need to be initialized in the script as is done with "db" in the example above.
I hope this helps.

Best regards
Jan

Mark White

unread,
Dec 2, 2015, 5:28:31 PM12/2/15
to ArangoDB
I believe that executes against the _system database. 

I'm curious to know if there's a similar way of executing queries against other databases ...

Thanks,
Mark.

Jan Steemann

unread,
Dec 3, 2015, 3:44:28 AM12/3/15
to aran...@googlegroups.com
Hi,

yes, definitely this can be executed in another database.
Just switch the database via `db._useDatabase("someOtherDatabaseName");` before you execute the line `arango.POST("/_admin/execute?returnAsJSON=true", require("fs").read("test.js"));` in the ArangoShell.
Switching the database will make all further actions in the ArangoShell executed in that database.
Because the ArangoShell has a cache for collection names, the shell may not be aware of collections that are created server-side after it was started. So it may necessary to run a `db._flushCache();` in the shell after you create a collection server-side.

Best regards
Jan

--
You received this message because you are subscribed to a topic in the Google Groups "ArangoDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/arangodb/pXXF3t_pF6U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to arangodb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Suhas Chatekar

unread,
Dec 3, 2015, 4:34:43 AM12/3/15
to aran...@googlegroups.com
Jan,

This is good. Looks quite similar to stored procedures in traditional RDBMS world. Couple of questions

1. Where is test.js located? How do I deploy this file?
2. Is it possible for this code to accept arguments? 
3. If I had to unit test the code in test.js, can I load it inside a mocha script and execute it against local database?


Regards,
Suhas Chatekar
Skype - suhas.chatekar

Jan Steemann

unread,
Dec 3, 2015, 5:26:46 AM12/3/15
to aran...@googlegroups.com
Hi,

the command I sent you is good for sending arbitrary ad-hoc operations to the server.
However, if you're after something like stored procedures (i.e. fixed code that is called with variable parameters), I suggest using [Foxx](https://docs.arangodb.com/Foxx/) instead.

Writing the stored procedure using Foxx makes the procedure code callable via a (definable) URL path.
You can then use HTTP URL or body parameters to pass input parameters to the procedure.
Additionally, by using Foxx you will get an automatic API documentation and can write tests in Mocha. There's a recipe for writing tests [here](https://docs.arangodb.com/cookbook/FoxxTesting.html).

Best regards
Jan

Reply all
Reply to author
Forward
0 new messages