Documentation Index
Fetch the complete documentation index at: https://mintlify.com/anomalyco/sst/llms.txt
Use this file to discover all available pages before exploring further.
The Dynamo component lets you add an Amazon DynamoDB table to your app.
Constructor
const table = new sst.aws.Dynamo("MyTable", {
fields: {
userId: "string",
noteId: "string"
},
primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
});
Parameters
fields
fields
Record<string, 'string' | 'number' | 'binary'>
required
Define the fields used for indexes. You only need to define fields used in indexes, not all table fields.{
fields: {
userId: "string",
noteId: "string",
createdAt: "number"
}
}
Field types cannot be changed after table creation.
primaryIndex
Define the table’s primary index.{
primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
}
globalIndexes
Configure global secondary indexes. You can have up to 20 global indexes per table.{
globalIndexes: {
CreatedAtIndex: { hashKey: "userId", rangeKey: "createdAt" }
}
}
Create composite keys with multiple attributes:{
globalIndexes: {
RegionCategoryIndex: {
hashKey: ["region", "category"],
rangeKey: "createdAt"
}
}
}
localIndexes
Configure local secondary indexes. You can have up to 5 local indexes per table.{
localIndexes: {
CreatedAtIndex: { rangeKey: "createdAt" }
}
}
stream
stream
"keys-only" | "new-image" | "old-image" | "new-and-old-images"
Enable DynamoDB Streams to capture item changes.{
stream: "new-and-old-images"
}
Use "new-and-old-images" as a good default since it contains all the data.
Streams are not enabled by default due to associated costs.
ttl
The field name to store Time to Live (TTL) timestamps. Items are deleted when TTL is reached.
Properties
name
arn
nodes
Methods
subscribe
SDK
Query the table from your function code:
import { Resource } from "sst";
import { DynamoDBClient, QueryCommand } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient();
await client.send(new QueryCommand({
TableName: Resource.MyTable.name,
KeyConditionExpression: "userId = :userId",
ExpressionAttributeValues: {
":userId": { S: "my-user-id" }
}
}));
Examples
Add a global index
new sst.aws.Dynamo("MyTable", {
fields: {
userId: "string",
noteId: "string",
createdAt: "number"
},
primaryIndex: { hashKey: "userId", rangeKey: "noteId" },
globalIndexes: {
CreatedAtIndex: { hashKey: "userId", rangeKey: "createdAt" }
}
});
Subscribe to streams
const table = new sst.aws.Dynamo("MyTable", {
fields: {
userId: "string",
noteId: "string"
},
primaryIndex: { hashKey: "userId", rangeKey: "noteId" },
stream: "new-and-old-images"
});
table.subscribe("MySubscriber", "src/subscriber.handler");
Link to a function
const table = new sst.aws.Dynamo("MyTable", {
fields: {
userId: "string",
noteId: "string"
},
primaryIndex: { hashKey: "userId", rangeKey: "noteId" }
});
new sst.aws.Function("MyFunction", {
handler: "src/api.handler",
link: [table]
});