
driven-adapter.dynamo-db.dynamo-db-template-adapter.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}}.dynamodb;
import {{package}}.dynamodb.helper.TemplateAdapterOperations;
import org.reactivecommons.utils.ObjectMapper;
import org.springframework.stereotype.Repository;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest;
import java.util.List;
@Repository
public class DynamoDBTemplateAdapter extends TemplateAdapterOperations /* implements Gateway from domain */ {
public DynamoDBTemplateAdapter(DynamoDbEnhancedClient connectionFactory, ObjectMapper mapper) {
/**
* Could be use mapper.mapBuilder if your domain model implement builder pattern
* super(repository, mapper, d -> mapper.mapBuilder(d,ObjectModel.ObjectModelBuilder.class).build());
* Or using mapper.map with the class of the object model
*/
super(connectionFactory, mapper, d -> mapper.map(d, Object.class /*domain model*/), "table_name", "secondary_index" /*index is optional*/);
}
public List getEntityBySomeKeys(String partitionKey, String sortKey) {
QueryEnhancedRequest queryExpression = generateQueryExpression(partitionKey, sortKey);
return query(queryExpression);
}
public List getEntityBySomeKeysByIndex(String partitionKey, String sortKey) {
QueryEnhancedRequest queryExpression = generateQueryExpression(partitionKey, sortKey);
return queryByIndex(queryExpression, "secondary_index" /*index is optional if you define in constructor*/);
}
private QueryEnhancedRequest generateQueryExpression(String partitionKey, String sortKey) {
return QueryEnhancedRequest.builder()
.queryConditional(QueryConditional.keyEqualTo(Key.builder().partitionValue(partitionKey).build()))
.queryConditional(QueryConditional.sortGreaterThanOrEqualTo(Key.builder().sortValue(sortKey).build()))
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy