-
Notifications
You must be signed in to change notification settings - Fork 11
/
converter.ts
302 lines (273 loc) · 10 KB
/
converter.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// import DynamoDB = require('../../clients/dynamodb');
import { base64ToUint8Array, base64FromUint8Array } from "../deps.ts";
import { Doc, DynamoDBSet, DynamoDBNumberValue, typeOf } from "../util.ts";
/** Formats a list. */
function formatList(data: any[], options: Doc = {}): Doc {
const list: Doc = { L: [] };
for (let i: number = 0; i < data.length; i++) {
list["L"].push(Converter.input(data[i], options));
}
return list;
}
/** Converts a number. */
function convertNumber(value: string, wrapNumbers: boolean = false): any {
return wrapNumbers ? new DynamoDBNumberValue(value) : Number(value);
}
/** Formats a map. */
function formatMap(data: Doc, options: Doc = {}): Doc {
const map: Doc = { M: {} };
for (const key in data) {
const formatted: Doc = Converter.input(data[key], options);
if (formatted !== void 0) {
map["M"][key] = formatted;
}
}
return map;
}
/** Formats a set. */
function formatSet(data: Doc, options: Doc = {}): Doc {
let values: any[] = data.values;
if (options.convertEmptyValues) {
values = filterEmptySetValues(data);
if (values.length === 0) {
return Converter.input(null);
}
}
const map: Doc = {};
switch (data.type) {
case "String":
map["SS"] = values;
break;
case "Binary":
map["BS"] = values;
break;
case "Number":
map["NS"] = values.map(function (value) {
return value.toString();
});
}
return map;
}
/** Filters empty set values. */
function filterEmptySetValues(set: Doc): any[] {
const nonEmptyValues: any[] = [];
const potentiallyEmptyTypes: Doc = {
String: true,
Binary: true,
Number: false,
};
if (potentiallyEmptyTypes[set.type]) {
for (let i: number = 0; i < set.values.length; i++) {
if (set.values[i].length === 0) {
continue;
}
nonEmptyValues.push(set.values[i]);
}
return nonEmptyValues;
}
return set.values;
}
/** aws DynamoDB req/res document converter. */
export class Converter {
/**
* Convert a JavaScript value to its equivalent DynamoDB AttributeValue type
*
* @param data [any] The data to convert to a DynamoDB AttributeValue
* @param options [map]
* @option options convertEmptyValues [Boolean] Whether to automatically
* convert empty strings, blobs,
* and sets to `null`
* @option options wrapNumbers [Boolean] Whether to return numbers as a
* NumberValue object instead of
* converting them to native JavaScript
* numbers. This allows for the safe
* round-trip transport of numbers of
* arbitrary size.
* @return [map] An object in the Amazon DynamoDB AttributeValue format
*
* @see AWS.DynamoDB.Converter.marshall AWS.DynamoDB.Converter.marshall to
* convert entire records (rather than individual attributes)
*/
static input(data: any, options: Doc = {}): Doc {
const type: string = typeOf(data);
if (type === "Object") {
return formatMap(data, options);
} else if (type === "Array") {
return formatList(data, options);
} else if (type === "Set") {
return formatSet(data, options);
} else if (type === "String") {
if (data.length === 0 && options.convertEmptyValues) {
return Converter.input(null);
}
return { S: data };
} else if (type === "Number" || type === "NumberValue") {
return { N: data.toString() };
} else if (type === "Binary") {
if (data.length === 0 && options.convertEmptyValues) {
return Converter.input(null);
}
// return { B: data };
return { B: base64FromUint8Array(data) };
} else if (type === "Boolean") {
return { BOOL: data };
} else if (type === "null") {
return { NULL: true };
} else if (type !== "undefined" && type !== "Function") {
// this value has a custom constructor
return formatMap(data, options);
}
return {};
}
/**
* Convert a JavaScript object into a DynamoDB record.
*
* @param data [any] The data to convert to a DynamoDB record
* @param options [map]
* @option options convertEmptyValues [Boolean] Whether to automatically
* convert empty strings, blobs,
* and sets to `null`
* @option options wrapNumbers [Boolean] Whether to return numbers as a
* NumberValue object instead of
* converting them to native JavaScript
* numbers. This allows for the safe
* round-trip transport of numbers of
* arbitrary size.
*
* @return [map] An object in the DynamoDB record format.
*
* @example Convert a JavaScript object into a DynamoDB record
* var marshalled = AWS.DynamoDB.Converter.marshall({
* string: 'foo',
* list: ['fizz', 'buzz', 'pop'],
* map: {
* nestedMap: {
* key: 'value',
* }
* },
* number: 123,
* nullValue: null,
* boolValue: true,
* stringSet: new DynamoDBSet(['foo', 'bar', 'baz'])
* });
*/
static marshall(data: Doc, options?: Doc): Doc {
return Converter.input(data, options).M;
}
/**
* Convert a DynamoDB AttributeValue object to its equivalent JavaScript type.
*
* @param data [map] An object in the Amazon DynamoDB AttributeValue format
* @param options [map]
* @option options convertEmptyValues [Boolean] Whether to automatically
* convert empty strings, blobs,
* and sets to `null`
* @option options wrapNumbers [Boolean] Whether to return numbers as a
* NumberValue object instead of
* converting them to native JavaScript
* numbers. This allows for the safe
* round-trip transport of numbers of
* arbitrary size.
*
* @return [Object|Array|String|Number|Boolean|null]
*
* @see AWS.DynamoDB.Converter.unmarshall AWS.DynamoDB.Converter.unmarshall to
* convert entire records (rather than individual attributes)
*/
static output(data: Doc, options: Doc = {}): any {
for (const type in data) {
const values: any = data[type];
if (type === "M") {
const map: Doc = {};
for (const key in values) {
map[key] = Converter.output(values[key], options);
}
return map;
} else if (type === "L") {
// list = [];
// for (i = 0; i < values.length; i++) {
// list.push(Converter.output(values[i], options));
// }
// return list;
return values.map((value: any): any =>
Converter.output(value, options)
);
} else if (type === "SS") {
// list = [];
// for (i = 0; i < values.length; i++) {
// list.push(values[i] + '');
// }
// return new DynamoDBSet(list);
return new DynamoDBSet(values.map(String));
} else if (type === "NS") {
// list = [];
// for (i = 0; i < values.length; i++) {
// list.push(convertNumber(values[i], options.wrapNumbers));
// }
// return new DynamoDBSet(list);
return new DynamoDBSet(
values.map((value: any): number =>
convertNumber(value, options.wrapNumbers)
),
);
} else if (type === "BS") {
// list = [];
// for (i = 0; i < values.length; i++) {
// list.push(base64ToUint8Array(values[i]));
// }
// return new DynamoDBSet(list);
return new DynamoDBSet(values.map(base64ToUint8Array));
} else if (type === "S") {
return String(values);
} else if (type === "N") {
return convertNumber(values, options.wrapNumbers);
} else if (type === "B") {
return base64ToUint8Array(values);
} else if (type === "BOOL") {
return values === "true" || values === "TRUE" || values === true;
} else if (type === "NULL") {
return null;
}
}
}
/**
* Convert a DynamoDB record into a JavaScript object.
*
* @param data [any] The DynamoDB record
* @param options [map]
* @option options convertEmptyValues [Boolean] Whether to automatically
* convert empty strings, blobs,
* and sets to `null`
* @option options wrapNumbers [Boolean] Whether to return numbers as a
* NumberValue object instead of
* converting them to native JavaScript
* numbers. This allows for the safe
* round-trip transport of numbers of
* arbitrary size.
*
* @return [map] An object whose properties have been converted from
* DynamoDB's AttributeValue format into their corresponding native
* JavaScript types.
*
* @example Convert a record received from a DynamoDB stream
* var unmarshalled = AWS.DynamoDB.Converter.unmarshall({
* string: {S: 'foo'},
* list: {L: [{S: 'fizz'}, {S: 'buzz'}, {S: 'pop'}]},
* map: {
* M: {
* nestedMap: {
* M: {
* key: {S: 'value'}
* }
* }
* }
* },
* number: {N: '123'},
* nullValue: {NULL: true},
* boolValue: {BOOL: true}
* });
*/
static unmarshall(data: Doc, options?: Doc): Doc {
return Converter.output({ M: data }, options);
}
}