HENNGE / aiodynamo

Asynchronous, fast, pythonic DynamoDB Client
https://aiodynamo.readthedocs.io/
Other
72 stars 21 forks source link

Get most recently added entry #148

Closed torphix closed 1 year ago

torphix commented 1 year ago

Hi, thanks for the awesome lib.

What is the best practice for getting the most recently added entry My model has a attribute created_on that is the unix timestamp of when it was created I therefore just want to get the most recently added value ie: the one with the largest value

Thanks!

ojii commented 1 year ago

My model has a attribute created_on that is the unix timestamp of when it was created I therefore just want to get the most recently added value ie: the one with the largest value

is created_at your sort/range key? if not, you might need to add a secondary index to your table. Please refer to AWS resources on this topic such as this one explaining sort keys or this one explaining indices.

torphix commented 1 year ago

Thank you, I have set sort key to created_at. However I am a bit confused with the table.query() API.

Namely if I have to always pass a HashKey as an argument which requires also providing a unique ID won't the API always just fetch one unique item?

Sorry this is probably a really dumb question

ojii commented 1 year ago

Namely if I have to always pass a HashKey as an argument which requires also providing a unique ID won't the API always just fetch one unique item?

Query only works if you use composite keys, where the combination of hash key and range key form a unique key, with many items sharing the same hash key.

it would help to know your table/item schema and use case to help you.

dimaqq commented 1 year ago

Hash key doesn’t make a unique record.

hash key and range key together don’t make a unique record in a secondary index. Dynamo doesn’t have unique secondary indices by design.

hash key and range key do identify none or one record on the main index.

does this make sense?

torphix commented 1 year ago

Thanks for getting back to me , I worked it out using GSI's

On Sun, 22 Jan 2023, 23:32 Dima Tisnek, @.***> wrote:

Hash key doesn’t make a unique record.

hash key and range key together don’t make a unique record in a secondary index. Dynamo doesn’t have unique secondary indices by design.

hash key and range key do identify none or one record on the main index.

does this make sense?

— Reply to this email directly, view it on GitHub https://github.com/HENNGE/aiodynamo/issues/148#issuecomment-1399641092, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP52P67JLUER74LNMYPOSW3WTW7PXANCNFSM6AAAAAAUASMBPU . You are receiving this because you authored the thread.Message ID: @.***>