[英]How to make child array pagination with node js and aws dynamo db
I want to make pagination of " car_types " using aws dynamo db and node js.我想使用 aws dynamo db 和 node js 对“ car_types ”进行分页。 I don't want to use js, Can we make it using dynamo db ?我不想使用 js,我们可以使用 dynamo db 吗? I want total items, total page, page size, current page and data in response.我想要总项目、总页面、页面大小、当前页面和数据作为响应。
{
"uid": "222-3333",
"car_types": [
{
"description": "fsdf",
"title": "sdfsd"
},
{
"description": "fdfdfdf",
"title": "dfdfd"
},
{
"description": "dasda",
"title": "asdas"
},
{
"description": "dasd",
"title": "asdas"
},
{
"description": "dasdasd",
"title": "asdas"
}
]
}
Aws Dynamo DB and Node js Code, Which I used to get result. Aws Dynamo DB 和 Node js 代码,我用来得到结果。
export function get_car_types_list(){
var params = {
TableName : "cms_cars",
KeyConditionExpression: "#uid = :uid",
ExpressionAttributeNames:{
"#uid": "uid"
},
ExpressionAttributeValues: {
":uid": "222-3333"
}
};
return docClient.query(params).promise()
.then(function(data) {
console.log(data);
return data;
}).catch( (err) => {
console.log('got Error', err);
});
}
I want Result using dynamo db query:我想要使用 dynamo db 查询的结果:
{
"totalItem":5,
"totalPage":1,
"pageSize":"1",
"currentPage":"1",
"car_types": [
{
"description": "fsdf",
"title": "sdfsd"
},
{
"description": "fdfdfdf",
"title": "dfdfd"
},
{
"description": "dasda",
"title": "asdas"
},
{
"description": "dasd",
"title": "asdas"
},
{
"description": "dasdasd",
"title": "asdas"
}
]
}
DynamoDb will return 1 mb data when scan/query
is executed, also LastEvaluatedKey
is added to result if there are any remaining data.执行scan/query
时,DynamoDb 将返回 1 mb 数据,如果有任何剩余数据,还会将LastEvaluatedKey
添加到结果中。 If you pass ExclusiveStartKey: LastEvaluatedKey
you can scan/query
with pagination.如果您通过ExclusiveStartKey: LastEvaluatedKey
您可以使用分页scan/query
。 I added some tweaks to your approach, it may help you.我对您的方法进行了一些调整,它可能对您有所帮助。
Edit: You can limit the result by passing Limit: Number
to your params.编辑:您可以通过将Limit: Number
传递给您的参数来限制结果。 This will allow you to limit the returning item count and you can get more with LastEvaluatedKey
.这将允许您限制返回的项目数,并且您可以使用LastEvaluatedKey
获得更多。
export function get_car_types_list(LastEvaluatedKey){
var params = {
TableName : "cms_cars",
KeyConditionExpression: "#uid = :uid",
ExpressionAttributeNames:{
"#uid": "uid"
},
ExpressionAttributeValues: {
":uid": "222-3333"
},
Limit: 5,
};
if (LastEvaluatedKey) {
params.ExclusiveStartKey = LastEvaluatedKey;
}
return docClient.query(params).promise()
.then(function(data) {
console.log(data);
return data;
}).catch( (err) => {
console.log('got Error', err);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.