forked from kayomarz/dynamodb-data-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb-data-types.js
405 lines (340 loc) · 10.7 KB
/
dynamodb-data-types.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/* For browserify to create a build for the browser */
window.DynamoDbDataTypes = require('./lib/dynamodb-data-types');
},{"./lib/dynamodb-data-types":4}],2:[function(require,module,exports){
(function (Buffer){
var isArray = require('./util').isArray;
var errs = require('./errs');
// var DEBUG = false;
var __preserveArrays = false;
function _preserveArrays() {
__preserveArrays = true;
}
function test(arr, fn) {
for (var i = 0; i < arr.length; i++) {
if (!fn(arr[i]))
return false;
}
return true;
}
function isnumber(el) {
return typeof el === 'number' || el instanceof Number;
}
function isstring(el) {
return typeof el === 'string' || el instanceof String;
}
function isbinary(el) {
if (el instanceof Buffer)
return true;
return false;
}
function detectType(val) {
if (isArray(val)) {
var arr = val;
if (test(arr, isnumber))
return 'NS';
if (test(arr, isstring))
return 'SS';
if (test(arr, isbinary))
return 'BS';
return 'L';
}
if (isstring(val))
return 'S';
if (isnumber(val))
return 'N';
if (isbinary(val))
return 'B';
if (val === null)
return 'NULL';
if (typeof val === 'boolean')
return 'BOOL';
if (typeof val === 'object') {
return 'M';
}
}
function explicit_type(opts, key) {
var type_specified = typeof opts === 'object' &&
typeof opts.types === 'object' &&
typeof key === 'string' &&
typeof opts.types[key] === 'string';
if (!type_specified)
return;
var type = opts.types[key];
if (typeExists(type))
return type;
}
var MULTIPLES = ['L', 'BS', 'NS', 'SS'];
function getType(val, opts, key) {
var explicit = explicit_type(opts, key);
var detected = detectType(val);
var type = detected;
if (isArray(val) && __preserveArrays)
type = 'L';
if (MULTIPLES.indexOf(explicit) > -1 && MULTIPLES.indexOf(detected) > -1)
type = explicit;
return type;
}
function eachToString(arr) {
return arr.map(function(v) {
return v.toString();
});
}
/**
* Wrap a single value into DynamoDB's AttributeValue.
* @param {String|Number|Array} val The value to wrap.
* @return {Object} DynamoDB AttributeValue.
*/
function wrap1(val, opts, key) {
switch(getType(val, opts, key)) {
case 'B': return {'B': val};
case 'BS': return {'BS': val};
case 'N': return {'N': val.toString()};
case 'NS': return {'NS': eachToString(val)};
case 'S': return {'S': val.toString()};
case 'SS': return {'SS': eachToString(val)};
case 'BOOL': return {'BOOL': val ? true: false};
case 'L': return {'L': val.map(function(obj){ return wrap1(obj, opts); })};
case 'M': return {'M': wrap(val, opts)};
case 'NULL': return {'NULL': true};
default: return;
}
}
/**
* Wrap object properties into DynamoDB's AttributeValue data type.
* @param {Object} obj The object to wrap.
* @return {Object} A DynamoDb AttributeValue.
*/
function wrap(obj, opts) {
var result = {};
for (var key in obj) {
if(obj.hasOwnProperty(key)) {
var wrapped = wrap1(obj[key], opts, key);
if (typeof wrapped !== 'undefined')
result[key] = wrapped;
}
}
return result;
}
var unwrapFns = {
'B': undefined,
'BS': undefined,
'N': function (o) { return Number(o); },
'NS':function (arr) { return arr.map(function(o) {return Number(o);}); },
'S': undefined,
'SS': undefined,
'BOOL': undefined,
'L': function(val) { return val.map(unwrap1); },
'M': function (val) { return unwrap(val); },
'NULL': function() { return null; }
};
function typeExists(type) {
return unwrapFns.hasOwnProperty(type);
}
/**
* Unwrap a single DynamoDB's AttributeValue to a value of the appropriate
* javascript type.
* @param {Object} attributeValue The DynamoDB AttributeValue.
* @return {String|Number|Array} The javascript value.
*/
function unwrap1(dynamoData) {
var keys = Object.keys(dynamoData);
if (keys.length !== 1)
throw new Error('Unexpected DynamoDB AttributeValue');
var typeStr = keys[0];
if (!unwrapFns.hasOwnProperty(typeStr))
throw errs.NoDatatype;
var val = dynamoData[typeStr];
return unwrapFns[typeStr] ? unwrapFns[typeStr](val) : val;
}
/**
* Unwrap DynamoDB AttributeValues to values of the appropriate types.
* @param {Object} attributeValue The DynamoDb AttributeValue to unwrap.
* @return {Object} Unwrapped object with properties.
*/
function unwrap(attrVal) {
var result = {};
for (var key in attrVal) {
if(attrVal.hasOwnProperty(key)) {
var value = attrVal[key];
if (value !== null && typeof value !== 'undefined')
result[key] = unwrap1(attrVal[key]);
}
}
return result;
}
// function printData(input, result, title) {
// if (DEBUG) {
// console.log(title + ' input:');
// console.log(JSON.stringify(input, undefined, 2));
// console.log(title + ' result:');
// console.log(JSON.stringify(result, undefined, 2));
// }
// return result;
// }
function wrapDebug(obj, opts) {
var wrapped = wrap(obj, opts);
// if (DEBUG)
// printData(obj, wrapped, 'wrap');
return wrapped;
}
function unwrapDebug(attrVal) {
var unwrapped = unwrap(attrVal);
// if (DEBUG)
// printData(attrVal, unwrapped, 'unwrap');
return unwrapped;
}
module.exports = {
wrap: wrapDebug,
unwrap: unwrapDebug,
wrap1: wrap1,
unwrap1: unwrap1,
_preserveArrays: _preserveArrays
};
}).call(this,require("buffer").Buffer)
},{"./errs":5,"./util":6,"buffer":7}],3:[function(require,module,exports){
var isArray = require('./util').isArray;
var errs = require('./errs');
var attr = require('./AttributeValue');
var util = require('./util');
function isStr(val) {
return typeof val === "string" || val instanceof String;
}
function hasKeysOnly(attrs) {
return (attrs && (isStr(attrs) || isArray(attrs))) ? true : false;
}
function appendAttr(action, key, value, updates, opts) {
if (value !== null && typeof value !== 'undefined')
updates[key] = {Action: action, Value: attr.wrap1(value, opts, key)};
else
updates[key] = {Action: action};
}
/* Append attributes without a value.
* @param {String} action
* @param {String|Array} attrs key names sepcified as comma separated string or
* as an array of strings.
* @param {Updates} updates object.
*/
function appendAttrsKeysOnly(action, attrs, updates, opts) {
if (typeof attrs === "string" || attrs instanceof String)
attrs = attrs.split(",");
if (attrs && isArray(attrs)) {
for(var i = 0; i < attrs.length; i++) {
var key = attrs[i];
if (key && isStr(key) && key.length > 0)
appendAttr(action, key.trim(), null, updates, opts);
}
}
return updates;
}
/*
* Append action and attributes to the specified updates object.
*/
function appendAttrs(action, attrs, _updates, opts) {
var updates = isTypeUpdates(this) ? this : (_updates || (new Updates()));
if (attrs && hasKeysOnly(attrs)) {
appendAttrsKeysOnly(action, attrs, updates, opts);
} else if (typeof attrs === "object") {
util.forIn(attrs, function(value, key) {
if (value !== null && typeof value !== 'undefined')
appendAttr(action, key, value, updates, opts);
});
}
return updates;
}
/**
* Append attributes to be updated with action "ADD".
* This function can be chained with further calls to `add', `put' or `delete'.
* @param {Object} attrs Object with attributes to be updated.
* @return {Updates} Object with all update attributes in the chain.
*/
function add(attrs, opts) {
return appendAttrs.call(this, "ADD", attrs, undefined, opts);
}
/**
* Append attributes to be updated with action "PUT".
* This function can be chained with further calls to `add', `put' or `delete'.
* @param {Object} attrs Object with attributes to be updated.
* @return {Updates} Object with all update attributes in the chain.
*/
function put(attrs, opts) {
return appendAttrs.call(this, "PUT", attrs, undefined, opts);
}
/**
* Append attributes to be updated with action "DELETE".
* This function can be chained with further calls to `add', `put' or `delete'.
* @param {Object|String|Array} attrs If this argument is an an Object,
* the Object's property values must be an array, containing elements to be
* removed, as required by DynamoDb SDK.
* If this argument is a String, it should contain comma seperated names of
* properties to be deleted. If its an Array, each array element should be a
* property name to be deleted.
* @return {Updates} Object with all update attributes in the chain.
*/
function del(attrs) {
return appendAttrs.call(this, "DELETE", attrs);
}
module.exports = {
add: add,
put: put,
delete: del, // use del instead of delete as its a reserved keyword.
isTypeUpdates: isTypeUpdates,
Updates: Updates
};
function Updates() {
}
function isTypeUpdates(obj) {
return obj instanceof Updates;
}
function addProp(name, value, target) {
Object.defineProperty(target, name, {
value: value,
writable: false,
enumerable: false,
configurable: false
});
}
addProp("add", add, Updates.prototype);
addProp("put", put, Updates.prototype);
addProp("delete", del, Updates.prototype);
},{"./AttributeValue":2,"./errs":5,"./util":6}],4:[function(require,module,exports){
var attr = require('./AttributeValue');
function preserveArrays() {
attr._preserveArrays();
}
module.exports = {
errs: require('./errs'),
AttributeValue: attr,
AttributeValueUpdate: require('./AttributeValueUpdate'),
preserveArrays: preserveArrays
};
},{"./AttributeValue":2,"./AttributeValueUpdate":3,"./errs":5}],5:[function(require,module,exports){
module.exports = {
NoDatatype: new Error("No data type (B, BS, N, NS, S, SS)."),
NoData: new Error("No data")
};
},{}],6:[function(require,module,exports){
/*
* For each non-inherited, enumarable property, the callback is invoked with
* arguments: (value, key, object).
* @param {Object} The object.
* @param {Function} The function called for each iteration with
* arguments(value, key, object)
*/
function forIn(obj, fn) {
var keys = Object.keys(obj);
var len = keys.length;
for(var i = 0; i < len; i++) {
var key = keys[i];
fn(obj[key], key, obj);
}
}
var isArray = Array.isArray || function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
module.exports = {
forIn: forIn,
isArray: isArray
};
},{}],7:[function(require,module,exports){
},{}]},{},[1]);