Low-level client. All values must use DynamoDB AttributeValue types (e.g., { S: "hello" }).
@aws-sdk/lib-dynamodb
Document Client wrapper. Accepts and returns plain JavaScript types. Recommended for application code.
Use the Document Client (@aws-sdk/lib-dynamodb) for application code. It marshals native JavaScript types to AttributeValue automatically, so you write { name: "Alice" } instead of { name: { S: "Alice" } }.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";// Base DynamoDB clientconst ddbClient = new DynamoDBClient({ region: "us-east-1" });// Document Client wrapping the base clientconst docClient = DynamoDBDocumentClient.from(ddbClient);
You can pass optional marshalling configuration to DynamoDBDocumentClient.from:
The examples below use the Document Client commands from @aws-sdk/lib-dynamodb. Each section also shows the equivalent low-level command from @aws-sdk/client-dynamodb.
GetCommand / GetItemCommand — retrieve an item by key
PutCommand / PutItemCommand — create or replace an item
// Document Client (recommended)import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";await docClient.send( new PutCommand({ TableName: "Users", Item: { userId: "user-456", name: "Bob", age: 30, active: true, tags: ["admin", "developer"], }, // Optional: fail if the item already exists ConditionExpression: "attribute_not_exists(userId)", }));
UpdateCommand / UpdateItemCommand — update specific attributes
import { DynamoDBDocumentClient, UpdateCommand } from "@aws-sdk/lib-dynamodb";await docClient.send( new UpdateCommand({ TableName: "Users", Key: { userId: "user-123", }, UpdateExpression: "SET #name = :name, age = :age", ExpressionAttributeNames: { "#name": "name", // "name" is a reserved word in DynamoDB }, ExpressionAttributeValues: { ":name": "Alice Smith", ":age": 31, }, ReturnValues: "ALL_NEW", }));
Use ExpressionAttributeNames to alias reserved words or attribute names with special characters.
DeleteCommand / DeleteItemCommand — delete an item
import { DynamoDBDocumentClient, DeleteCommand } from "@aws-sdk/lib-dynamodb";await docClient.send( new DeleteCommand({ TableName: "Users", Key: { userId: "user-123", }, // Optional: only delete if a condition is met ConditionExpression: "attribute_exists(userId)", }));
QueryCommand — query by partition key
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";const response = await docClient.send( new QueryCommand({ TableName: "Orders", KeyConditionExpression: "customerId = :customerId AND createdAt > :since", ExpressionAttributeValues: { ":customerId": "customer-abc", ":since": "2024-01-01", }, // Optional: filter results further (applied after key condition) FilterExpression: "status = :status", ExpressionAttributeValues: { ":customerId": "customer-abc", ":since": "2024-01-01", ":status": "shipped", }, ScanIndexForward: false, // Descending sort by sort key Limit: 20, }));console.log(response.Items); // Array of plain JS objectsconsole.log(response.Count); // Number of items returned
QueryCommand requires a partition key condition. To query a GSI, add IndexName.