-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamoDb.js
More file actions
699 lines (641 loc) · 19.5 KB
/
dynamoDb.js
File metadata and controls
699 lines (641 loc) · 19.5 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/* eslint-disable no-plusplus */
/* eslint-disable no-ternary */
'use strict';
const capacityLogLimit = 5;
const { AllDM: DM } = require('../model/dataModel');
function makeDb({ makeDbConnect, getTableName }) {
return Object.freeze({
putItem,
getItem,
updateItem,
query,
queryGSI,
});
async function putItem({ itemInfo }) {
// Connect to the database.
const db = makeDbConnect();
// Prepare the item info for DynamoDB.
const item = objectToItem({ itemInfo });
// Create the input object for DynamoDB.
const input = createPutItemInput(item);
// Put the item in the database.
const result = await db.putItem(input).promise();
// Return the result and remove metrics.
return removeMetrics({ result, fnName: 'putItem', itemInfo });
}
async function updateItem({ itemInfo, returnValues = true }) {
// 1. Connect to the DynamoDB table
const db = makeDbConnect();
// 2. Update the item in the table
const result = await db
.updateItem(
createUpdateItemInput({
item: objectToItem({ itemInfo }),
returnValues,
})
)
.promise();
// 3. Return data from the updated item
if (returnValues) {
return splitKeys(
itemToObject({
item: removeMetrics({ result, fnName: 'updateItem', itemInfo }),
})
);
}
return removeMetrics({ result, fnName: 'updateItem', itemInfo });
}
// Return a single item from the database
// itemInfo: The unique identifier for the item
// to retrieve from the database
// Return: The item from the database
async function getItem({ itemInfo }) {
// Connect to the database
const db = makeDbConnect();
// Retrieve the item from the database
const result = await db
.getItem(createGetItemInput(objectToItem({ itemInfo })))
.promise();
// Return the item from the database
return splitKeys(
itemToObject({
item: removeMetrics({ result, fnName: 'getItem', itemInfo }),
})
);
}
async function query({
itemInfo,
limit = 1,
operator = '>=',
statusReason,
validOnly = false,
}) {
// Connect to the database
const db = makeDbConnect();
// Create the query input
const result = await db
.query(
createQueryInput({
itemInfo: objectToItem({ itemInfo }),
operator,
limit,
statusReason,
validOnly,
})
)
.promise();
// Return the results
return splitKeysArray(
itemToObjectArray({
item: removeMetrics({ result, fnName: 'query', itemInfo }),
})
);
}
async function queryGSI({
itemInfo,
limit = 0,
operator = '>=',
statusReason,
validOnly = false,
status,
}) {
const db = makeDbConnect();
const result = await db
.query(
createQueryGSIInput({
itemInfo: objectToItem({ itemInfo }),
operator,
limit,
statusReason,
validOnly,
status,
})
)
.promise();
return splitKeysArray(
itemToObjectArray({
item: removeMetrics({ result, fnName: 'queryGSI', itemInfo }),
})
);
}
function itemToObjectArray({ item }) {
const response = [];
item.forEach((i) => {
response.push(itemToObject({ item: i }));
});
return response;
}
function objectToItem({ itemInfo: obj }) {
const response = {};
Object.keys(obj).forEach((element) => {
if (obj[element]) {
switch (obj[element].constructor.name) {
case 'Number': {
response[element] = { N: obj[element].toString() };
break;
}
case 'Array': {
response[element] = objectArrayToItem({ itemInfo: obj[element] });
break;
}
case 'Object': {
response[element] = objectMapToItem({ itemInfo: obj[element] });
break;
}
case 'Boolean': {
response[element] = { BOOL: obj[element] };
break;
}
default: {
response[element] = { S: obj[element] };
break;
}
}
}
});
return response;
}
function objectArrayToItem({ itemInfo }) {
const response = { L: [] };
itemInfo.forEach((element) => {
switch (element.constructor.name) {
case 'Number': {
response.L.push({ N: element.toString() });
break;
}
case 'Array': {
response.L.push(objectArrayToItem({ itemInfo: element }));
break;
}
case 'Object': {
response.L.push(objectMapToItem({ itemInfo: element }));
break;
}
case 'Boolean': {
response.L.push({ BOOL: element });
break;
}
default: {
response.L.push({ S: element });
}
}
});
return response;
}
function objectMapToItem({ itemInfo }) {
const response = { M: {} };
Object.keys(itemInfo).forEach((element) => {
if (!itemInfo[element]) return;
switch (itemInfo[element].constructor.name) {
case 'Number': {
response.M[element] = { N: itemInfo[element].toString() };
break;
}
case 'Array': {
response.M[element] = objectArrayToItem({
itemInfo: itemInfo[element],
});
break;
}
case 'Object': {
response.M[element] = objectMapToItem({
itemInfo: itemInfo[element],
});
break;
}
case 'Boolean': {
response.M[element] = { BOOL: itemInfo[element] };
break;
}
default: {
response.M[element] = { S: itemInfo[element] };
}
}
});
return response;
}
function itemToObject({ item }) {
const validDataTypes = ['S', 'N', 'BOOL', 'L', 'M'];
const response = {};
Object.keys(item).forEach((element) => {
const dataType = Object.keys(item[element]);
if (dataType.length > 1) {
console.warn('multiple data types ', dataType);
}
if (dataType && dataType[0] && validDataTypes.includes(dataType[0])) {
response[element] = itemToValue({
item: item[element],
dataType: dataType[0],
});
}
});
return response;
}
// This function is used to convert a DynamoDB value to a Javascript value
// It is used in the following functions:
// - get
// - getBatch
// - scan
// - query
// - update
// - updateBatch
function itemToValue({ item, dataType }) {
let response;
switch (dataType) {
case 'N':
// If the DynamoDB value is a number, convert it to a Javascript number
return parseInt(item[dataType]);
case 'L':
// If the DynamoDB value is a list, convert it to a Javascript array
response = [];
item[dataType].forEach((item) =>
response.push(itemToValue({ item, dataType: Object.keys(item)[0] }))
);
return response;
case 'M':
// If the DynamoDB value is a map, convert it to a Javascript object
response = {};
Object.keys(item[dataType]).forEach((i) => {
response[i] = itemToValue({
item: item[dataType][i],
dataType: Object.keys(item[dataType][i])[0],
});
});
return response;
default:
// If the DynamoDB value is a string, binary, boolean, or null, return the value as is
return item[dataType];
}
}
function createPutItemInput(item, allowOverwrite = false) {
if (!item.PK.S || !item.SK.S) {
console.error(item);
throw new Error('Missing or invalid key');
}
const input = {
TableName: getTableName(),
Item: item,
...(allowOverwrite && {
ConditionExpression: 'attribute_not_exists(PK)',
}),
ReturnConsumedCapacity: 'TOTAL',
};
return input;
}
/**
* @description
* @param {Object} itemInfo
* @param {Object} itemInfo.PK
* @param {Object} itemInfo.SK
* @param {string} [operator='='] - one of =, >, >=, <, <=, begins_with, between
* @param {boolean} [validOnly=true]
* @param {string} [statusReason]
* @param {number} [limit]
* @returns {Object}
*/
function createQueryInput({
itemInfo,
operator = '=',
validOnly = true,
statusReason,
limit,
}) {
if (!itemInfo.PK) {
console.error(itemInfo);
throw new Error('Missing or invalid key');
}
if (
!['=', '>', '>=', '<', '<=', 'begins_with', 'between'].includes(operator)
) {
console.error(itemInfo);
throw new Error('Invalid KeyConditionExpression operator');
}
// build key condition expression
// input itemInfo must have PK and SK (if exists)
// operator is a valid DynamoDB operator
// ex: operator = 'begins_with' or operator = '='
// The key condition expression is the condition that must be met on the primary key.
// The attribute names in the expression must be preceded by a hash (#) character.
// The attribute values in the expression must be preceded by a colon (:) character.
let keyCond = '#PK = :PK ';
if (itemInfo.SK) {
if (operator === 'begins_with') {
keyCond += 'and begins_with(#SK,:SK)';
} else {
keyCond += `and #SK ${operator} :SK`;
}
}
// The filter expression determines which items we want to return in our query.
// In this case, we want to return all items with no status attribute or
// with the specified status.
let filterExp = `(attribute_not_exists (#status) or #status = :status)`;
// If a status reason was provided, we also want to return only items with the specified
// status reason.
if (statusReason) {
filterExp += (filterExp ? ' and ' : '') + '#statusReason = :statusReason';
}
// Create the input object for the DynamoDB query operation
const input = {
// Specify the name of the table to query
TableName: getTableName(),
// Specify the maximum number of items to return
Limit: limit,
// Specify the condition that the primary key must meet
KeyConditionExpression: keyCond,
// If validOnly is true, add a condition that the status attribute must be '1'
...(validOnly && {
FilterExpression: filterExp,
}),
// Specify the attributes to return
Select: 'ALL_ATTRIBUTES',
// Specify the names of the attributes to return
ExpressionAttributeNames: {
'#PK': 'PK',
...(itemInfo.SK && { '#SK': 'SK' }),
...(validOnly && { '#status': 'status' }),
...(statusReason && { '#statusReason': 'statusReason' }),
},
// Specify the values of the attributes to return
ExpressionAttributeValues: {
':PK': {
...itemInfo.PK,
},
...(itemInfo.SK && {
':SK': {
...itemInfo.SK,
},
}),
...(validOnly && {
':status': { S: '1' },
}),
...(statusReason && { ':statusReason': { S: statusReason } }),
},
// Specify the return value
ReturnConsumedCapacity: 'TOTAL',
};
return input;
}
// Creates a DynamoDB input object for a getItem operation
// The 'item' parameter must be an object with 'PK' and 'SK' properties
// The 'PK' and 'SK' properties must be objects with 'S' properties
// Returns the input object
function createGetItemInput(item) {
// Check that the item has the required 'PK' and 'SK' properties
if (!item.PK.S || !item.SK.S) {
console.error('ItemInfo:', item);
throw new Error('Missing or invalid key');
}
// Create a 'Key' property with the 'PK' and 'SK' values
const input = {
TableName: getTableName(),
Key: {
PK: { ...item.PK },
SK: { ...item.SK },
},
ReturnConsumedCapacity: 'TOTAL',
};
return input;
}
// This function creates an input object for the DynamoDB updateItem command.
// It takes the following parameters:
// item: The item that will be updated
// returnValues: Whether or not to return the updated item (default = false)
// increments: An array of attributes that will be incremented (default = [])
// The function returns an object that can be passed to the DynamoDB updateItem command.
function createUpdateItemInput({
item: { PK: _PK, SK: _SK, ...itemInput },
returnValues,
increments,
}) {
// Validate that the input item has a PK and SK
if (!_PK || !_SK) {
throw new Error('Missing or invalid key');
}
// Create the UpdateExpression
let exp = 'SET',
i = 0;
const values = {},
names = {};
if (increments)
increments.forEach((element) => {
exp =
exp +
(i++ ? ',' : '') +
' #' +
element.attName +
'=' +
`if_not_exists(#${element.attName}, :${element.attName})` +
// ' #' +
// element.attName +
'+:' +
element.attName;
values[`:${element.attName}`] = { N: element.inc.toString() };
names[`#${element.attName}`] = element.attName;
});
Object.keys(itemInput).forEach((element) => {
exp = exp + (i++ ? ',' : '') + ' #' + element + '=:' + element;
values[`:${element}`] = { ...itemInput[element] };
names[`#${element}`] = element;
});
// Create the input object for the DynamoDB updateItem command
const input = {
TableName: getTableName(),
Key: {
PK: { ..._PK },
SK: { ..._SK },
},
UpdateExpression: exp,
ExpressionAttributeValues: values,
ExpressionAttributeNames: names,
ReturnConsumedCapacity: 'TOTAL',
...(returnValues && { ReturnValues: 'ALL_NEW' }),
};
return input;
}
function createQueryGSIInput({
itemInfo,
operator = '=',
validOnly = true,
statusReason,
status,
limit,
index = '1',
}) {
// Define the names of the primary key attributes
const _PK = 'PK' + index,
_SK = 'SK' + index,
_PKn = '#' + _PK,
_PKv = ':' + _PK,
_SKn = '#' + _SK,
_SKv = ':' + _SK,
indexName = 'GSI' + index;
// Ensure the index is valid
if (!['1', '2'].includes(index)) {
throw new Error(`Index can only be '1' or '2'`);
}
// Ensure the itemInfo contains the required attributes
if (!itemInfo[_PK] || !itemInfo[_SK]) {
throw new Error(
`${indexName} requires attributes ({_PK${index}}, {_SK${index}}).`
);
}
// Ensure the operator is valid
if (!['=', '>', '>=', '<', '<=', 'begins_with'].includes(operator)) {
throw new Error('Invalid KeyConditionExpression operator');
}
// Define the attribute names
const attributeNames = {},
attributeValues = {};
// Define the primary key attribute names
attributeNames[_PKn] = _PK;
attributeValues[_PKv] = { ...itemInfo[_PK] };
attributeNames[_SKn] = _SK;
attributeValues[_SKv] = { ...itemInfo[_SK] };
// Define the filter expression
let filterExp = validOnly
? '(attribute_not_exists (#expiryTs) or #expiryTs > :expiryTs)'
: '';
filterExp +=
(filterExp ? ' and ' : '') +
'(attribute_not_exists (#status) or #status = :status)';
if (statusReason) {
filterExp += (filterExp ? ' and ' : '') + '#statusReason = :statusReason';
}
// Define the key condition expression
let keyCond = '#PK1 = :PK1 ';
if (itemInfo.SK1) {
if (operator === 'begins_with') {
keyCond += 'and begins_with(#SK1,:SK1)';
} else {
keyCond += `and #SK1 ${operator} :SK1`;
}
}
/**
* Returns a list of items matching the specified filter and limit
* @param {string} filter
* @param {number} limit
* @param {string} indexName
* @param {string} keyCond
* @param {object} attributeNames
* @param {object} attributeValues
* @param {boolean} validOnly
* @param {string} status
* @param {string} statusReason
* @returns {Promise<*>}
*/
const input = {
TableName: getTableName(),
// If the limit is greater than 0, limit the number of results returned
...(limit > 0 && { Limit: limit }),
// Use the specified index
IndexName: indexName,
// Filter the results by the specified key condition
KeyConditionExpression: keyCond,
// If validOnly is true, add a filter expression for the expiry timestamp and status
...(validOnly && {
...(filterExp && { FilterExpression: filterExp }),
}),
// Return all attributes
Select: 'ALL_ATTRIBUTES',
// Add the specified attribute names
ExpressionAttributeNames: {
...attributeNames,
...(validOnly && {
'#expiryTs': 'expiryTs',
'#status': 'status',
'#statusReason': 'statusReason',
}),
},
// Add the specified attribute values
ExpressionAttributeValues: {
...attributeValues,
...(validOnly && {
':expiryTs': { S: new Date().toISOString() },
':status': { S: status },
':statusReason': { S: statusReason },
}),
},
// Return the capacity used by the operation
ReturnConsumedCapacity: 'TOTAL',
};
return input;
}
/**
* Remove metrics from a DynamoDB result
* @param {object} result - The result from a DynamoDB function
* @param {string} fnName - The name of the function
* @returns {object} - The original result without metrics
*/
function removeMetrics({ result, fnName }) {
// Extract metrics from the result object
const {
Count: count,
ScannedCount: scannedCount,
LastEvaluatedKey: lastEvaluatedKey,
ConsumedCapacity: consumedCapacity,
ItemCollectionMetrics: itemCollMetrics,
Attributes: attributes,
Item: item,
Items: items,
...remaining
} = result;
// Log consumed capacity if it is above the limit
if (consumedCapacity.CapacityUnits > capacityLogLimit) {
console.warn(
`Consumed Capacity [${fnName}]:= ${JSON.stringify(consumedCapacity)}`
);
}
// Log additional metrics
console.warn(
`Consumed Capacity [${fnName}]:= ${JSON.stringify(consumedCapacity)}`
);
console.warn('Additional Metrics', {
...(itemCollMetrics && { itemCollMetrics }),
...(count && { count }),
...(scannedCount && { scannedCount }),
...(lastEvaluatedKey && { lastEvaluatedKey }),
});
// Return the original result without metrics
return item || items || attributes || remaining;
}
function splitKeysArray(item) {
return item.map((i) => splitKeys(i));
}
/**
* This code is used to split the keys of a given item.
*
* @param item The item to split the keys of.
* @return The split keys of the given item.
*/
function splitKeys(item) {
// If there is no item, return an empty item.
if (item.length === 0) {
return item;
}
// Split the keys for each type of item.
const {
PK: _PK,
SK: _SK,
PK1: _PK1,
SK1: _SK1,
PK2: _PK2,
SK2: _SK2,
type: _type,
...resultInfo
} = item;
// Return an object that contains the split keys and the result info.
return Object.assign(
!_PK ? {} : DM[_type].splitPK(_PK),
!_SK ? {} : DM[_type].splitSK(_SK),
!_PK1 ? {} : DM[_type].splitPK1(_PK1),
!_SK1 ? {} : DM[_type].splitSK1(_SK1),
!_PK2 ? {} : DM[_type].splitPK2(_PK2),
!_SK2 ? {} : DM[_type].splitSK2(_SK2),
_type ? { type: _type } : {},
!resultInfo ? {} : resultInfo
);
}
}
module.exports = {
makeDb,
};