0
votes

Suppose I had a users and images tables in DynamoDB.

users table
userId (hash) name email 

images table
imagesId (hash) userId filename

How should I set up the range or GSI if I wanted to get all images for a single userId? Should images table be a composite key with imageId (hash) and userId (range) and search by range (if possible)? or should userId (be a GSI) and query just be userId?

2

2 Answers

3
votes

It looks like you could add a GSI on your Images table with the reverse key schema of your table:

  • hash key - userId
  • range key - imagesId
  • filename - additional property

You can then query this GSI with a userId to get all of its associated imagesId.

1
votes

You cannot query using just a range key in DynamoDB.

Unless you want your query results from the images table to be sorted by userId, there's no point to using it as a range key. If you want to retrieve all of the images with a certain userId from the images table using a query, you need to use a GSI on userId.