user1989933
user1989933

Reputation: 131

Pagination in DynamoDB

I have a requirement for to show the search result on the jsp with maxcount of 10 and it should have a pagination to traverse back and forward as pagination functionality.

Dynamodb has a lastevaluatedkey, but it doesn't help to go back to the previous page, though I can move to the next result set by the lastevaluatedKey.

Can anybody please help on this.

I am using Java SPRING and DynamoDB as the stack.

Thanks Satya

Upvotes: 7

Views: 13776

Answers (4)

Seb
Seb

Reputation: 5842

For a simple stateless forward and reverse navigation with dynamodb check out my answer to a similar question: https://stackoverflow.com/a/64179187/93451.

In summary it returns the reverse navigation history in each response, allowing the user to explicitly move forward or back until either end.

GET /accounts                -> first page
GET /accounts?next=A3r0ijKJ8 -> next page
GET /accounts?prev=R4tY69kUI -> previous page

Upvotes: 0

gnattwc
gnattwc

Reputation: 41

Building on Ray's answer, here's what I did. sortId is the sort key.

// query a page of items and create prev and next cursor
// cursor idea from this article: https://hackernoon.com/guys-were-doing-pagination-wrong-f6c18a91b232
async function queryCursor( cursor) {
  const cursor1 = JSON.parse(JSON.stringify(cursor));
  const pageResult = await queryPage( cursor1.params, cursor1.pageItems);

  const result = {
    Items: pageResult.Items,
    Count: pageResult.Count
  };

  if ( cursor.params.ScanIndexForward) {
    if (pageResult.LastEvaluatedKey) {
      result.nextCursor = JSON.parse(JSON.stringify(cursor));
      result.nextCursor.params.ExclusiveStartKey = pageResult.LastEvaluatedKey;
    }
    if ( cursor.params.ExclusiveStartKey) {
      result.prevCursor = JSON.parse(JSON.stringify(cursor));
      result.prevCursor.params.ScanIndexForward = !cursor.params.ScanIndexForward;
      result.prevCursor.params.ExclusiveStartKey.sortId = pageResult.Items[0].sortId;
    }
  } else {
    if (pageResult.LastEvaluatedKey) {
      result.prevCursor = JSON.parse(JSON.stringify(cursor));
      result.prevCursor.params.ExclusiveStartKey = pageResult.LastEvaluatedKey;
    }
    if ( cursor.params.ExclusiveStartKey) {
      result.nextCursor = JSON.parse(JSON.stringify(cursor));
      result.nextCursor.params.ScanIndexForward = !cursor.params.ScanIndexForward;
      result.nextCursor.params.ExclusiveStartKey.sortId = pageResult.Items[0].sortId;
    }
  }

  return result;
}

Upvotes: 4

Ray Liu
Ray Liu

Reputation: 61

To enable forward/backward, all you need is to keep

the first key, which is hash key + sort key of the first record of the previously returned page (null if you are about to query the first page).

and

the last key of the retrieved page, which is hash key + sort key of the last record of the previously returned page

Then to navigate forward or backward, you need to pass in below parameters in the query request:

Forward: last key as the ExclusiveStartKey, order = ascend

Backward: first key as the ExclusiveStartKey, order = descend

I have achieved this in a project in 2016. DynamoDB might provide some similar convenient APIs now, but I'm not sure as I haven't checked DynamoDB for a long time.

Upvotes: 6

greg
greg

Reputation: 6913

You will have to keep a record of the previous key in a session var, query string, or something similar you can access later, then execute the query using that key when you want to go backwards. Dynamo does not keep track of that for you.

Upvotes: 3

Related Questions