@izara_project/izara-core-library-dynamodb 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@izara_project/izara-core-library-dynamodb",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Connecting with AWS DynamoDB Resource",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -457,15 +457,17 @@ module.exports.query = async (_izContext, tableName, partitionKeyValue, queryEle
457
457
  throw new NoRetryError('partitionKeyValue should be "object" type.')
458
458
  }
459
459
 
460
+ let attributeReservedNames = ['partitionKeyValue', 'betweenStartValue', 'betweenEndValue'];
461
+
460
462
  // ------------ set payload ---------------
461
463
  let payload = {
462
464
  TableName: tableName, // require
463
465
  // KeyConditionExpression: '',
464
- KeyConditionExpression: `${Object.keys(partitionKeyValue)[0]} = :${Object.keys(partitionKeyValue)[0]}`, // require partition key
466
+ KeyConditionExpression: `${Object.keys(partitionKeyValue)[0]} = :partitionKeyValue`, // require partition key
465
467
  ExpressionAttributeValues: {}, // require value of partition key
466
468
  ReturnConsumedCapacity: "TOTAL" // NONE(default) | TOTAL | INDEXES
467
469
  }
468
- payload.ExpressionAttributeValues[`:${Object.keys(partitionKeyValue)[0]}`] = partitionKeyValue[Object.keys(partitionKeyValue)[0]]
470
+ payload.ExpressionAttributeValues[`:partitionKeyValue`] = partitionKeyValue[Object.keys(partitionKeyValue)[0]]
469
471
 
470
472
  // ---- sortKeyCondition ----
471
473
  /* NOTE: KeyConditionExpression
@@ -494,20 +496,26 @@ module.exports.query = async (_izContext, tableName, partitionKeyValue, queryEle
494
496
  payload.KeyConditionExpression += ' AND '
495
497
 
496
498
  if (queryElements.sortKeyCondition.comparison == 'between') {
497
- // validate between sidehand, should have both side and value is number
498
- if (!queryElements.sortKeyCondition.hasOwnProperty('betweenStartValue') || !queryElements.sortKeyCondition.hasOwnProperty('betweenEndValue')
499
- || typeof queryElements.sortKeyCondition.betweenStartValue !== 'number' || typeof queryElements.sortKeyCondition.betweenEndValue !== 'number'
500
- ) {
501
- throw new NoRetryError('sortKeyCondition should set both side-hand betweenStartValue/end and value should be number')
502
- }
499
+ // can compare both numeric and stringfs in Dynamo, but the typoe sent must match the type store in Dynamo
500
+ // in future maybe check type from objectSchema to make sure they match
501
+ // if (!queryElements.sortKeyCondition.hasOwnProperty('betweenStartValue') || !queryElements.sortKeyCondition.hasOwnProperty('betweenEndValue')
502
+ // || typeof queryElements.sortKeyCondition.betweenStartValue !== 'number' || typeof queryElements.sortKeyCondition.betweenEndValue !== 'number'
503
+ // ) {
504
+ // throw new NoRetryError('sortKeyCondition should set both side-hand betweenStartValue/end and value should be number')
505
+ // }
503
506
  // sortKeyName BETWEEN :sortkeyval1 AND :sortkeyval2
504
- payload.KeyConditionExpression += `${queryElements.sortKeyCondition.name} BETWEEN :${queryElements.sortKeyCondition.betweenStartValue} AND :${queryElements.sortKeyCondition.betweenEndValue}`
505
- payload.ExpressionAttributeValues[`:${queryElements.sortKeyCondition.betweenStartValue}`] = queryElements.sortKeyCondition.betweenStartValue
506
- payload.ExpressionAttributeValues[`:${queryElements.sortKeyCondition.betweenEndValue}`] = queryElements.sortKeyCondition.betweenEndValue
507
+ payload.KeyConditionExpression += `${queryElements.sortKeyCondition.name} BETWEEN :betweenStartValue AND :betweenEndValue`
508
+ payload.ExpressionAttributeValues[`:betweenStartValue`] = queryElements.sortKeyCondition.betweenStartValue
509
+ payload.ExpressionAttributeValues[`:betweenEndValue`] = queryElements.sortKeyCondition.betweenEndValue
507
510
 
508
511
 
509
512
  } else { // others comparisons eg. < > <> = , has one side-hand
510
513
 
514
+ // check does not use attributeReservedNames
515
+ if (attributeReservedNames.includes(queryElements.sortKeyCondition.name)) {
516
+ throw new NoRetryError(`sortKeyCondition.name cannot be a attributeReservedName (${queryElements.sortKeyCondition.name})`);
517
+ };
518
+
511
519
  let subKeyConditionalExpression = null
512
520
  if (queryElements.sortKeyCondition.comparison == 'begins_with') { // begins_with ( sortKeyName, :sortkeyval ) ***CANNOT USE WITH NUMBER TYPE
513
521
  if (typeof queryElements.sortKeyCondition.value !== 'string') {
@@ -768,7 +776,7 @@ module.exports.putItem = async (
768
776
 
769
777
  // for (let idx = 0; idx < attributes.length; idx++) { // for loop more performance than for of
770
778
  for (let idx = 0, len = attributes.length; idx < len; idx++) {
771
- if (attributes[idx].action !== 'REMOVE' && attributes[idx].value === undefined) {
779
+ if (attributes[idx].action.toUpperCase() !== 'REMOVE' && attributes[idx].value === undefined) {
772
780
  continue;
773
781
  }
774
782
 
@@ -925,7 +933,7 @@ module.exports.updateItem = async (_izContext, tableName, keyValues, attributes,
925
933
 
926
934
  // ====== for loop to check each element
927
935
  for (let idx = 0, len = attributes.length; idx < len; idx++) {
928
- if (attributes[idx].action !== 'REMOVE' && (attributes[idx].value === undefined)) continue;
936
+ if (attributes[idx].action.toUpperCase() !== 'REMOVE' && (attributes[idx].value === undefined)) continue;
929
937
 
930
938
  // change action string to upperCase
931
939
  attributes[idx].action = attributes[idx].action.toUpperCase()