-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathAttributeValue.js
224 lines (188 loc) · 5.07 KB
/
AttributeValue.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
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
};