Dynamo 数据库分页 [英] Dynamo db pagination
问题描述
我想使用 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 数据库分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!