2

In a foxx service, normally the response is send back using

res.send("Whatever");

But in the case of my data to send back is huge thus, I want to imitate the behavior of cursor(send in several smaller chunk), how can I do it?

Loredra L
  • 1,485
  • 2
  • 16
  • 32

1 Answers1

1

You will want to use Query Cursors.

If you are using Arangojs, then here is that documentation.

Query all the data:

const cursor = await db._query('FOR x IN 1..5 RETURN x');
const result = await cursor.all()
// result is an array containing the entire query result
assert.deepEqual(result, [1, 2, 3, 4, 5]);
assert.equal(cursor.hasNext(), false);

Or one by one:

// query result list: [1, 2, 3, 4, 5]
const val = await cursor.next();
assert.equal(val, 1);
// remaining result list: [2, 3, 4, 5]

const val2 = await cursor.next();
assert.equal(val2, 2);
// remaining result list: [3, 4, 5]

For the Foxx end of things, you will need accept and use parameters to control the paging. Using the LIMIT functionality:

router.get('/entries/:skip/:take', function (req, res) {
  const keys = db._query(aql`
    FOR entry IN ${foxxColl}
    LIMIT @skip, @take // ** 
    RETURN entry._key
  `, {'skip': skip, 'take': take});
  res.send(keys);
})

** either pass in the skip/take values or calculate them based on a page number.

*EDIT Foxx endpoints are essentially javascript apps. You can easily use a cursor using the paging method above or the cursor endpoints as documented. You will need to pass the cursor ID and hasMore properties to the client so it can request the next batch. The 'batchSize' property sets how many results are returned via cursor.

Andrew Grothe
  • 2,562
  • 1
  • 32
  • 48
  • So it means that in case of complex traversal that I must use Foxx , there is no support for cursor yet? Thank you anyway, I will wait for a while to confirm this before setting this as accepted answer – Loredra L Feb 22 '18 at 14:45
  • @LoredraL Yes, you can use a cursor via the `/_api/cursor` endpoint. Added some clarification to the answer. Does that help? – Andrew Grothe Feb 23 '18 at 13:49