5

I tried to add timestamp automatically when I create some post. But it is not working the example of appsync resolver-context-reference.

https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference.html#time-helpers-in-util-time

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key": {
        "id" : $util.dynamodb.toDynamoDBJson($util.autoId())
    },

    #set( $myfoo = $util.dynamodb.toMapValues($ctx.args) )
    #set( $myFoo.version = $util.dynamodb.toNumber(1) )
    #set( $myFoo.timestamp = $util.time.nowISO8601() )

    "attributeValues" : $util.toJson($myFoo)
}
3
  • Are you trying to set the timestamp while writing to the DB or upon returning the values? Looks likes the latter. Commented May 7, 2018 at 2:00
  • @Vladimir It is for while writing the DB. Commented May 8, 2018 at 1:22
  • Got it. See my answer then. Commented May 8, 2018 at 1:52

3 Answers 3

7

This is a working example of what you're looking to do (taken from my AppSync API resolver). Note the "messageId" and "createdDate" attributes. That's how you can add a date while writing to DDB.

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "eventId": $util.dynamodb.toDynamoDBJson($ctx.args.input.eventId),
    "messageId": $util.dynamodb.toDynamoDBJson("$util.time.nowISO8601()$util.autoId()"),
  },
  "attributeValues": {
    "message": $util.dynamodb.toDynamoDBJson($ctx.args.input.message),
    "createdDate": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601())
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is it work correctly as "$util.time.nowISO8601()$util.autoId()"?
Yes. You can use additional values for sorting and range purposes as well. Up to you. Here's a good guide to follow: docs.aws.amazon.com/amazondynamodb/latest/developerguide/…
I was strugging on how to pass the current itmestamp to dynamodb to fetch a result and found this post and used this --> $util.dynamodb.toDynamoDBJson($util.time.nowISO8601()) and it worked like charm. Thanks for the code snippet. You saved my day.
5

Change your resolver to the following

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key": {
        "id" : $util.dynamodb.toDynamoDBJson($util.autoId())
    },

    #set( $myFoo = $util.dynamodb.toMapValues($ctx.args) )
    #set( $myFoo.version = $util.dynamodb.toNumber(1) )
    #set( $myFoo.timestamp = $util.dynamodb.toDynamoDB($util.time.nowISO8601()) )

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

Also note the call to $util.time.nowISO8601() is now wrapped with $util.dynamodb.toDynamoDB()

Comments

1

For dynamoDB, String should change to dynamodb type through $util. Thus, it would be work after changing time string to dynamoDB type.

#set( $myFoo.timestamp = $util.time.nowISO8601() )
=>  
#set( $myFoo.timestamp = $util.dynamodb.toDynamoDB($util.time.nowISO8601())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.