What is Attribute in DynamoDB: Definition and Examples
attribute is a fundamental data element that represents a single piece of information within an item, similar to a column in a table. Each attribute has a name and a value, and together attributes form the structure of an item in a DynamoDB table.How It Works
Think of a DynamoDB table like a spreadsheet where each row is an item and each column is an attribute. An attribute holds a specific piece of data about that item, such as a name, age, or price. Unlike traditional tables, DynamoDB items can have different sets of attributes, so not every item needs to have the same columns.
Attributes have names (like "Name" or "Price") and values (like "Alice" or 25). These values can be simple types like strings or numbers, or more complex types like lists or maps. This flexible structure lets you store diverse data easily.
Example
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({ region: "us-east-1" }); async function addItem() { const params = { TableName: "Users", Item: { "UserId": { S: "user123" }, "Name": { S: "Alice" }, "Age": { N: "30" } } }; try { const data = await client.send(new PutItemCommand(params)); console.log("Item added successfully", data); } catch (err) { console.error("Error adding item", err); } } addItem();
When to Use
Use attributes in DynamoDB whenever you need to store data about an item. Attributes let you describe each item with details like names, dates, prices, or any other information relevant to your application.
For example, in an online store, each product item might have attributes like ProductID, Name, Price, and StockQuantity. In a user database, attributes could include UserID, Email, and SignupDate.
Because DynamoDB allows flexible attributes per item, you can add new attributes as your data needs grow without changing the whole table structure.
Key Points
- An attribute is a named data element within a DynamoDB item.
- Attributes can be simple types like strings and numbers or complex types like lists and maps.
- Items in DynamoDB can have different sets of attributes, allowing flexible data models.
- Attributes together define the data stored for each item in a table.