-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeep_for_now.ts
More file actions
129 lines (117 loc) · 3.8 KB
/
keep_for_now.ts
File metadata and controls
129 lines (117 loc) · 3.8 KB
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
123
124
125
126
127
128
129
export interface RequestMappingTemplate {
toTemplate(): string
}
export class RawRequestMappingTemplate implements RequestMappingTemplate {
template: string
constructor(template: string) {
this.template = template
}
toTemplate() {
return this.template
}
}
export enum RequestMappingTemplateVersion {
V2017 = '2017-02-28',
V2018 = '2018-05-29',
}
export enum DynamoDBOperation {
'GetItem' = 'GetItem',
'Scan' = 'Scan',
}
export interface RequestMapKeyProps {
partitionKeyName: string
partitionKeyArgument?: string
sortKeyName?: string
sortKeyArgument?: string
}
export class RequestMapKey {
partitionKeyName: string
partitionKeyArgument: string
sortKeyName?: string
sortKeyArgument?: string
constructor(props: RequestMapKeyProps) {
this.partitionKeyName = props.partitionKeyName
this.partitionKeyArgument = props.partitionKeyArgument || props.partitionKeyName
this.sortKeyName = props.sortKeyName
this.sortKeyArgument = props.sortKeyArgument || props.sortKeyName
}
toTemplate() {
let key = `"${this.partitionKeyName}"`
let value = `$util.dynamodb.toDynamoDBJson($ctx.args.${this.partitionKeyArgument})`
const partitionLine = `${key}: ${value}${this.sortKeyName ? ',\n' : ''}`
key = `"${this.sortKeyName}"`
value = `$util.dynamodb.toDynamoDBJson($ctx.args.${this.sortKeyArgument})`
const sortLine = `${key}: ${value}`
return `{ ${partitionLine}${this.sortKeyName ? sortLine : ''} }`
}
}
export interface GetItemRequestMappingTemplateProps {
key: string | RequestMapKey
consistentRead?: boolean
version?: RequestMappingTemplateVersion
}
export class GetItemRequestMappingTemplate implements RequestMappingTemplate {
version: RequestMappingTemplateVersion
operation = DynamoDBOperation.GetItem
key: string
consistentRead: boolean
constructor(props: GetItemRequestMappingTemplateProps) {
this.version = props.version || RequestMappingTemplateVersion.V2018
this.key = props.key instanceof RequestMapKey ? props.key.toTemplate() : props.key
this.consistentRead = props.consistentRead || false
}
toTemplate() {
return `{
"version": "${this.version}",
"operation": "${this.operation}",
"key": ${this.key},
"consistentRead": ${this.consistentRead}
}`
}
}
export interface ScanRequestMappingTemplateProps {
consistentRead?: boolean
defaultLimit?: number
index?: string
limit?: number
limitArg?: string
nextTokenArg?: string
version?: RequestMappingTemplateVersion
}
export class ScanRequestMappingTemplate implements RequestMappingTemplate {
version: RequestMappingTemplateVersion
operation = DynamoDBOperation.Scan
consistentRead?: boolean
defaultLimit?: number
index?: string
limit?: number
limitArg?: string
nextTokenArg?: string
constructor(props: ScanRequestMappingTemplateProps = {}) {
this.version = props.version || RequestMappingTemplateVersion.V2018
this.consistentRead = props.consistentRead
this.defaultLimit = props.defaultLimit
this.index = props.index
this.limit = props.limit
this.limitArg = props.limitArg
this.nextTokenArg = props.nextTokenArg
}
toTemplate() {
let limitLine = ''
if (this.limitArg) {
const defaultLimit = this.defaultLimit || 20
limitLine = `"limit": $util.defaultIfNull($ctx.args.${this.limitArg}, ${defaultLimit})`
} else if (this.limit) {
limitLine = `"limit": ${this.limit}`
}
const nextTokenValue = `$util.toJson($util.defaultIfNullOrEmpty($ctx.args.${this.nextTokenArg}, null))`
return `{
"version": "${this.version}",
"operation": "${this.operation}",
${this.consistentRead ? ` "consistentRead": ${this.consistentRead},` : ''}
${this.index ? ` "index": "${this.index}",` : ''}
${limitLine ? ` ${limitLine},` : ''}
${this.nextTokenArg ? ` "nextToken": ${nextTokenValue},` : ''}
}`
}
}