0

I know that ArangoDB allows for custom AQL functions to be defined server side, but is it possible to define server-side Javascript functions that can be called during transactions?

I'm using the Java driver to connect to ArangoDB, which performs transactions by sending a Javascript function as a string.

To avoid sending a large and complex string every time (one transaction I'm using is ~500 lines long), I'd prefer to have it stored server-side, and called more simply from Java.

E.g. instead of running something like:

String action = "function (params) {"
              + "const db = require('@arangodb').db;"
              + "return db._query('FOR i IN test RETURN i._key').toArray();"
              + "}";
String[] keys = arango.db().transaction(action, String[].class, new TransactionOptions());

I'd like to call something like:

String action = "my_function";
String[] keys = arango.db().transaction(action, String[].class, new TransactionOptions());

Or:

String action = "function(params) {"
              + "const my_function = require("somefunc");
              + "return my_function(params);
              + "}";

Is this possible to achieve?

1 Answer 1

1

You can write custom endpoints with Foxx.

It is a microservice framework written in JavaScript that lets you execute complex JS code on the server-side, with access to ArangoDB's internal JS API (db object etc.). You can encapsulate whatever business logic in it, e.g. your 500 lines of transaction code and add some parameters to control it from outside without having to send all the code each time.

Sign up to request clarification or add additional context in comments.

2 Comments

I considered something like that, but in my case the transactions I need are quite dynamic (based on some java business logic), so it wouldn't be easy to set up a Foxx endpoint without duplicating/migrating all the existing logic there. They all l require some shared functions, which currently get repeated in every transaction, so that's the main part I'd like to move server-side.
Well, you can setup shared code in Foxx Services. You can create one service that contains your shared functions and then call it from your other end points. The documentation to link services can be found here : arangodb.com/docs/stable/foxx-guides-dependencies.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.