Is it possible to get query/scan results in ascending/descending order based on the timestamp?
type Todos @model @auth(rules: [{ allow: owner }]) {
id: ID!
owner: String
timestamp: AWSTimestamp
todo: String
createTime: AWSDateTime
color: Int
completed: Boolean
}
I tried to create a secondary global index with "id" as the primary key and "timestamp" as the sort key. The amplify cli automatically creates the resolvers based on the models above. Here is the modification I tried to make to the resolver but it still doesn't respond with sorted results.
#set( $limit = $util.defaultIfNull($context.args.limit, 10) )
{
"version": "2017-02-28",
"operation": "Scan",
"filter":
#if( $context.args.filter )
$util.transform.toDynamoDBFilterExpression($ctx.args.filter)
#else
null
#end,
## https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html#aws-appsync-resolver-mapping-template-reference-dynamodb-scan
## "index" : "id-timestamp-index",
##
"limit": $limit,
"nextToken":
#if( $context.args.nextToken )
"$context.args.nextToken"
#else
null
#end
}
similar issues have the following:
"scanIndexForward" : true,
But this only works when the operation is query
"operation" : "Query"
this is followed by an error that point to the "query" key.
"query" : {
"expression" : "some expression",
"expressionNames" : {
"#foo" : "foo"
},
"expressionValues" : {
":bar" : ... typed value
}
}
Looking at the documents lead me to the Key Conditions and Dynamo DB Reference parts, but it isn't helpful.
I'm just trying to get all todos written by the user in either ascending/descending order based on the timestamp.
The problem is that I was using the amplify cli that asks for a graphql schema and creates it's own resolvers.
This is what it produced on its own.
{
"version": "2017-02-28",
"operation": "Scan",
"filter": #if($context.args.filter) $util.transform.toDynamoDBFilterExpression($ctx.args.filter) #else null #end,
"limit": $util.defaultIfNull($ctx.args.limit, 20),
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)),
}
Helpful Resources:
First, if you are using the amplify cli, go to the AWS console and create a global secondary index where the primary key is the owner and the timestamp as the sort key. For this example, I will name the seconday index as todos-owner-timestamp-index.
Lastly, find the resolver that you will use to query/scan and replace it with the following. Use the resources above to look at the query language for dynamodb.
{
"version" : "2017-02-28",
"operation" : "Query",
"query" : {
"expression" : "#owner = :owner AND #timestamp >= :timestamp",
"expressionNames" : {
"#owner": "owner",
"#timestamp": "timestamp"
},
"expressionValues" : {
":owner": { "S" : "$context.identity.username" },
":timestamp": { "N" : 0 }
}
},
"index" : "todos-owner-timestamp-index",
"nextToken" : $util.toJson($util.defaultIfNullOrEmpty($ctx.args.nextToken, null)),
"limit" : $util.defaultIfNull($context.args.limit, 10),
"scanIndexForward" : false,
"consistentRead" : false,
"select" : "ALL_ATTRIBUTES",
"filter" : #if($context.args.filter) $util.transform.toDynamoDBFilterExpression($ctx.args.filter) #else null #end,
}
use scanIndexForward to sort in ascending or descending order
Most helpful comment
The problem is that I was using the amplify cli that asks for a graphql schema and creates it's own resolvers.
This is what it produced on its own.
Helpful Resources:
Solution
First, if you are using the amplify cli, go to the AWS console and create a global secondary index where the primary key is the owner and the timestamp as the sort key. For this example, I will name the seconday index as
todos-owner-timestamp-index.Lastly, find the resolver that you will use to query/scan and replace it with the following. Use the resources above to look at the query language for dynamodb.
use
scanIndexForwardto sort in ascending or descending order