-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathtransact-write.js
55 lines (52 loc) · 1.95 KB
/
transact-write.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// This is an example of a simple TransactWriteCommand with the higher level DocumentClient for Amazon DynamoDB
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, TransactWriteCommand } = require("@aws-sdk/lib-dynamodb");
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);
}
}
transactWriteItems()
.then((data) =>
console.log("TransactWriteCommand succeeded:", JSON.stringify(data, null, 2)))
.catch((error) => console.error(JSON.stringify(error, null, 2)));