Dynamo db pagination

孤街浪徒 提交于 2020-01-05 05:38:07

问题


I want to use pagination in dynamodb using aws-sdk DocumentClient() I am using node.js.

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.


回答1:


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;
    }


来源:https://stackoverflow.com/questions/56074919/dynamo-db-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!