DEV Community

Discussion on: Consider DynamoDB for your next project

Collapse
 
vladcostea profile image
Vlad Costea

Nice article, but I'm a bit confused about the last access pattern (getting the users that liked a post):

wouldn't we have to update the transaction code that creates the like to also add a new item (POST#, LIKEPOST#) with the GSI1PK and GSI1SK fields?

Collapse
 
fampinheiro profile image
Filipe Pinheiro • Edited

Yes, I haven't included the attribute properties when Puting an object.
On the Item property we would need to specify the attributes for the different types.

In the case of a Post:

Item: {
  PK: { S: `TOPIC#<TOPIC>` },
  SK: { S: `POST#<KSUID>` },
  GSI1PK: { S: `POST#<KSUID>` },
  GSI1SK: { S: `POST#<KSUID>` },
  type: "POST",
  nrLikes: 0
}

And for the Like:

Item: {
  PK: { S: `USER#<USERNAME>` },
  SK: { S: `LIKEPOST#<USERNAME>` },
  GSI1PK: { S: `POST#<KSUID>` },
  GSI1SK: { S: `LIKEPOST#<USERNAME>` },
  type: "LIKE"
}

In case you already have items on your table you need a migration.
Depending on your use case:

  • You can use Scan method to migrate every entry;
  • Update POST entries when you have the first like;
  • You can use another creative method 🙂.
Collapse
 
vladcostea profile image
Vlad Costea

brilliant, thanks :)