简体   繁体   English

Arangodb。 Foxx服务中使用游标发回数据

[英]Arangodb. Using cursor to send back data in Foxx service

In a foxx service, normally the response is send back using在 Foxx 服务中,通常响应使用

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?但是在我发回的数据很大的情况下,我想模仿游标的行为(以几个较小的块发送),我该怎么做?

You will want to use Query Cursors .您将需要使用Query Cursors

If you are using Arangojs, then here is that documentation .如果您使用的是 Arangojs,那么这里是文档

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.对于 Foxx 的事情,您将需要接受和使用参数来控制分页。 Using the LIMIT functionality:使用LIMIT功能:

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. *编辑 Foxx 端点本质上是 javascript 应用程序。 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.您需要将游标IDhasMore属性传递给客户端,以便它可以请求下一批。 The 'batchSize' property sets how many results are returned via cursor. 'batchSize' 属性设置通过游标返回的结果数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM