Search code examples
amazon-web-servicesaws-lambdaamazon-dynamodbaws-serverlessdynamodb-queries

How to store and retrieve one to many relationship data in AWS dynamodb


{
"vendor_id": "jack123xx",
  "book_list": [
    {
      "category": "crime",
       "price" : "100$",
      "book_id": "1234A",
      "name": "human_body"
    },
    {
      "category": "romantic",
      "book_id": "1234B",
      "name": "trash",
      "price" : "121$",
    }
  ]
}

i want to store the above data in a dynamodb table,frequently retrieve and update the price of books based on book_id and vendor_id ,which is the best way to organise these data in aws dynamodb.


Solution

  • You can use vendor_id as the PartitionKey and book_id as the SortKey.

    Using this approach you achieve all of your requirements like:

    • Fetch all books based on vendor_id
    • Update/delete book details based on vender_id and book_id

    Also check DynamoDB LSI and GSI features that will help you to accommodating future requirements.