Back to quests
Quest · quest 2
Simple Primary Key in Action
We'll store each Pokémon by its unique Name as the Partition Key.
Table designPutItemGetItem
Key takeaways
- Choose a partition key that is unique and frequently queried.
- Match attribute types between your definitions and data writes.
- Use consistent casing when you put and get items.
Scenario
We'll store each Pokémon by its unique Name as the Partition Key.
Simple primary keys shine when you rarely query for groups of items and just need fast lookups by ID.
Table Definition
Define a DynamoDB table with Name as the partition key.
const table = new dynamodb.Table(this, "FirstGenPokemon", {
partitionKey: { name: "Name", type: dynamodb.AttributeType.STRING },
});Insert Item Example
Insert a Pokémon record using the PutItem API:
{
Name: "Bulbasaur",
Type1: "grass",
Type2: "poison",
Height_m: 0.7,
Weight_kg: 6.9
}Get Item Example
Retrieve the Pokémon by supplying the same partition key value:
{
TableName: "FirstGenPokemon",
Key: { Name: "Bulbasaur" }
}Check your understanding
Quick quiz
Which attribute is used as the Partition Key here?