I have a dynamodb table in which one column is of type Map. I want an update query which removes the item from the map if the item is in the map or add the item to the map if the item is not in the map. How can i write a VTL fro this problem ?
Asked
Active
Viewed 737 times
0
-
How have you defined the Map in DynamoDB? You should look at https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeDefinition.html for attribute definitions, only allowed values are B, N, S. – enricop89 Jun 18 '19 at 13:16
1 Answers
0
You have to use a pipeline resolver for this.
I did this in a similar case for like/unlike with a single mutation .
You can change and adapt this code for your task.
likeEvent.req.vtl
#if( "$context.identity.username" =="guest")
$util.unauthorized()
#end
#set($id=$util.autoId())
$util.qr($ctx.stash.put("sk", ".."))
$util.qr($ctx.stash.put("skBegining", ".."))
$util.qr($ctx.stash.put("id", ".."))
{}
likeEvent.res.vtl
{
"value": "success"
}
checkIfLiked.req.vtl
{
"version": "2018-05-29",
"operation": "Query",
"index": "creator-sk-index",
"query" : {
"expression": "creator = :creator and begins_with(sk, :sk) ",
"expressionValues" : {
":creator" : $util.dynamodb.toDynamoDBJson("$context.identity.username"),
":sk" : $util.dynamodb.toDynamoDBJson("$ctx.stash.skBegining")
}
}
}
checkIfLiked.res.vtl
$util.toJson($ctx.result)
like.req.vtl
#if( "$context.stash.username" =="guest")
$util.unauthorized()
#end
#set($sk=$ctx.stash.sk)
#set($id=$ctx.stash.id)
#set( $input = {} )
$util.qr($input.put("updatedAt", $util.time.nowEpochSeconds()))
$util.qr($input.put("createdAt", $util.time.nowEpochSeconds()))
$util.qr($input.put("creator", "$ctx.identity.username"))
#if($context.prev.result.items.size()!=0)
{
"version" : "2017-02-28",
"operation" : "DeleteItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson("$context.prev.result.items[0].id"),
"sk": $util.dynamodb.toDynamoDBJson("$context.prev.result.items[0].sk")
}
}
#else
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson("$id"),
"sk": $util.dynamodb.toDynamoDBJson("$sk")
},
"attributeValues" : $util.dynamodb.toMapValuesJson($input)
}
#end
like.res.vtl
{
"value": "success"
}

uchar
- 2,552
- 4
- 29
- 50