0

I am using a dynamoDB table with GraphQL api to store posts. I want a user to be able to mark certain posts as favorites.

I have thought to create a relation table of user to post, but I also thought to just add an array of userId's to the post object with all the userIds of users who have made that post a favorite.

My understanding is a UUID is 16 bytes so even if say 10,000 users favorite the object then that array will be 160kb. Not insignificant but manageable to pass that much data with the object each time it is loaded.

Just wondering what is the best practice for this scenario. I'm pretty new to nosql.

alionthego
  • 8,508
  • 9
  • 52
  • 125
  • How are you going to be using this? Are you going to be showing the user their own favorites? Or showing who favorited on each post? Or both? How you intend to use data has a big impact on how you want to structure it in noSQL – robinsax Aug 08 '21 at 02:26

1 Answers1

0

With dynamoDB you have to think about access patterns first:

  • To get the favorite posts of a user, store a postsIds array in the user table
  • To get the users who like a post, store a likerIds array in the post table
  • To get a bidirectional link, do both of the above

Please also keep in mind that:

  • You can select fields when getting a document (only select the fields you are interested in)
  • I don't see a scenario where you would load 10k usernames and display them

The above solution looks pretty good for common scenarios.

More advanced solution:

There could be a more powerful way to do that using range keys. For instance:

Hash Key: postID range key: likerID title ...
post1 MyFancyPost
post1 user1
post1 user2

This structures is more powerful, and could store a lot of connections without having any "big" field in the post model.

  • you could easily paginate, and count the list of likers
  • can handle many more likers for only one post
aherve
  • 3,795
  • 6
  • 28
  • 41