Skip to main content

UpdateItem Request Builder

Overview

This section describes the EfficientDynamoDb API for building the UpdateItem request.

The UpdateEntityRequestBuilder<TEntity> interface provides a builder pattern for constructing a UpdateItem operation in DynamoDB.

Use the DynamoDbContext.UpdateItem() method to get the builder:

var builder = ddbContext.UpdateItem();

For the UpdateItem request you only required to specify primary key using the WithPrimaryKey method. All other builder methods are optional and can be omitted. In this case, DynamoDB will use the default behavior.

UpdateItem Configuration

WithPrimaryKey (with only Partition key)

Specifies the partition key of the item to update.

IUpdateEntityRequestBuilder<TEntity> WithPrimaryKey<TPk>(TPk pk);
info

It is required to specify the primary key for every UpdateItem request.

caution

Use this method if the table has only a partition key. If the table has both partition and sort keys, use WithPrimaryKey<TPk, TSk> instead.

Parameters

  • pk: The partition key of the item.

Example

builder = builder.WithPrimaryKey("partitionKey");

WithPrimaryKey (with both Partition and Sort keys)

Specifies the partition and sort keys of the item to update.

IDeleteItemEntityRequestBuilder<TEntity> WithPrimaryKey<TPk, TSk>(TPk pk, TSk sk);
info

It is required to specify the primary key for every UpdateItem request.

caution

Use this method only if the table has both partition and sort keys. If the table only has a partition key, use WithPrimaryKey<TPk> instead.

Parameters

  • pk: The partition key of the item.
  • sk: The sort key of the item.

Example

builder = builder.WithPrimaryKey("partitionKey", "sortKey");

WithReturnValues

Specifies the attributes to include in the response.

UpdateEntityRequestBuilder<TEntity> WithReturnValues(ReturnValues returnValues);

If not specified, no values are returned in the response.

Parameters

  • returnValues: ReturnValues enum option. All values of ReturnValues enum are valid for UpdateItem operation. Passing ReturnValues.None is equivalent to not using the WithReturnValues method at all.

Example

builder = builder.WithReturnValues(ReturnValues.UpdatedOld);

WithReturnConsumedCapacity

Specifies the consumed capacity details to include in the response.

UpdateEntityRequestBuilder<TEntity> WithReturnConsumedCapacity(ReturnConsumedCapacity returnConsumedCapacity);

If not specified, no consumed capacity info is returned in the response.

Parameters

  • returnConsumedCapacity: The type of consumed capacity information to return. Setting it to ReturnConsumedCapacity.None is equivalent to not using the ReturnConsumedCapacity() method at all.

Example

builder = builder.WithReturnConsumedCapacity(ReturnConsumedCapacity.Total);

WithReturnValuesOnConditionCheckFailure

Specifies how to handle return values if the operation fails.

UpdateEntityRequestBuilder<TEntity> WithReturnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure option);

If not specified, no values are returned in case of condition check failure.

Parameters

  • option: Option for handling return values on condition check failure.

Example

builder = builder.WithReturnValuesOnConditionCheckFailure(ReturnValuesOnConditionCheckFailure.AllOld);

WithCondition (Explicit condition)

Specifies condition for the UpdateItem operation.

UpdateEntityRequestBuilder<TEntity> WithCondition(FilterBase condition);

Parameters

  • condition: The condition that is used to determine whether the UpdateItem operation should succeed or fail. Refer to our condition expression building guide to learn how to build a condition in EfficientDynamoDb.

Example

var expr = Condition<EntityClass>.On(item => item.Pk).Exists();
builder = builder.WithCondition(expr);

WithCondition (Function condition)

Specifies the condition function for the UpdateItem operation.

IUpdateEntityRequestBuilder<TEntity> WithCondition(Func<EntityFilter<TEntity>, FilterBase> filterSetup);

Parameters

  • filterSetup: The condition function that is used to determine whether the UpdateItem operation should succeed or fail. Refer to our condition expression building guide to learn how to build a condition in EfficientDynamoDb.

Example

builder = builder.WithCondition(
cond => cond.On(item => item.FirstName).EqualTo("John")
);

AsDocument

Represents the returned item as a Document.

This method changes the type of the builder.

IUpdateItemDocumentRequestBuilder<TEntity> AsDocument();

Example

var documentBuilder = builder.AsDocument();

After execution, this UpdateItem request will return the Document instead of the original entity of the builder.

On

Specifies the attribute to be updated in the DynamoDB item.

To update multiple attributes, call this method multiple times. For a detailed walkthrough and examples, refer to the update expression developer guide.

IAttributeUpdate<IUpdateEntityRequestBuilder<TEntity>, TEntity, TProperty> On<TProperty>(Expression<Func<TEntity, TProperty>> expression);

Parameters

  • expression: An expression identifying the attribute to be updated.

Example

builder = builder.On(item => item.FirstName).Assign("John")
.On(item => item.UpdatesCount).Increment(1);

UpdateItem Execution

There are 2 versions of every query execution method: regular and document. All versions have same parameters, the only difference is entity type returned value:

  • In most cases, the original entity TEntity is returned.
  • If AsDocuments() was used, the execution method will contain the entity type of Document.

For simplicity, this document covers only regular version of execution methods.

ExecuteAsync

Executes the UpdateItem operation.

Task ExecuteAsync(CancellationToken cancellationToken = default);

Parameters

  • cancellationToken: Token that can be used to cancel the task.

Example

await builder.ExecuteAsync();

ToItemAsync

Executes the UpdateItem operation and returns the item before the update.

The item is returned as it appeared before the UpdateItem operation, but only if WithReturnValues was specified in the request chain. Otherwise, null is returned.

Task<TEntity?> ToItemAsync(CancellationToken cancellationToken = default);

Parameters

  • cancellationToken: Token that can be used to cancel the task.

Example

var item = await builder.ToItemAsync();

ToResponseAsync

Executes the UpdateItem operation and returns the deserialized response. Besides the item, this response contains ConsumedCapacity property if WithReturnConsumedCapacity was used.

Task<UpdateItemEntityResponse<TEntity>> ToResponseAsync(CancellationToken cancellationToken = default);

Example

var response = await builder.ToResponseAsync();
var item = response.Item;
var consumedCapacity = response.ConsumedCapacity;