What you'll learn
- GetItem retrieves a single item by its primary key efficiently.
- Using the partition key directly is faster than scanning the entire table.
- GetItem needs the full primary key: the partition key, plus the sort key if the table has one.
Section 01
The Choice
Professor Oak presents you with three starter Pokémon. Each has unique characteristics that will shape your journey. Take a look at their types and sizes:
| Name | Type1 | Type2 | Height(m) | Weight(kg) |
|---|---|---|---|---|
| Bulbasaur | grass | poison | 0.7 | 6.9 |
| Charmander | fire | --- | 0.6 | 8.5 |
| Squirtle | water | --- | 0.5 | 9.0 |
Choose wisely! Each starter has a different type and build that will influence your adventure.
Section 02
Why GetItem Instead of Scan?
In Quest 3, you learned about Scan, which reads every item in the table. But when you know exactly which Pokémon you want, there's a much more efficient way!
- Scan reads through every single item in the table, even if you only need one Pokémon.
- GetItem uses the partition key to go directly to the exact item you want, like looking up a word in a dictionary.
- GetItem is faster and cheaper because DynamoDB doesn't need to examine every item.
- You must know the exact
Namevalue to use GetItem—perfect for fetching your chosen starter! - GetItem is eventually consistent by default (half a read unit per 4 KB). Add
ConsistentRead: truewhen you must see the very latest write; it costs a full read unit.
Ready · 7 steps · Sprig
This animation plays when you scroll to it. Press Play to start it now.
All steps as text
- Your app sends Scan to the Pokemon table. FilterExpression Name = "Bulbasaur"
- P0 reads 3 items to return 1: Bulbasaur.
- P1 reads 2 items to return 0.
- P2 reads 2 items to return 0.
- P3 reads 1 items to return 0.
- This call consumed 1 RCU.
- DynamoDB responds with success: 8 items read and paid for to find 1.
Section 03
Fetching Your Starter Pokémon
Now it's time to fetch your chosen starter. This is an authentic GetItem request shape evaluated only by the ServerlessCreed emulator:
Key Points: Notice how we specify the exact Name in the Key object. DynamoDB uses this to compute the hash and go directly to the partition containing that Pokémon. This is much more efficient than scanning the entire table!
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({
region: "ap-south-1",
});
const docClient = DynamoDBDocumentClient.from(client);
async function getStarterPokemon(pokemonName: string) {
const command = new GetCommand({
TableName: "Pokemon",
Key: {
Name: pokemonName,
},
});
try {
const response = await docClient.send(command);
if (response.Item) {
console.log("Starter Pokémon found:", response.Item);
return response.Item;
} else {
console.log("Pokémon not found");
return null;
}
} catch (err) {
console.error("GetItem failed:", err);
throw err;
}
}
getStarterPokemon("Bulbasaur");Practice
Your turn
Name in the Key object to one of the starters — "Bulbasaur", "Charmander" or "Squirtle" — then press Run code. Stuck? The Reference solution tab has the answer.Name line inside Key is editable. Pick your starter.Check
Check your understanding
Quick Quiz · one question, as many tries as you need.
Name of a Pokémon you want to fetch, which operation should you use?Choose one answer.