forked from aws-samples/aws-dynamodb-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-item.js
32 lines (29 loc) · 1.33 KB
/
delete-item.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
/* This is an example of a DeleteCommand with a ConditionExpression using
the higher level DocumentClient for Amazon DynamoDB. The conditional
expression must be true for this DeleteCommand to succeed. */
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, DeleteCommand } = require("@aws-sdk/lib-dynamodb");
async function deleteItem() {
const client = new DynamoDBClient({ region: "us-west-2" });
const ddbDocClient = DynamoDBDocumentClient.from(client);
return await ddbDocClient.send(
new DeleteCommand({
TableName: "RetailDatabase",
Key: {
pk: "[email protected]", // Partition key
sk: "metadata", // Sort key
},
// Below is an optional conditional expression to validate the value of the address.pcode
// value is the value provided. If the item matching the partition and sort key are found
// but the values below does not match, then the delete will fail and the item not deleted.
ConditionExpression: "address.pcode = :val",
ExpressionAttributeValues: {
":val" : "98261",
},
})
);
}
deleteItem()
.then((data) =>
console.log("DeleteCommand succeeded:", JSON.stringify(data, null, 2)))
.catch((error) => console.error(JSON.stringify(error, null, 2)));