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.