Way to sort by createdAt in AWS DynamoDB in conjunction with ApolloClient/React, WITHOUT AWS Amplify

142 Views Asked by At

I am having trouble sorting my DynamoDB database by createdAt

I am working from the AWS console. I’m using AWS AppSync and DynamoDB for the back-end, and React and Apollo Client for the front-end. I’m not using AWS Amplify (company policy)

My original DynamoDB table doesn’t have a createdAt column, which I think is adding to the issue

I have tried creating mutations to add in new records WITH a createdAt timestamp, but it is not working.

I have tried writing queries multiple different ways.

I have tried updating my Type in Schema to include "@model (timestamps: etc.)"

I have tried adding this to the response template for the schema resolver: "$util.time.nowISO8601()"

What is the best way be able to sort the records according to most recent to least and vice versa? With AWS AppSync, DynamoDB, ApolloClient, and React, not AWS Amplify

If you could, please specify if I need to change up my Type, or my Query/mutation, or what the query should look like

Thank you

1

There are 1 best solutions below

2
On

Sorting on DynamoDB is only by the sort key for a given item collection (items which share the same key but uniquely identified by sort key). That means you cannot get ordering of items across the entire table.

If you want to have a sorted order of all items across the entire table you will need to create an index with a static value as the partition key and createdAt as the sort key.