0
votes

I have the following table:

ItemUUID (string) | Purchases (number) | Type (string)
--------------------------------------
book-a | 5 | book
book-b | 1 | book
book-c | 10 | book

Every time someone buys an item, I would like to increment the Purchases. I then would like to query the table to get the most purchased items (ItemUUIDs sorted DESC by Purchases)

I thought I could make ItemUUID a primary key, and make a global secondary index on Type with a sort key of Purchases. Then query the GSI where Type = book, leave sort key blank, and Sort Descending.

This is not working. Is my use case possible with DynamoDB?

1

1 Answers

0
votes

What I came up with works, but I feel is kinda hack'ish.

Table definition

Primary partition key   type (String)
Primary sort key        uuid (String)

Global Secondary Index

Primary partition key   type (String)
Primary sort key        purchases (Number)

Because GSI's don't have to have unique partition+sort keys, I can get a way with doing a query where type=book, leave purchases blank, sort DESC