Created
July 2, 2019 11:13
-
-
Save praisegeek/66ca5dcb9a5f6b2a82cb4bfbd0bbf243 to your computer and use it in GitHub Desktop.
Velocity Snippets for VSCode.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Get Item By Id": { | |
"prefix": "GetItem", | |
"body": [ | |
"{", | |
" \"version\": \"2017-02-28\",", | |
" \"operation\": \"GetItem\",", | |
" \"key\": {", | |
" \"id\": \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.id),", | |
" }", | |
"}" | |
], | |
"description": "Get Item By Id" | |
}, | |
"List Items": { | |
"prefix": "Scan", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Scan\",", | |
" ## Add 'limit' and 'nextToken' arguments to this field in your schema to implement pagination. **", | |
" ## \"limit\": \\$util.defaultIfNull(\\${ctx.args.limit}, 20),", | |
" ## \"nextToken\": \\$util.toJson(\\$util.defaultIfNullOrBlank(\\$ctx.args.nextToken, null))", | |
"}" | |
], | |
"description": "List Items" | |
}, | |
"Put Item": { | |
"prefix": "PutItem", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"PutItem\",", | |
" \"key\" : {", | |
" ## If object \"id\" should come from GraphQL arguments, change to \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.id)", | |
" \"id\": \\$util.dynamodb.toDynamoDBJson(\\$util.autoId()),", | |
" },", | |
" \"attributeValues\" : \\$util.dynamodb.toMapValuesJson(\\$ctx.args)", | |
"}" | |
], | |
"description": "Put Item" | |
}, | |
"Put Item With S3": { | |
"prefix": "PutItem+S3", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"PutItem\",", | |
" \"key\" : {", | |
" \"id\" : { \"S\" : \"\\${util.autoId()}\" }", | |
" },", | |
" #set( \\$attribs = \\$util.dynamodb.toMapValues(\\$ctx.args) )", | |
" #if(\\$util.isNull(\\$ctx.args.file.version))", | |
" #set( \\$attribs.file = \\$util.dynamodb.toS3Object(\\$ctx.args.file.key, \\$ctx.args.file.bucket, \\$ctx.args.file.region))", | |
" #else", | |
" #set( \\$attribs.file = \\$util.dynamodb.toS3Object(\\$ctx.args.file.key, \\$ctx.args.file.bucket, \\$ctx.args.file.region, \\$ctx.args.file.version))", | |
" #end", | |
" \"attributeValues\" : \\$util.toJson(\\$attribs)", | |
"}" | |
], | |
"description": "Put Item With S3" | |
}, | |
"Delete Item by ID": { | |
"prefix": "DeleteItem", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"DeleteItem\",", | |
" \"key\" : {", | |
" ## If your table's hash key is not named 'id', update it here. **", | |
" \"id\" : { \"S\" : \"\\${ctx.args.id}\" }", | |
" ## If your table has a sort key, add it as an item here. **", | |
" }", | |
"}" | |
], | |
"description": "Delete Item by ID" | |
}, | |
"Simple Query": { | |
"prefix": "Query", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"query\" : {", | |
" ## Provide a query expression. **", | |
" \"expression\": \"id = :id\",", | |
" \"expressionValues\" : {", | |
" \":id\" : \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.id)", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Simple Query" | |
}, | |
"Query with greater than expression": { | |
"prefix": "Query with > expression", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"query\" : {", | |
" ## Provide a query expression. **", | |
" \"expression\": \"id = :id and createdAt > :createdAt\",", | |
" \"expressionValues\" : {", | |
" \":id\" : {", | |
" \"S\" : \"\\${ctx.args.id}\"", | |
" },", | |
" \":createdAt\": {", | |
" \"S\": \"\\${ctx.args.createdAt}\"", | |
" }", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Simple Query with greater than expression" | |
}, | |
"Query with filter on index expression": { | |
"prefix": "Query with filter expression", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"index\" : \"name-index\",", | |
" \"query\" : {", | |
" \"expression\": \"#name = :name\",", | |
" \"expressionNames\" : {", | |
" \"#name\" : \"name\"", | |
" },", | |
" \"expressionValues\" : {", | |
" \":name\" : {", | |
" \"S\" : \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.name)", | |
" }", | |
" }", | |
" },", | |
" \"filter\" : {", | |
" \"expression\" : \"contains(#city, :city)\",", | |
" \"expressionNames\" : {", | |
" \"#city\" : \"city\"", | |
" },", | |
" \"expressionValues\" : {", | |
" \":city\" : \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.city)", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Query with filter on index expression" | |
}, | |
"Query with contains expression": { | |
"prefix": "Query with contains expression", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"query\" : {", | |
" ## Provide a query expression. **", | |
" \"expression\": \"id = :id\",", | |
" \"expressionValues\" : {", | |
" \":id\" : {", | |
" \"S\" : \"\\${ctx.args.id}\"", | |
" }", | |
" }", | |
" },", | |
" \"filter\": {", | |
" \"expression\": \"contains(tags, :tag)\",", | |
" \"expressionValues\" : {", | |
" \":tag\": {", | |
" \"S\": \"\\${ctx.args.tag}\"", | |
" }", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Query with contains expression" | |
}, | |
"Query items created today": { | |
"prefix": "Query items created today|date", | |
"body": [ | |
"#set( \\$todayString = \\$util.time.nowISO8601().substring(0, 10) )", | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"query\" : {", | |
" ## Provide a query expression. **", | |
" \"expression\": \"id = :id and begins_with(createdAt, :today)\",", | |
" \"expressionValues\" : {", | |
" \":id\" : {", | |
" \"S\" : \"\\${ctx.args.id}\"", | |
" },", | |
" \":today\": {", | |
" \"S\": \"\\$todayString\"", | |
" }", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Query items created today" | |
}, | |
"Simple Query with pagination": { | |
"prefix": "Query with pagination", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Query\",", | |
" \"query\" : {", | |
" ## Provide a query expression. **", | |
" \"expression\": \"id = :id\",", | |
" \"expressionValues\" : {", | |
" \":id\" : {", | |
" \"S\" : \"\\${ctx.args.id}\"", | |
" }", | |
" }", | |
" },", | |
" ## Add 'limit' and 'nextToken' arguments to this field in your schema to implement pagination. **", | |
" \"limit\": \\$util.defaultIfNull(\\${ctx.args.limit}, 20),", | |
" \"nextToken\": \\$util.toJson(\\$util.defaultIfNullOrBlank(\\$ctx.args.nextToken, null))", | |
"}" | |
], | |
"description": "Simple Query with pagination" | |
}, | |
"Batch Get Items": { | |
"prefix": "BatchGetItems", | |
"body": [ | |
"#set(\\$ids = [])", | |
"#foreach(\\$id in \\${ctx.args.ids})", | |
" #set(\\$map = {})", | |
" \\$util.qr(\\$map.put(\"id\", \\$util.dynamodb.toString(\\$id)))", | |
" \\$util.qr(\\$ids.add(\\$map))", | |
"#end", | |
"", | |
"{", | |
" \"version\" : \"2018-05-29\",", | |
" \"operation\" : \"BatchGetItem\",", | |
" \"tables\" : {", | |
" ## Note: \\${sampleTableName} was defined in substitutions field.", | |
" \"\\${sampleTableName}\": {", | |
" \"keys\": \\$util.toJson(\\$ids),", | |
" \"consistentRead\": true", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Batch Get Items" | |
}, | |
"Batch Put Items": { | |
"prefix": "BatchPutItems", | |
"body": [ | |
"#set(\\$postsdata = [])", | |
"#foreach(\\$item in \\${ctx.args.posts})", | |
" \\$util.qr(\\$postsdata.add(\\$util.dynamodb.toMapValues(\\$item)))", | |
"#end", | |
"", | |
" ## Note: \\${sampleTableName} was defined in substitutions field.", | |
"{", | |
" \"version\" : \"2018-05-29\",", | |
" \"operation\" : \"BatchPutItem\",", | |
" \"tables\" : {", | |
" \"\\${sampleTableName}\": \\$utils.toJson(\\$postsdata)", | |
" }", | |
"}" | |
], | |
"description": "Batch Put Items" | |
}, | |
"Batch Delete Items": { | |
"prefix": "BatchDeleteItems", | |
"body": [ | |
"#set(\\$ids = [])", | |
"#foreach(\\$id in \\${ctx.args.ids})", | |
" #set(\\$map = {})", | |
" \\$util.qr(\\$map.put(\"id\", \\$util.dynamodb.toString(\\$id)))", | |
" \\$util.qr(\\$ids.add(\\$map))", | |
"#end", | |
"", | |
"## Note: \\${sampleTableName} was defined in substitutions field.", | |
"{", | |
" \"version\" : \"2018-05-29\",", | |
" \"operation\" : \"BatchDeleteItem\",", | |
" \"tables\" : {", | |
" \"\\${sampleTableName}\": \\$util.toJson(\\$ids)", | |
" }", | |
"}" | |
], | |
"description": "Batch Delete Items" | |
}, | |
"Cognito group access": { | |
"prefix": "Cognito Group Access Authorization", | |
"body": [ | |
"#set(\\$expression = \"\")", | |
"#set(\\$expressionValues = {})", | |
"#foreach(\\$group in \\$ctx.identity.claims.get(\"cognito:groups\"))", | |
" #set( \\$expression = \"\\${expression} contains(groupsCanAccess, :var\\$foreach.count )\" )", | |
" #set( \\$val = {})", | |
" #set( \\$test = \\$val.put(\"S\", \\$group))", | |
" #set( \\$values = \\$expressionValues.put(\":var\\$foreach.count\", \\$val))", | |
" #if ( \\$foreach.hasNext )", | |
" #set( \\$expression = \"\\${expression} OR\" )", | |
" #end", | |
"#end", | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"Scan\",", | |
" \"filter\":{", | |
" \"expression\": \"\\$expression\",", | |
" \"expressionValues\": \\$utils.toJson(\\$expressionValues)", | |
" }", | |
"}" | |
], | |
"description": "Cognito group access" | |
}, | |
"Put Item with cognito identity": { | |
"prefix": "PutItem with Owner Identity", | |
"body": [ | |
"#set( \\$attributes = \\$util.dynamodb.toMapValues(\\$ctx.args) )", | |
"\\$util.qr(\\$attributes.put(\"$Identity\", \\$ctx.identity.cognitoIdentityId))", | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"PutItem\",", | |
" \"key\" : {", | |
" \"id\" : { \"S\" : \"\\${context.args.id}\" }", | |
" },", | |
" \"attributeValues\": \\$util.toJson(\\$attributes)", | |
"}" | |
], | |
"description": "Put Item with cognito identity" | |
}, | |
"Delete Item with cognito identity": { | |
"prefix": "DeleteItem with Owner Identity", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"DeleteItem\",", | |
" \"key\" : {", | |
" \"id\" : { \"S\" : \"\\${context.args.id}\" }", | |
" },", | |
" \"condition\" : {", | |
" \"expression\" : \"contains($Owner,:expectedOwner)\",", | |
" \"expressionValues\" : {", | |
" \":expectedOwner\" : { \"S\" : \"\\${context.identity.username}\" }", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Delete Item with cognito identity" | |
}, | |
"Put Item offline enabled": { | |
"prefix": "PutItem Offline", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"PutItem\",", | |
" \"key\": {", | |
" \"id\": { \"S\" : \"\\$utils.autoId()\"}", | |
" },", | |
" #set( \\$attribs = \\$util.dynamodb.toMapValues(\\$ctx.args) )", | |
" \\$util.qr(\\$attribs.remove(\"relayState\"))", | |
" #set( \\$attribs.version = { \"N\" : 1 } )", | |
" \"attributeValues\" : \\$util.toJson(\\$attribs)", | |
"}" | |
], | |
"description": "Put Item offline enabled" | |
}, | |
"Conditional Update with version": { | |
"prefix": "UpdateItem with version", | |
"body": [ | |
"{", | |
" \"version\" : \"2017-02-28\",", | |
" \"operation\" : \"UpdateItem\",", | |
" \"key\" : {", | |
" \"id\" : \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.id)", | |
" },", | |
"", | |
" ## Set up some space to keep track of things we're updating **", | |
" #set( \\$expNames = {} )", | |
" #set( \\$expValues = {} )", | |
" #set( \\$expSet = {} )", | |
" #set( \\$expAdd = {} )", | |
" #set( \\$expRemove = [] )", | |
"", | |
" ## Increment \"version\" by 1 **", | |
" \\$!{expAdd.put(\"version\", \":one\")}", | |
" \\$!{expValues.put(\":one\", \\$util.dynamodb.toDynamoDB(1))}", | |
"", | |
" ## Iterate through each argument, skipping \"id\" and \"expectedVersion\" **", | |
" #foreach( \\$entry in \\$util.map.copyAndRemoveAllKeys(\\$ctx.args, [\"id\",\"expectedVersion\"]).entrySet() )", | |
" #if( \\$util.isNull(\\$entry.value) )", | |
" ## If the argument is set to \"null\", then remove that attribute from the item in DynamoDB **", | |
"", | |
" #set( \\$discard = \\${expRemove.add(\"#\\${entry.key}\")} )", | |
" \\$!{expNames.put(\"#\\${entry.key}\", \"\\${entry.key}\")}", | |
" #else", | |
" ## Otherwise set (or update) the attribute on the item in DynamoDB **", | |
"", | |
" \\$!{expSet.put(\"#\\${entry.key}\", \":\\${entry.key}\")}", | |
" \\$!{expNames.put(\"#\\${entry.key}\", \"\\${entry.key}\")}", | |
" \\$!{expValues.put(\":\\${entry.key}\", \\$util.dynamodb.toDynamoDB(\\$entry.value))}", | |
" #end", | |
" #end", | |
"", | |
" ## Start building the update expression, starting with attributes we're going to SET **", | |
" #set( \\$expression = \"\" )", | |
" #if( !\\${expSet.isEmpty()} )", | |
" #set( \\$expression = \"SET\" )", | |
" #foreach( \\$entry in \\$expSet.entrySet() )", | |
" #set( \\$expression = \"\\${expression} \\${entry.key} = \\${entry.value}\" )", | |
" #if ( \\$foreach.hasNext )", | |
" #set( \\$expression = \"\\${expression},\" )", | |
" #end", | |
" #end", | |
" #end", | |
"", | |
" ## Continue building the update expression, adding attributes we're going to ADD **", | |
" #if( !\\${expAdd.isEmpty()} )", | |
" #set( \\$expression = \"\\${expression} ADD\" )", | |
" #foreach( \\$entry in \\$expAdd.entrySet() )", | |
" #set( \\$expression = \"\\${expression} \\${entry.key} \\${entry.value}\" )", | |
" #if ( \\$foreach.hasNext )", | |
" #set( \\$expression = \"\\${expression},\" )", | |
" #end", | |
" #end", | |
" #end", | |
"", | |
" ## Continue building the update expression, adding attributes we're going to REMOVE **", | |
" #if( !\\${expRemove.isEmpty()} )", | |
" #set( \\$expression = \"\\${expression} REMOVE\" )", | |
"", | |
" #foreach( \\$entry in \\$expRemove )", | |
" #set( \\$expression = \"\\${expression} \\${entry}\" )", | |
" #if ( \\$foreach.hasNext )", | |
" #set( \\$expression = \"\\${expression},\" )", | |
" #end", | |
" #end", | |
" #end", | |
"", | |
" ## Finally, write the update expression into the document, along with any expressionNames and expressionValues **", | |
" \"update\" : {", | |
" \"expression\" : \"\\${expression}\",", | |
" #if( !\\${expNames.isEmpty()} )", | |
" \"expressionNames\" : \\$utils.toJson(\\$expNames),", | |
" #end", | |
" #if( !\\${expValues.isEmpty()} )", | |
" \"expressionValues\" : \\$utils.toJson(\\$expValues),", | |
" #end", | |
" },", | |
"", | |
" \"condition\" : {", | |
" \"expression\" : \"version = :expectedVersion\",", | |
" \"expressionValues\" : {", | |
" \":expectedVersion\" : \\$util.dynamodb.toDynamoDBJson(\\$ctx.args.expectedVersion)", | |
" }", | |
" }", | |
"}" | |
], | |
"description": "Conditional Update with version" | |
}, | |
"Return single item": { | |
"prefix": "Return single item", | |
"body": [ | |
"## Pass back the result from DynamoDB. **", | |
"\\$util.toJson(\\$ctx.result)" | |
], | |
"description": "return single item" | |
}, | |
"Return a list of results": { | |
"prefix": "Return list", | |
"body": ["\\$util.toJson(\\$ctx.result.items)"], | |
"description": "Return a list of results" | |
}, | |
"Return paginated results": { | |
"prefix": "Return paginated results", | |
"body": [ | |
"{", | |
" \"items\": \\$util.toJson(\\$ctx.result.items),", | |
" \"nextToken\": \\$util.toJson(\\$util.defaultIfNullOrBlank(\\$context.result.nextToken, null))", | |
"}" | |
], | |
"description": "Return paginated results" | |
}, | |
"Return batch response": { | |
"prefix": "Return Batch Response", | |
"body": [ | |
"## Batch results are available in $context.result.data as name of the table (TABLENAME below)", | |
"## Read more: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html", | |
"", | |
"\\$util.toJson(\\$ctx.result.data.${1:TABLENAME})" | |
], | |
"description": "Return batch response" | |
}, | |
"Return batch response with error handling": { | |
"prefix": "Return Batch Response + Error Handling", | |
"body": [ | |
"## Batch results are available in \\$context.result.data as name of the table (TABLENAME below)", | |
"## If there was an error with invocation there may be partial results in \\$ctx.error object", | |
"## You can append an error for that field in GraphQL response along with successful data", | |
"## Read more: https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-dynamodb-batch.html", | |
"", | |
"#if(\\$ctx.error)", | |
" \\$utils.appendError(\\$ctx.error.message, \\$ctx.error.message)", | |
"#end", | |
"\\$utils.toJson(\\$ctx.result.data)" | |
], | |
"description": "Return batch response with error handling" | |
}, | |
"Return only my owned records": { | |
"prefix": "Return Owner Records", | |
"body": [ | |
"#set(\\$myResults = [])", | |
"#foreach(\\$item in \\$ctx.result.items)", | |
" ## For Cognito User Pools use \\$ctx.identity.username instead", | |
" #if(\\$item.Owner == \\$ctx.identity.cognitoIdentityId)", | |
" #set(\\$added = \\$myResults.add(\\$item))", | |
" #end", | |
"#end", | |
"\\$utils.toJson(\\$myResults)" | |
], | |
"description": "Return only own records" | |
}, | |
"Return a single record if public": { | |
"prefix": "Return Single Record If Public", | |
"body": [ | |
"#if(\\$ctx.result.public == 'yes')", | |
" \\$utils.toJson(\\$ctx.result)", | |
"#else", | |
" \\$utils.unauthorized()", | |
"#end" | |
], | |
"description": "Return a single record if public" | |
}, | |
"Return record if viewable by my groups": { | |
"prefix": "Return Record If Viewable By My Groups", | |
"body": [ | |
"#set(\\$permissions = \\$ctx.result.GroupsCanAccess)", | |
"#set(\\$claimPermissions = \\$ctx.identity.claims.get(\"cognito:groups\"))", | |
"", | |
"#foreach(\\$per in \\$permissions)", | |
" #foreach(\\$cgroups in \\$claimPermissions)", | |
" #if(\\$cgroups == \\$per)", | |
" #set(\\$hasPermission = true)", | |
" #end", | |
" #end", | |
"#end", | |
"", | |
"#if(\\$hasPermission)", | |
" \\$utils.toJson(\\$ctx.result)", | |
"#else", | |
" \\$utils.unauthorized()", | |
"#end" | |
], | |
"description": "Return record if viewable by my groups" | |
}, | |
"Return a list of public records": { | |
"prefix": "Return A List Of Public Records", | |
"body": [ | |
"#set(\\$publicRecords = [])", | |
"#foreach(\\$item in \\$ctx.result.items)", | |
" #if(\\$item.public == 'yes')", | |
" #set(\\$added = \\$publicRecords.add(\\$item))", | |
" #end", | |
"#end", | |
"\\$utils.toJson(\\$publicRecords)" | |
], | |
"description": "Return a list of public records" | |
}, | |
"Return Offline enabled response": { | |
"prefix": "Return Offline Enabled Response", | |
"body": [ | |
"\\$util.qr(\\$context.result.put(\"relayState\", \"\\$context.arguments.relayState\"))", | |
"\\$util.toJson(\\$context.result)" | |
], | |
"description": "Return Offline enabled response" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment