1
votes

I can't find out if it's possible to invoke AQL queries set up on the ArangoDb web UI in Foxx script - rather like stored procedures can be called by services of a RDMS.

So far all examples I've seen show the AQL embedded into the Foxx services JavaScript.

const result = db._query([name of query defined in Db], {
  "@arg": arg-value
}).toArray();

I would expect the query defined on the ArangoDb to run and pass argument value from Foxx service. but the _query method seems to only accept a query string.

1

1 Answers

0
votes

The queries are stored as properties of the user and as such are available via the users-API:

var _ = require("lodash"); // helper to group the queries
var users = require('@arangodb/users'); 
var myQueries = _.groupBy(users.document(users.currentUser())['extra']['queries'], 'name');

const result = db._query(myQueries['name of query defined in Db'], ...);

lodash is available per default, no need to install it.

This is not clearly documented anywhere -- I took a look at the network traffic of the query-editor, to find the queries under 'extra/queries' of the user; than I searched for a js user-API and found an article on 'User Managment'. I'd say the lack of documentation is a clear 'caveat emptor' -- it might change without notice; but this should get you going.

Additionally, thinking a bit, I only tested this in arangosh, not a foxx app -- maybe the users module isn't available there. If it works you will probably have to replace the users.currentUser() call with the username as string.

Given the ease this approach lends to breaking your application, I wouldn't recommend to use this (if it works...) outside of R&D and/or prototyping.

Edit: I implemented this to test it:

router.post(
    '/getQueries',
    /**
     * @param {Object} req
     * @param {Object} res
     */
    function(req, res) {
        var _ = require("lodash"); // helper to group the queries
        var users = require('@arangodb/users'); 
        var myQueries = _.groupBy(users.document(req.body.user, ['extra']['queries'], 'name'));

        res.json({success: (myQueries !== null), queries: myQueries, error: null});
    }).summary('Return queries stored for the given user').body(joi.object().keys({
        'user': joi.string().required()
    }).unknown(true), ['json']);

This works, the queries are objects with of the following form:

[
               "value" => """
                 FOR x IN @params\n
                    RETURN x
                 """,
               "parameter" => [
                 "params" => "",
               ],
               "name" => "x_test",
             ],

so a foxx service can indeed access and execute queries stored for a user, but I still strongly suggest to not do this in production.