Home > Back-end >  How to Implement sorting in REST API with DynamoDB
How to Implement sorting in REST API with DynamoDB

Time:10-29

I am trying to implement pagination sorting of records in my rest API using dynamodb.

There are 4 attributes in the table:-

  • a - pk ( partition key)
  • b - SK (range key)
  • createdAt - normal attribute
  • updatedAt - normal attribute

Since dynamodb always sorts data by default on the range key. This will be a problem when I would like to sort based on created or updated with pagination as some data will be on page 1 for 2020 and 2021 years and some data on page 2 will be of 2022 and 2020 if the range key item starts with z ( just an example)

is there any workaround for this where we can sort dynamic data with pagination and other attributes that are not range key? Also i dont want to use index for the attributes as it can ensure cost

CodePudding user response:

If you want to search more indices, you must create more indices. It sounds like you need local secondary indices on updated and created. Then when you sort by that, you switch your pagination to using the correct index.

And I understand that you don't want to pay, but that cost is precisely for what you seek, as far as I can tell.

  • Related