DynamoDB Single Table Design: How to get all entities of a type

Viewed 1004

I am on the way creating a Single Table DynamoDB Design for relevant entities in my application. I mainly borrowed ideas from here: https://github.com/aws-samples/amazon-dynamodb-design-patterns/blob/master/data-models/an-online-shop/AnOnlineShop.md

While most of the things are clear to me, one simple thing is missing for me. How can i query all entities of a certain type? See the image below for the schema.

enter image description here

As you can see, the main entity records have PK and SK with the same values. SK only changes when you want to have 1:N relations to other entities, as seen in the entityType warehouseItem.

My main question is: How do i get all users (fast without scan of course)?

What i learned is that i can't do a begins_with on a PartitionKey. Of course i could think of doing records like this:

PK       SK
users   u#logemann
users   u#smith

But this would require additional data entries and would be kind of duplicates to my main user records seen above. Or is it possible to create a GSI with a constant value of "users" for PK and the value of SK for SK?

Whats the best way to solve this?

2 Answers

I think the easiest way is to add "EntityType" in a GSI as PartitionKey and "PK" as SortKey. This way i have all my entities queryable in a performant way.

Related