JBDev
JBDev

Reputation: 186

VTL: Add a new property to an maps inside an array (AWS Appsync, DynamoDB)

I'm new to VTL and AWS appsync and try to get my head around how things are working.

The maps that are representing Steps in a List should have the property id with a UUID before there are stored in DynamoDB. To accomplish this I tried to iterate over the array and access the put method on the map like in the example below.

{
  "version" : "2017-02-28",
  "operation" : "PutItem",
  "key": {
      "id" : $util.dynamodb.toDynamoDBJson($util.autoId())
  },
  #set($input = $util.dynamodb.toMapValues($ctx.args.input))
  #set($input.createdAt = $util.dynamodb.toDynamoDB($util.time.nowISO8601()))

  #foreach($step in $input.steps)
    $step.put('id', $util.dynamodb.toDynamoDBJson($util.autoId())))
  #end 

  "attributeValues": $util.toJson($input)
}

my second try:

#foreach($step in $input.steps)
    #set($step.id = $util.dynamodb.toDynamoDBJson($util.autoId()))
  #end

but still, for no reason, my maps do not have the property id. Is there maybe the problem what the foreach loop is giving me just a copy of the map I try to modify and not the original object?

Thank you for your time! Hopefully, my question will serve all newbies to VTL and appsync

Upvotes: 1

Views: 1078

Answers (1)

Carlos Espin
Carlos Espin

Reputation: 119

They do, it's just that the toMapValues utility method is returning you DynamoDB types.

So if "input.steps" is supposed to be a list what you're gonna get in there is an object like {"L": [ ... ]}

Try this:

#foreach($step in $input.steps.L)
    $step.put('id', $util.dynamodb.toDynamoDBJson($util.autoId())))
  #end 

Upvotes: 2

Related Questions