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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
2×
1×
2×
1×
1×
1×
1×
1×
1×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
2×
2×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
1×
1×
276×
1×
2×
1×
1×
1×
1×
| var AWS = require('aws-sdk');
var async = require('async');
var lo = require('lodash');
var q = require('q');
AWS.config.update({
region: 'us-west-2',
});
var dataHelper = require('./helpers/dataHelper');
var db = new AWS.DynamoDB();
var scanTimeLimit = 20000;
module.exports = {
describeTable: describeTable,
initialize: initialize,
scan: scan,
getItem: getItem,
putItem: putItem,
deleteItem: deleteItem,
query: query,
updateItem: updateItem
};
/**
* initialize dynamodb access
*
* @param {string} [region="us-west-2"] sets the region of your dynamodb table
* @param {function} callback callback function to call when initialization is done (not necessary, this is synchronous)
*/
function initialize(params, callback) {
AWS.config.update({
region: params.region || 'us-west-2'
});
db = new AWS.DynamoDB();
Eif (callback) {
callback();
}
}
/**
* describeTable - get table metadata
*
* @param {string} table table name
* @param {function} callback callback function
*/
function describeTable(params, callback) {
var settings = {
TableName: params.table
};
db.describeTable(settings, callback);
}
/**
* updateItem - update a single item
*
* @param {string} table table name
* @param {object} key primary key to table entry to update
* @param {object} expression update expression
* @param {object} values update values
* @param {function} callback callback callback function
*
* @example
* var table = 'table-name';
var key = {
username: {
'S': params.username
}
};
var expression = "set password = :val1";
var values = {
':val1': {
'S': bcrypt.hashSync(params.newPassword, 8)
},
};
var params = {
table: table,
key: key,
expression: expression,
values: values
};
db.updateItem(params, function(err, results(){});
*/
function updateItem(params, callback) {
if (!params.key || !params.expression || !params.values || !params.table) {
return callback('Required parameters: key, expression, values, table');
}
var settings = {
TableName: params.table,
Key: params.key,
UpdateExpression: params.expression,
ExpressionAttributeValues: params.values
};
db.updateItem(settings, function (err, data) {
Iif (err) {
callback(err, data);
} else {
callback(null, data);
}
});
}
/**
* query - query items from the table.
*
* @param {string} table table name~
* @param {object} key keys to query
* @param {function} callback callback callback function
*
* @example
* var table = 'table-name';
var key = {
customerId: {
ComparisonOperator: 'EQ',
AttributeValueList: [{
S: '1455-15412'
}]
}
}
db.query(params, function(err, results(){});
*/
function query(params, callback) {
Iif (!params.table || !params.key) {
return callback('Required parameters: table, key');
}
var settings = {
TableName: params.table,
KeyConditions: params.key
};
db.query(settings, function (err, data) {
Iif (err) {
callback(err, data);
} else {
Eif (!params.raw) {
dataHelper.removeKey(data.Items);
}
callback(null, data.Items);
}
});
}
/**
* deleteItem - delete an item from the table
*
* @param {string} table table name
* @param {object} key primary key to table entry to update
* @param {function} callback callback callback function
*
* @example
* var table = 'table-name';
var key = {
username: {
'S': 'steve'
}
};
db.deleteItem(params, function(err, results(){});
*/
function deleteItem(params, callback) {
if (!params.table || !params.key) {
return callback('Required parameters: table, key');
}
var settings = {
TableName: params.table,
Key: params.key
};
db.deleteItem(settings, function (err, data) {
if (err) {
callback(err, data);
} else {
callback(null, data);
}
});
}
/**
* getItem - get a single item from the table
*
* @param {string} table table name
* @param {object} key primary key to table entry to retrieve
* @param {function} callback callback callback function
*
* @example
* var table = 'table-name';
var key = {
username: {
'S': 'steve'
}
};
db.getItem(params, function(err, results(){});
*/
function getItem(params, callback) {
if (!params.table || !params.key) {
callback('Required parameters: table, key');
}
var settings = {
TableName: params.table,
Key: params.key
};
db.getItem(settings, function (err, data) {
if (err) {
callback(err, data);
} else {
try {
if (!params.raw) {
dataHelper.removeKey(data.Item);
}
callback(null, data.Item);
} catch (e) {
callback(e, data);
}
}
});
}
/**
* scan - scan table
*
* @param {string} table table name
* @param {boolean} [raw=false] if true, prevents removing the data types from the response
* @param {number} [sleep=false] sets a sleep timer between pagination calls
* @param {function} callback callback callback function
*
* @example
* var table = 'table-name';
db.scan({table: table}, function(err, results(){});
*/
function scan(params, mainCallback) {
var start = new Date().getTime();
Iif (!params.table) {
return callback('Required parameters: table');
}
var settings = {
TableName: params.table
};
Iif (params.limit) {
settings.Limit = params.limit;
}
var response = [];
var recurse = false;
async.doWhilst(function (callback) {
db.scan(settings, function (err, data) {
if (err) {
recurse = false;
callback({
error: err,
maxReached: response.length > 0
});
} else {
Eif (!params.raw) {
dataHelper.removeKey(data.Items);
}
buildArray(response, data.Items);
Eif (!data.LastEvaluatedKey) {
recurse = false;
callback();
} else {
if (new Date().getTime() - start < scanTimeLimit) {
recurse = true;
}
else {
recurse = false;
}
settings.ExclusiveStartKey = data.LastEvaluatedKey;
if (params.sleep) {
setTimeout(function () {
callback();
}, params.sleep);
} else {
callback();
}
}
}
});
},
function () {
return recurse;
},
function (err) {
mainCallback(err, response);
});
}
function buildArray(array, newArray) {
newArray.forEach(function (val) {
array.push(val);
});
}
/**
* putItem - inserts an item into the table
*
* @param {string} table table name
* @param {object} key item to insert
* @param {function} callback callback callback function
*
* @example
var table = 'table-name';
var item = {
username: {
'S': data.username
},
createDate: {
'S': new Date().toString()
},
};
var params = {
table: table,
key: item
};
db.putItem(params, function(err,results){});
*/
function putItem(params, callback) {
if (!params.table || !params.item) {
return callback('Required parameters: table, item');
}
db.putItem({
TableName: params.table,
Item: params.item
}, function (err, data) {
Iif (err) {
callback(err, data);
} else {
callback(null, data);
}
});
}
|