What is Item in DynamoDB: Definition and Usage
item is a single record in a table, similar to a row in a spreadsheet. Each item is made up of attributes, which are key-value pairs that store data about that record.How It Works
Think of a DynamoDB table like a big filing cabinet. Each item is like a single file folder inside that cabinet. This folder holds information about one specific thing, such as a customer or a product.
Each item contains attributes, which are like labels on the folder describing details about it. For example, a customer item might have attributes like CustomerID, Name, and Email. These attributes store the actual data values.
Unlike traditional databases where every row must have the same columns, DynamoDB items can have different sets of attributes. This flexibility lets you store varied data easily.
Example
This example shows how to create and put an item into a DynamoDB table using AWS SDK for JavaScript (v3).
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); async function addItem() { const params = { TableName: "Customers", Item: { CustomerID: { S: "123" }, Name: { S: "Alice" }, Email: { S: "alice@example.com" } } }; try { const command = new PutItemCommand(params); const result = await client.send(command); console.log("Item added successfully", result); } catch (error) { console.error("Error adding item", error); } } addItem();
When to Use
Use items in DynamoDB whenever you need to store and retrieve individual records with flexible attributes. This is ideal for applications like user profiles, product catalogs, or session data where each record may have different fields.
Because items can vary in structure, DynamoDB is great for fast, scalable storage without strict schemas. For example, an e-commerce site can store each product as an item with attributes like price, description, and stock count.
Key Points
- An
itemis a single record in a DynamoDB table. - Items contain
attributeswhich are key-value pairs. - Each item can have different attributes, offering flexible data storage.
- Items are similar to rows in a spreadsheet but with no fixed columns.
- Use items to store varied data like user profiles, products, or sessions.