-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynamodb-utils.js
112 lines (92 loc) · 2.2 KB
/
dynamodb-utils.js
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
export const dynamodbUtils = {
toDynamoDB: function(value) {
if (typeof (value) === "number") {
return this.toNumber(value);
} else if (typeof (value) === "string") {
return this.toString(value);
} else if (typeof (value) === "boolean") {
return this.toBoolean(value);
} else if (typeof (value) === "object") {
if (value.length !== undefined) {
return this.toList(value);
} else {
return this.toMap(value);
}
} else {
throw new Error(`Not implemented for ${value}`);
}
},
toString: function(value) {
if (value === null) { return null; };
return { S: value };
},
toStringSet: function(value) {
if (value === null) { return null; };
return { SS: value };
},
toNumber: function(value) {
if (value === null) { return null; };
return { N: value };
},
toNumberSet: function(value) {
if (value === null) { return null; };
return { NS: value };
},
toBinary: function(value) {
if (value === null) { return null; };
return { B: value };
},
toBinarySet: function(value) {
if (value === null) { return null; };
return { BS: value };
},
toBoolean: function(value) {
if (value === null) { return null; };
return { BOOL: value };
},
toNull: function() {
return { NULL: null };
},
toList: function(values) {
let out = [];
for (const value of values) {
out.push(this.toDynamoDB(value));
}
return { L: out }
},
toMap: function(mapping) {
return { M: this.toMapValues(mapping) };
},
toMapValues: function(mapping) {
let out = {};
for (const [k, v] of Object.entries(mapping)) {
out[k] = this.toDynamoDB(v);
}
return out;
},
toS3Object: function(key, bucket, region, version) {
let payload;
if (version === undefined) {
payload = {
s3: {
key,
bucket,
region,
}
};
} else {
payload = {
s3: {
key,
bucket,
region,
version,
}
};
};
return this.toString(JSON.stringify(payload));
},
fromS3ObjectJson: function(value) {
throw new Error("not implemented");
},
}