arangodbfoxx

Arangodb. Using cursor to send back data in Foxx service


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?


Solution

  • 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.