问题描述
我想使用 aws-sdk DocumentClient()
在dynamodb中使用分页,我正在使用node.js。
I want to use pagination in dynamodb using aws-sdk DocumentClient()
I am using node.js.
我要做的是先获取10个项目,然后将这些值返回给用户。在该用户发出新请求后,他告诉服务器从10开始,服务器从10到20再获得10,然后将响应返回。我已经尝试过 LastEvaluatedKey
,但是我的情况有所不同。有什么方法可以告诉dynamodb从特定项开始,例如1,然后设置 Limit:10
。
What I want to do is get first 10 items and then return these value to the user. After that user makes a new request in which he tell the server to start from 10 and the server get other 10 from 10 to 20 and return the response back. I have tried the LastEvaluatedKey
But my scenario is different. Is there any way that I can tell dynamodb to start from specific Item e.g 1 and then set Limit: 10
.
推荐答案
我找到了解决此问题的方法。您需要从dynamodb响应中获取 LastEvaluatedKey
并将其发送回前端,然后您的前端应发送 LastEvaluatedKey
在参数中,您可以将其用作 ExclusiveStartKey
。
I found a way to work around this. You need to get the LastEvaluatedKey
from the dynamodb response and send it back to the front-end then your front-end should send the LastEvaluatedKey
in params and you can use it as ExclusiveStartKey
.
getItems(pageSize, lastItem?) {
try {
const params = {
TableName: 'User',
Limit: pageSize,
};
if (lastItem) {
params.ExclusiveStartKey = { item_id: lastItem};
}
const response = await dynamoDb.scan(params).promise();
return {
items: response.Items,
lastItem: response.LastEvaluatedKey
}
} catch (error) {
throw error;
}
这篇关于Dynamo db分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!