Class UpdateExpression
java.lang.Object
dev.hogwai.dynamodb.simplified.expression.UpdateExpression
Fluent builder for constructing DynamoDB update expressions as part of the
DynamoDB Simplified library.
Supports all four update expression clauses, SET, REMOVE,
ADD, and DELETE, with a chainable API. The builder
automatically manages expression attribute name placeholders (#u0,
#u1, ...) and expression attribute value placeholders (:u0,
:u1, ...) to avoid reserved word conflicts. Common update patterns
such as incrementing/decrementing numbers, appending/prepending to lists,
and adding/removing set elements are provided as dedicated methods.
-
Method Summary
Modifier and TypeMethodDescription@NonNull UpdateExpressionAdds anADDaction that increments a number attribute by the given value.@NonNull UpdateExpressionAdds anADDaction that inserts the given values into a set attribute.@NonNull UpdateExpressionappendToList(@NonNull String attribute, @NonNull List<?> values) Adds aSETaction that appends a list of values to an existing list attribute usinglist_append.static @NonNull UpdateExpressionbuilder()Creates a newUpdateExpressionbuilder.@NonNull UpdateExpressionAdds aSETaction that decrements a number attribute by the given delta using an in-place arithmetic expression (attr = attr - delta).@NonNull UpdateExpressiondeleteFromSet(@NonNull String attribute, @NonNull Set<?> values) Adds aDELETEaction that removes the given values from a set attribute.@NonNull StringReturns the built update expression string withSET,REMOVE,ADD, and/orDELETEclauses, formatted as required by the DynamoDB API (e.g.,"SET #u0 = :u0, #u1 = :u1 REMOVE #u2").Returns the map of expression attribute names used in this expression.Returns the map of expression attribute values used in this expression.@NonNull UpdateExpressionAdds aSETaction that increments a number attribute by the given delta using an in-place arithmetic expression (attr = attr + delta).booleanisEmpty()Returns whether no update actions have been added to this expression yet.@NonNull UpdateExpressionprependToList(@NonNull String attribute, @NonNull List<?> values) Adds aSETaction that prepends a list of values to an existing list attribute usinglist_append.@NonNull UpdateExpressionAdds one or more attributes to theREMOVEclause, deleting them from the item.@NonNull UpdateExpressionremoveListElement(@NonNull String attribute, int index) Adds a specific list element to theREMOVEclause, removing it by index (e.g.,myList[2]).@NonNull UpdateExpressionAdds aSETaction that assigns the given value to the specified attribute, overwriting any existing value.@NonNull UpdateExpressionsetIfNotExists(@NonNull String attribute, @NonNull Object value) Adds aSETaction that assigns the given value to the specified attribute only if the attribute does not already exist, using theif_not_existsfunction.@NonNull UpdateExpressionsetListElement(@NonNull String attribute, int index, @NonNull Object value) Adds aSETaction that assigns a value to a specific element in a list attribute by index (e.g.,myList[2] = value).@NonNull UpdateExpressionAdds aSETaction that assigns a value to a nested attribute path (dot-separated), such asaddress.city.@NonNull UpdateExpressionSets the TTL attribute to the given duration from now.
-
Method Details
-
builder
Creates a newUpdateExpressionbuilder.- Returns:
- a new empty
UpdateExpression
-
set
Adds aSETaction that assigns the given value to the specified attribute, overwriting any existing value.- Parameters:
attribute- the attribute name (must not be null)value- the value to set (must not be null: useremove(String...)instead)- Returns:
- this builder for chaining
- Throws:
IllegalArgumentException- if value is null
-
setIfNotExists
Adds aSETaction that assigns the given value to the specified attribute only if the attribute does not already exist, using theif_not_existsfunction.- Parameters:
attribute- the attribute namevalue- the value to set if the attribute does not exist- Returns:
- this builder for chaining
-
increment
Adds aSETaction that increments a number attribute by the given delta using an in-place arithmetic expression (attr = attr + delta).- Parameters:
attribute- the attribute namedelta- the amount to increment by- Returns:
- this builder for chaining
-
decrement
Adds aSETaction that decrements a number attribute by the given delta using an in-place arithmetic expression (attr = attr - delta).- Parameters:
attribute- the attribute namedelta- the amount to decrement by- Returns:
- this builder for chaining
-
appendToList
Adds aSETaction that appends a list of values to an existing list attribute usinglist_append. If the attribute does not exist, it is initialized to an empty list before appending.- Parameters:
attribute- the list attribute namevalues- the values to append- Returns:
- this builder for chaining
-
prependToList
Adds aSETaction that prepends a list of values to an existing list attribute usinglist_append. If the attribute does not exist, it is initialized to an empty list before prepending.- Parameters:
attribute- the list attribute namevalues- the values to prepend- Returns:
- this builder for chaining
-
setListElement
public @NonNull UpdateExpression setListElement(@NonNull String attribute, int index, @NonNull Object value) Adds aSETaction that assigns a value to a specific element in a list attribute by index (e.g.,myList[2] = value).- Parameters:
attribute- the list attribute nameindex- the zero-based index of the element to setvalue- the value to assign- Returns:
- this builder for chaining
-
setNested
Adds aSETaction that assigns a value to a nested attribute path (dot-separated), such asaddress.city. Each path segment is individually mapped to an expression attribute name placeholder.- Parameters:
path- the nested attribute path (e.g.,"address.city")value- the value to set- Returns:
- this builder for chaining
-
ttl
Sets the TTL attribute to the given duration from now.Convenience method that computes the epoch timestamp and delegates to
set(String, Object).- Parameters:
attribute- the TTL attribute nameduration- the duration from now when the item should expire- Returns:
- this builder for chaining
-
remove
Adds one or more attributes to theREMOVEclause, deleting them from the item.- Parameters:
attributes- one or more attribute names to remove- Returns:
- this builder for chaining
-
removeListElement
Adds a specific list element to theREMOVEclause, removing it by index (e.g.,myList[2]).- Parameters:
attribute- the list attribute nameindex- the zero-based index of the element to remove- Returns:
- this builder for chaining
-
addToSet
Adds anADDaction that inserts the given values into a set attribute. If the attribute does not exist, DynamoDB creates it with the provided values.- Parameters:
attribute- the set attribute namevalues- the values to add to the set- Returns:
- this builder for chaining
-
addNumber
Adds anADDaction that increments a number attribute by the given value. This uses theADDclause rather thanSET, which is useful for atomic counters in scenarios where you want to ensure the increment is applied regardless of concurrent modifications (DynamoDBADDis idempotent for sets but not for numbers).- Parameters:
attribute- the number attribute namevalue- the amount to add- Returns:
- this builder for chaining
-
deleteFromSet
Adds aDELETEaction that removes the given values from a set attribute.- Parameters:
attribute- the set attribute namevalues- the values to remove from the set- Returns:
- this builder for chaining
-
getExpression
Returns the built update expression string withSET,REMOVE,ADD, and/orDELETEclauses, formatted as required by the DynamoDB API (e.g.,"SET #u0 = :u0, #u1 = :u1 REMOVE #u2"). Only non-empty clauses are included.- Returns:
- the update expression string
-
getExpressionNames
-
getExpressionValues
public @NonNull Map<String, software.amazon.awssdk.services.dynamodb.model.AttributeValue> getExpressionValues()Returns the map of expression attribute values used in this expression. Keys are the generated placeholders (e.g.,:u0) and values are the correspondingAttributeValueinstances.- Returns:
- the expression attribute values map
-
isEmpty
public boolean isEmpty()Returns whether no update actions have been added to this expression yet.- Returns:
trueif no actions have been added,falseotherwise
-