in DynamoDB-SDK-Examples/node.js/WorkingWithItems/transact-write.js [6:50]
async function transactWriteItems() {
const client = new DynamoDBClient({ region: "us-west-2" });
const ddbDocClient = DynamoDBDocumentClient.from(client);
try {
return await ddbDocClient.send(
new TransactWriteCommand({
TransactItems: [
// Update the product status if the condition is met
{
Update: {
TableName: "Products",
Key: {
ProductId: "B07J1337PJ42",
},
UpdateExpression: "SET ProductStatus = :new_status",
ConditionExpression: "ProductStatus = :expected_status",
ExpressionAttributeValues: {
":new_status": "SOLD",
":expected_status": "IN_STOCK",
},
ReturnValuesOnConditionCheckFailure: "ALL_OLD",
},
},
// Create the order if it doesn't already exist
{
Put: {
TableName: "Orders",
Item: {
OrderId: "171-3549115-4111337",
ProductId: "productKey",
OrderStatus: "CONFIRMED",
OrderTotal: "100",
},
ConditionExpression: "attribute_not_exists(OrderId)",
ReturnValuesOnConditionCheckFailure: "ALL_OLD",
},
},
],
ReturnConsumedCapacity: "TOTAL",
})
);
} catch (err) {
console.error(err);
}
}