Is there a way to get the same output on a foxxservice as on the arangosh?

43 Views Asked by At

The goal is to develop a foxxservice that simply delivers the available user collections in json.

What I've already tried: Set up local arangodb server. Then read the documentation and use the arangosh to try the commands. Then extended the foxxservice hello world example to get the same output.

arangosh

127.0.0.1:8529@TestDatabase> db._collections()

[ 

  [ArangoCollection 2393976, "_appbundles" (type document, status loaded)], 

  [ArangoCollection 2393973, "_apps" (type document, status loaded)], 

  [ArangoCollection 2393961, "_aqlfunctions" (type document, status unloaded)], 

  [ArangoCollection 2394097, "_fishbowl" (type document, status loaded)],

  [ArangoCollection 2393964, "_frontend" (type document, status loaded)], 

  [ArangoCollection 2393943, "_graphs" (type document, status loaded)], 

  [ArangoCollection 2393970, "_jobs" (type document, status loaded)], 

  [ArangoCollection 2393948, "_modules" (type document, status loaded)],

  [ArangoCollection 2393967, "_queues" (type document, status loaded)], 

  [ArangoCollection 2393951, "_routing" (type document, status loaded)], 

  [ArangoCollection 2394131, "Relation" (type edge, status loaded)], 

  [ArangoCollection 2394114, "Table" (type document, status loaded)] 

]

The foxxservice:

'use strict';

const createRouter = require('@arangodb/foxx/router');

const db = require('@arangodb').db;

const router = createRouter();

module.context.use(router);

router.get('/hello-world', function (req, res) {

  res.send('Hello World!');

  const collections = db._collections();

  res.json(collections);

})

.response(['application/json'], 'A generic greeting.')

.summary('Generic greeting')

.description('Prints a generic greeting.');

Actually, I was expecting the same json. Hopefully it is not impossible for safety reasons.

But this is what the browser shows:

[
{"_id":"2393976","_dbName":"TestDatabase"},

{"_id":"2393973","_dbName":"TestDatabase"},

{"_id":"2393961","_dbName":"TestDatabase"},

{"_id":"2394097","_dbName":"TestDatabase"},

{"_id":"2393964","_dbName":"TestDatabase"},

{"_id":"2393943","_dbName":"TestDatabase"},

{"_id":"2393970","_dbName":"TestDatabase"},

{"_id":"2393948","_dbName":"TestDatabase"},

{"_id":"2393967","_dbName":"TestDatabase"},

{"_id":"2393951","_dbName":"TestDatabase"},

{"_id":"2394131","_dbName":"TestDatabase"},

{"_id":"2394114","_dbName":"TestDatabase"}
]
1

There are 1 best solutions below

0
On

Possible solution:

'use strict';
const createRouter = require('@arangodb/foxx/router');
const db = require('@arangodb').db;
const router = createRouter();
module.context.use(router);
router.get('/hello-world', function (req, res) {
    res.send('Hello World!');
    var collections = [];
    db._collections().forEach(function(element) { 
        if(!element.name().startsWith("_")) {
            var obj = {};
            obj[element._id] = element.name();
            collections.push(obj);
        }
    });
    res.json(collections);
})
.response(['application/json'], 'A generic greeting.')
.summary('Generic greeting')
.description('Prints a generic greeting.');

The generated output:

[
    {"2394131":"Relation"},
    {"2394114":"Table"}
]