I'm writing an API endpoint to allow users to list items according to the parameters they pass in on the request body.
I want the partition key to be the only required parameter; clients will be allowed to pass zero or more key-value pairs to achieve the desired level of granularity in their query.
My DynamoDB Table holds items with the following structure:
{
"appName": { //PARTITION KEY
"S": "APP_1"
},
"requestId": { // SORT KEY
"S": "request_1224"
},
"creationTime": {
"N": "1636332520679"
},
"resolver": {
"S": "SOMEONES_ID"
},
"status": {
"S": "PENDING"
}
}
I have some working code, where the DynamoDBQueryExpression
is hard-coded and I do have an idea of how to dynamically put together the Query Expression but I'm thinking of an ugly combination of for-loops and string manipulation.
Is there an elegant and clean way of dynamically putting together the DynamoDBQueryExpression
?
Here's the starter code I have, for reference:
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val1", new AttributeValue().withS(requestModel.getAppName()));
eav.put(":val2", new AttributeValue().withS(accessRequestModel.getRequestStatus()));
DynamoDBQueryExpression<RequestModel> queryExpression = new DynamoDBQueryExpression<RequestModel>()
.withKeyConditionExpression("appName = :val1")
.withFilterExpression("RequestStatus = :val2")
.withExpressionAttributeValues(eav);
Just to be extra clear, see how I'm doing put
and passing hard-coded values to withFilterConditionExpression
–I'd like these values being passed dynamically according to client's input.
I'm using Java's AWS DynamoDB Mapper.