Skip to content

Commit

Permalink
[sc130691] add a function to micronaut-aws-sdk dynamo lib to update m…
Browse files Browse the repository at this point in the history
…ultiple attributes (#236)

* [sc130691] add a function to micronaut-aws-sdk dynamo libs to update multiple attributes

* fix codenarc

* fix generated doc
  • Loading branch information
mkoterba-agorapulse authored Jul 10, 2024
1 parent b18fe6a commit 4844b91
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -736,22 +736,46 @@ class DefaultDynamoDBService<TItemClass> implements DynamoDBService<TItemClass>
String attributeName,
Object attributeValue,
AttributeAction action
) {
return this.updateItemAttributes(hashKey, rangeKey, Map.of(attributeName, attributeValue), action)
}

/**
* Update a multiple item attributes
*
* @param hashKey
* @param rangeKey
* @param valueByAttributeKey
* @param action
* @return
*/
UpdateItemResult updateItemAttributes(
Object hashKey,
Object rangeKey,
Map<String, Object> valueByAttributeKey,
AttributeAction action
) {
Map<String, Object> key = [(hashKeyName): buildAttributeValue(hashKey)]
if (rangeKey) {
key[rangeKeyName] = buildAttributeValue(rangeKey)
}

UpdateItemRequest request = new UpdateItemRequest(
tableName: metadata.mainTable.tableName(),
key: key,
returnValues: ReturnValue.UPDATED_NEW
).addAttributeUpdatesEntry(
attributeName,
new AttributeValueUpdate(
action: action,
value: buildAttributeValue(attributeValue)
)
)

valueByAttributeKey.entrySet().forEach { entry ->
request.addAttributeUpdatesEntry(
entry.key,
new AttributeValueUpdate(
action: action,
value: buildAttributeValue(entry.value)
)
)
}

return client.updateItem(request)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@ default UpdateItemResult deleteItemAttribute(Object hashKey, String attributeNam
*/
UpdateItemResult updateItemAttribute(Object hashKey, Object rangeKey, String attributeName, Object attributeValue, AttributeAction action);

UpdateItemResult updateItemAttributes(Object hashKey, Object rangeKey, Map<String, Object> values, AttributeAction action);

/**
* Update a single item attribute
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ import com.amazonaws.services.dynamodbv2.AmazonDynamoDB
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression
import com.amazonaws.services.dynamodbv2.datamodeling.IDynamoDBMapper
import com.amazonaws.services.dynamodbv2.model.AttributeAction
import com.amazonaws.services.dynamodbv2.model.CreateTableResult
import com.amazonaws.services.dynamodbv2.model.UpdateItemResult
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import org.joda.time.DateTime
import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import spock.lang.Specification
import spock.lang.Stepwise

import jakarta.inject.Inject
import java.time.Instant
import java.time.temporal.ChronoUnit

Expand Down Expand Up @@ -364,6 +366,27 @@ class DefaultDynamoDBServiceSpec extends Specification {
]) == 0
}
void 'updateItemAttributes should correctly update specified attribute'() {
given:
s.save(new DynamoDBEntity(parentId: '0', id: '1', rangeIndex: 'foo', number: 0))
Map<String, Object> valueByAttributeKey = Map.of('rangeIndex', 'bar')
when:
UpdateItemResult result = s.updateItemAttributes('0', '1', valueByAttributeKey, AttributeAction.PUT)
then:
assert result.attributes.size() == 1
assert result.attributes.get('rangeIndex').s == 'bar'
when:
valueByAttributeKey = Map.of(
'rangeIndex', 'randomRangeIndex',
'number', 1
)
result = s.updateItemAttributes('0', '1', valueByAttributeKey, AttributeAction.PUT)
then:
assert result.attributes.size() == 2
assert result.attributes.get('rangeIndex').s == 'randomRangeIndex'
assert result.attributes.get('number').n == '1'
}
private static <T> List<T> toList(Publisher<T> publisher) {
return Flux.from(publisher).collectList().block()
}
Expand Down

0 comments on commit 4844b91

Please sign in to comment.