forked from winglang/voting-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb.w
277 lines (245 loc) · 7.4 KB
/
dynamodb.w
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
bring "@cdktf/provider-aws" as tfaws;
bring aws;
bring cloud;
bring util;
// --- dynamodb ---
enum AttributeType {
String,
Number, // note: DynamoDB requires you to provide the `value` as a string
Binary,
}
struct Attribute {
type: AttributeType;
value: Json;
}
class Util {
extern "./util.js" static inflight jsonToMutArray(json: Json): MutArray<Map<Attribute>>;
extern "./util.js" static inflight jsonToArray(json: Json): Array<Map<Attribute>>;
extern "./util.js" static inflight mutArrayToJson(json: MutArray<Map<Attribute>>): Json;
}
// TODO: https://github.com/winglang/wing/issues/3350
// typealias Item = Map<Attribute>;
struct DynamoDBTableProps {
hashKey: str;
}
class DynamoDBTableSim {
key: str;
data: cloud.Bucket;
init(props: DynamoDBTableProps) {
this.key = "data.json";
this.data = new cloud.Bucket();
this.data.addObject(this.key, "[]");
}
inflight putItem(item: Map<Attribute>) {
let items = this.data.getJson(this.key);
let itemsMut = Util.jsonToMutArray(items);
itemsMut.push(item);
this.data.putJson(this.key, Util.mutArrayToJson(itemsMut));
}
inflight getItem(map: Map<Attribute>): Map<Attribute>? {
let items = this.data.getJson(this.key);
let itemsMut = Util.jsonToMutArray(items);
for item in itemsMut {
let var matches = true;
for key in map.keys() {
let attr1 = item.get(key);
let attr2 = map.get(key);
if attr1.value != attr2.value {
matches = false;
break;
}
}
if matches {
return item;
}
}
return nil;
}
inflight scan(): Array<Map<Attribute>> {
let items = this.data.getJson(this.key);
return Util.jsonToArray(items);
}
}
class DynamoDBTableAws {
table: tfaws.dynamodbTable.DynamodbTable;
tableName: str;
hashKey: str;
init(props: DynamoDBTableProps) {
this.hashKey = props.hashKey;
this.table = new tfaws.dynamodbTable.DynamodbTable(
name: "${this.node.id}-${this.node.addr.substring(this.node.addr.length - 8)}",
billingMode: "PAY_PER_REQUEST",
hashKey: this.hashKey,
attribute: [
{
name: this.hashKey,
type: "S",
},
],
);
this.tableName = this.table.name;
}
bind(host: std.IInflightHost, ops: Array<str>) {
if let host = aws.Function.from(host) {
if ops.contains("putItem") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:PutItem"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
if ops.contains("getItem") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:GetItem"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
if ops.contains("scan") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:Scan"],
resources: [this.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
}
}
extern "./dynamo.js" inflight _putItem(tableName: str, item: Json): void;
extern "./dynamo.js" inflight _getItem(tableName: str, key: Json): Map<Map<Map<str>>>;
extern "./dynamo.js" inflight _scan(tableName: str): Map<Array<Map<Map<str>>>>;
inflight putItem(item: Map<Attribute>) {
let json = this._itemToJson(item);
this._putItem(this.tableName, json);
}
inflight getItem(key: Map<Attribute>): Map<Attribute> {
let json = this._itemToJson(key);
let result = this._getItem(this.tableName, json);
return this._rawMapToItem(result.get("Item"));
}
inflight scan(): Array<Map<Attribute>> {
let result = this._scan(this.tableName);
let rawItems = result.get("Items");
let items = MutArray<Map<Attribute>>[];
for rawItem in rawItems {
let item = this._rawMapToItem(rawItem);
items.push(item);
}
return items.copy();
}
inflight _itemToJson(item: Map<Attribute>): Json {
let json = MutJson {};
for key in item.keys() {
let attribute = item.get(key);
let attributeTypeStr = this._attributeTypeToString(attribute.type);
let innerJson = MutJson {};
innerJson.set(attributeTypeStr, attribute.value);
json.set(key, innerJson);
}
return json;
}
inflight _rawMapToItem(input: Map<Map<str>>): Map<Attribute> {
let item = MutMap<Attribute> {};
for key in input.keys() {
let attributeJson = input.get(key);
let attributeTypeStr = attributeJson.keys().at(0);
let attributeType = this._stringToAttributeType(attributeTypeStr);
let attributeValue = attributeJson.get(attributeTypeStr);
item.set(key, Attribute {
type: attributeType,
value: attributeValue,
});
}
return item.copy();
}
inflight _attributeTypeToString(type: AttributeType): str {
if type == AttributeType.String {
return "S";
} elif type == AttributeType.Number {
return "N";
} elif type == AttributeType.Binary {
return "B";
}
}
inflight _stringToAttributeType(type: str): AttributeType {
if type == "S" {
return AttributeType.String;
} elif type == "N" {
return AttributeType.Number;
} elif type == "B" {
return AttributeType.Binary;
}
}
}
class DynamoDBTable {
tableSim: DynamoDBTableSim?;
tableAws: DynamoDBTableAws?;
init(props: DynamoDBTableProps) {
let target = util.env("WING_TARGET");
if target == "sim" {
this.tableSim = new DynamoDBTableSim(props);
} elif target == "tf-aws" {
this.tableAws = new DynamoDBTableAws(props);
} else {
throw("DynamoDBTable doesn't support target '${target}'");
}
}
bind(host: std.IInflightHost, ops: Array<str>) {
// currently simulator does not require permissions
// may change with https://github.com/winglang/wing/issues/3082
if let tableAws = this.tableAws {
if let host = aws.Function.from(host) {
if ops.contains("putItem") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:PutItem"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
if ops.contains("getItem") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:GetItem"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
if ops.contains("scan") {
host.addPolicyStatements([aws.PolicyStatement {
actions: ["dynamodb:Scan"],
resources: [tableAws.table.arn],
effect: aws.Effect.ALLOW,
}]);
}
}
}
}
inflight getItem(key: Map<Attribute>): Map<Attribute>? {
assert(key.size() == 1);
if let tableSim = this.tableSim {
return tableSim.getItem(key);
}
if let tableAws = this.tableAws {
return tableAws.getItem(key);
}
throw("no table instance found for getItem");
}
inflight putItem(item: Map<Attribute>) {
if let tableSim = this.tableSim {
tableSim.putItem(item);
return;
}
if let tableAws = this.tableAws {
tableAws.putItem(item);
return;
}
throw("no table instance found for putItem");
}
inflight scan(): Array<Map<Attribute>> {
if let tableSim = this.tableSim {
return tableSim.scan();
}
if let tableAws = this.tableAws {
return tableAws.scan();
}
throw("no table instance found for scan");
}
}