DynamoDB Cheatsheet
Posted on:
A cheatsheet of common functions called using the AWS SDK v3 DynamoDB DocumentClient.
Initial Setup
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
const ddbClient = new DynamoDBClient({
region: "<region>",
credentials: {
accessKeyId: "<acccessKeyId>",
secretAccessKey: "<secretAccessKey>",
},
});
const docClient = DynamoDBDocumentClient.from(ddbClient);
Query
The following expects a partition key of year_month
.
const resp = await docClient.send(
new QueryCommand({
TableName: "<table_name>",
KeyConditionExpression: "year_month = :ym",
ExpressionAttributeValues: {
":ym": "2022-12",
},
})
);
const items = resp.Items;
Put
const data = {
foo: "",
bar: "",
};
await docClient.send(
new PutCommand({
TableName: "<table_name>",
Item: {
year_month: "2022-12",
timestamp: dayjs().unix().toString(),
...data,
},
})
);