The catalogue is read one monster at a time, by its id, all day long. That is DynamoDB's job.
A table
A table fixes only its key: a partition key, and optionally a sort key (the next lesson). Every other attribute is up to each item. On-demand capacity (PAY_PER_REQUEST) bills per request; leave it out and the table is provisioned, and wants --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5.
aws dynamodb create-table --table-name market-monsters --attribute-definitions AttributeName=id,AttributeType=S --key-schema AttributeName=id,KeyType=HASH --billing-mode PAY_PER_REQUESTIt is CREATING for a few seconds, then ACTIVE: aws dynamodb describe-table --table-name market-monsters --query "Table.[TableStatus,ItemCount]" --output text.
Items
An item is typed JSON: each value says its type.
{ "id": {"S": "m-001"}, "name": {"S": "Glimmerwing"}, "type": {"S": "air"}, "price": {"N": "120"}, "rare": {"BOOL": true} }S is a string, N a number (written as a string), BOOL, L a list, M a map. Put writes the whole item, replacing one with the same key; the condition refuses that:
aws dynamodb put-item --table-name market-monsters --item file://glimmerwing.json --condition-expression "attribute_not_exists(id)"aws dynamodb batch-write-item --request-items file://catalogue.json puts up to 25 at once. Get reads one by its full key. name is a reserved word, so it goes through an alias:
aws dynamodb get-item --table-name market-monsters --key '{"id": {"S": "m-001"}}' --projection-expression "#n, price" --expression-attribute-names '{"#n": "name"}'Expressions use placeholders
| Placeholder | Holds | Given with |
|---|---|---|
| #n | an attribute name | --expression-attribute-names |
| :p | a value | --expression-attribute-values |
Update changes some attributes and leaves the rest; SET stock = stock + :n adds to a number.
aws dynamodb update-item --table-name market-monsters --key '{"id": {"S": "m-001"}}' --update-expression "SET price = :p" --expression-attribute-values '{":p": {"N": "135"}}'aws dynamodb delete-item --table-name market-monsters --key '{"id": {"S": "m-001"}}'