user2161301
user2161301

Reputation: 824

AWS DynamoDB scan returns Item array - how to request Key-Value dictionary by primary key?

In order to avoid delay when fetching data, I want to download/cache my whole DynamoDB (10MB, 120'000 entries, 3 values each) on launch of my javascript application so some data can be displayed immediately while updated data gets requested and displayed shortly after (in case the entry got updated after the cache time).

For the initial download, the app makes a httprequest to my lambda function which in turn calls

await dynamo.scan({ TableName: tableName }).

This returns a response that looks like the following:

{"Items":[{"key":"personA","address":"ABC"},{"key":"personB","address":"XYZ"}],"Count":2,"ScannedCount":2}

In my app I want to display the address for a specific person. Since the return type is an array I am afraid that I have to iterate over every entry and check for entry['key']='personA'. I would much rather do something like Items['personA'] since I have set key as the primary key of the DynamoDB.

Is there another operation that I can make use of in order to receive a dictionary indexed by the key column of the data?

So basically my goal would be to get

{"Items":{"personA":{"address":"ABC"}},{"personB":{"address":"XYZ"}},"Count":2,"ScannedCount":2}

(Dictionary with the value of key as the actual key instead of a rather inconvenient array)

Just in case my use case sounds dangerous - the app is not for retail customers, it's not available to anyone who should not have access to this data anyways.

Upvotes: 0

Views: 649

Answers (1)

jellycsc
jellycsc

Reputation: 12259

Is there another operation that I can make use of in order to receive a dictionary indexed by the key column of the data?

No, DynamoDB currently does not have such an API with the option for you to transform the data to the format you described.


So basically you are left with the following two options:

  1. Use the Scan response and build the index/dictionary yourself.

    • Advantage: you can reuse this index to look up other people
    • Disadvantage: might be slow to build the initial index since it requires a full list traversal
  2. Retrieve the data using GetItem by providing the primary key.

Upvotes: 1

Related Questions