Skip to content

Instantly share code, notes, and snippets.

@bboure
Created April 15, 2021 10:04
Show Gist options
  • Save bboure/de811ebdfc805efd7bd9a62ffffed65d to your computer and use it in GitHub Desktop.
Save bboure/de811ebdfc805efd7bd9a62ffffed65d to your computer and use it in GitHub Desktop.
AppSync advanced filter
#set($expression = "#PK = :PK")
#set($expressionNames = {"#PK": "PK", "#SK": "SK"})
#set($expressionValues = {":PK": $util.dynamodb.toString("ACCOUNT#${ctx.args.accountId}")})
#if(!$util.isNull($ctx.args.fromDate) && $util.isNull($ctx.args.toDate))
#set($expression = "${expression} and #SK >= :fromDate")
$util.qr($expressionValues.put(":fromDate", $util.dynamodb.toString("TX#${ctx.args.fromDate}")))
#elseif($util.isNull($ctx.args.fromDate) && !$util.isNull($ctx.args.toDate))
#set($expression = "${expression} and #SK <= :toDate")
$util.qr($expressionValues.put(":toDate", $util.dynamodb.toString("TX#${ctx.args.toDate}")))
#elseif(!$util.isNull($ctx.args.fromDate) && !$util.isNull($ctx.args.toDate))
#set($expression = "${expression} and #SK BETWEEN :fromDate AND :toDate")
$util.qr($expressionValues.put(":fromDate", $util.dynamodb.toString("TX#${ctx.args.fromDate}")))
$util.qr($expressionValues.put(":toDate", $util.dynamodb.toString("TX#${ctx.args.toDate}")))
#else
#set($expression = "${expression} and begins_with(#SK, :SK)")
$util.qr($expressionValues.put(":SK", $util.dynamodb.toString("TX#")))
#end
{
"version" : "2017-02-28",
"operation" : "Query",
"limit": $util.toJson($util.defaultIfNull($ctx.args.limit, 10)),
"scanIndexForward": false,
"nextToken": $util.toJson($ctx.args.nextToken),
"query" : {
"expression": "${expression}",
"expressionNames" : $util.toJson($expressionNames),
"expressionValues" : $util.toJson($expressionValues)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment