-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathUpdateItemConditional.cs
63 lines (57 loc) · 2.24 KB
/
UpdateItemConditional.cs
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
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
namespace DotnetSamples.WorkingWithItems
{
public class UpdateItemConditional
{
private readonly IAmazonDynamoDB amazonDynamoDB;
public UpdateItemConditional()
{
amazonDynamoDB = new AmazonDynamoDBClient(RegionEndpoint.USWest2);
}
public UpdateItemConditional(IAmazonDynamoDB amazonDynamoDB)
{
this.amazonDynamoDB = amazonDynamoDB;
}
public async Task ServiceClientExampleAsync()
{
try
{
// Define the name of a user account to update. Note that in this example, we have to alias "name" using ExpressionAttributeNames as name is a reserved word in DynamoDB.
// Notice also the conditional expression where it will only update if the age is greater than or equal to 21
var request = new UpdateItemRequest
{
TableName = "RetailDatabase",
Key = new Dictionary<string, AttributeValue>
{
{ "pk", new AttributeValue("[email protected]") },
{ "sk", new AttributeValue("metadata") },
},
UpdateExpression = "set #n = :nm",
ConditionExpression = "age >= :a",
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#n", "name" },
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{ ":nm", new AttributeValue("Big Jim Bob") },
{ ":a", new AttributeValue { N = "21" } },
},
ReturnValues = ReturnValue.ALL_NEW
};
var response = await amazonDynamoDB.UpdateItemAsync(request);
Console.WriteLine($"UpdateItemConditional succeeded.");
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
throw;
}
}
}
}