forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping-template.ts
122 lines (107 loc) · 3.93 KB
/
mapping-template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { readFileSync } from 'fs';
import { AttributeValues, KeyCondition, PrimaryKey } from './key';
/**
* MappingTemplates for AppSync resolvers
*/
export abstract class MappingTemplate {
/**
* Create a mapping template from the given string
*/
public static fromString(template: string): MappingTemplate {
return new StringMappingTemplate(template);
}
/**
* Create a mapping template from the given file
*/
public static fromFile(fileName: string): MappingTemplate {
return new StringMappingTemplate(readFileSync(fileName).toString('utf-8'));
}
/**
* Mapping template for a result list from DynamoDB
*/
public static dynamoDbResultList(): MappingTemplate {
return this.fromString('$util.toJson($ctx.result.items)');
}
/**
* Mapping template for a single result item from DynamoDB
*/
public static dynamoDbResultItem(): MappingTemplate {
return this.fromString('$util.toJson($ctx.result)');
}
/**
* Mapping template to scan a DynamoDB table to fetch all entries
*/
public static dynamoDbScanTable(): MappingTemplate {
return this.fromString('{"version" : "2017-02-28", "operation" : "Scan"}');
}
/**
* Mapping template to query a set of items from a DynamoDB table
*
* @param cond the key condition for the query
*/
public static dynamoDbQuery(cond: KeyCondition, indexName?: string): MappingTemplate {
return this.fromString(`{"version" : "2017-02-28", "operation" : "Query", ${indexName ? `"index" : "${indexName}", ` : ''}${cond.renderTemplate()}}`);
}
/**
* Mapping template to get a single item from a DynamoDB table
*
* @param keyName the name of the hash key field
* @param idArg the name of the Query argument
*/
public static dynamoDbGetItem(keyName: string, idArg: string): MappingTemplate {
return this.fromString(`{"version": "2017-02-28", "operation": "GetItem", "key": {"${keyName}": $util.dynamodb.toDynamoDBJson($ctx.args.${idArg})}}`);
}
/**
* Mapping template to delete a single item from a DynamoDB table
*
* @param keyName the name of the hash key field
* @param idArg the name of the Mutation argument
*/
public static dynamoDbDeleteItem(keyName: string, idArg: string): MappingTemplate {
return this.fromString(`{"version": "2017-02-28", "operation": "DeleteItem", "key": {"${keyName}": $util.dynamodb.toDynamoDBJson($ctx.args.${idArg})}}`);
}
/**
* Mapping template to save a single item to a DynamoDB table
*
* @param key the assigment of Mutation values to the primary key
* @param values the assignment of Mutation values to the table attributes
*/
public static dynamoDbPutItem(key: PrimaryKey, values: AttributeValues): MappingTemplate {
return this.fromString(`
${values.renderVariables()}
{
"version": "2017-02-28",
"operation": "PutItem",
${key.renderTemplate()},
${values.renderTemplate()}
}`);
}
/**
* Mapping template to invoke a Lambda function
*
* @param payload the VTL template snippet of the payload to send to the lambda.
* If no payload is provided all available context fields are sent to the Lambda function
* @param operation the type of operation AppSync should perform on the data source
*/
public static lambdaRequest(payload: string = '$util.toJson($ctx)', operation: string = 'Invoke'): MappingTemplate {
return this.fromString(`{"version": "2017-02-28", "operation": "${operation}", "payload": ${payload}}`);
}
/**
* Mapping template to return the Lambda result to the caller
*/
public static lambdaResult(): MappingTemplate {
return this.fromString('$util.toJson($ctx.result)');
}
/**
* this is called to render the mapping template to a VTL string
*/
public abstract renderTemplate(): string;
}
class StringMappingTemplate extends MappingTemplate {
constructor(private readonly template: string) {
super();
}
public renderTemplate() {
return this.template;
}
}