简体   繁体   中英

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. I don't want to use js, Can we make it using 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.

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:

{
"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. If you pass ExclusiveStartKey: LastEvaluatedKey you can scan/query with pagination. I added some tweaks to your approach, it may help you.

Edit: You can limit the result by passing Limit: Number to your params. This will allow you to limit the returning item count and you can get more with 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);
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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