@@ -392,6 +392,62 @@ class Entity {
392
392
// Merge defaults
393
393
const data = normalizeData ( this . DocumentClient ) ( schema . attributes , linked , Object . assign ( { } , defaults , item ) )
394
394
395
+ // Extract valid options
396
+ const {
397
+ conditions, // ConditionExpression
398
+ capacity, // ReturnConsumedCapacity (none, total, or indexes)
399
+ metrics, // ReturnItemCollectionMetrics: (size or none)
400
+ returnValues, // Return Values (none, all_old, updated_old, all_new, updated_new)
401
+ ..._args
402
+ } = options
403
+
404
+ // Remove other valid options from options
405
+ const args = Object . keys ( _args ) . filter ( x => ! [ 'execute' , 'parse' ] . includes ( x ) )
406
+
407
+ // Error on extraneous arguments
408
+ if ( args . length > 0 )
409
+ error ( `Invalid delete options: ${ args . join ( ', ' ) } ` )
410
+
411
+ // Verify metrics
412
+ if ( metrics !== undefined
413
+ && ( typeof metrics !== 'string' || ! [ 'NONE' , 'SIZE' ] . includes ( metrics . toUpperCase ( ) ) ) )
414
+ error ( `'metrics' must be one of 'NONE' OR 'SIZE'` )
415
+
416
+ // Verify capacity
417
+ if ( capacity !== undefined
418
+ && ( typeof capacity !== 'string' || ! [ 'NONE' , 'TOTAL' , 'INDEXES' ] . includes ( capacity . toUpperCase ( ) ) ) )
419
+ error ( `'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'` )
420
+
421
+ // Verify returnValues
422
+ if ( returnValues !== undefined
423
+ && ( typeof returnValues !== 'string'
424
+ || ! [ 'NONE' , 'ALL_OLD' , 'UPDATED_OLD' , 'ALL_NEW' , 'UPDATED_NEW' ] . includes ( returnValues . toUpperCase ( ) ) ) )
425
+ error ( `'returnValues' must be one of 'NONE', 'ALL_OLD', 'UPDATED_OLD', 'ALL_NEW', OR 'UPDATED_NEW'` )
426
+
427
+ let ConditionExpression // init ConditionExpression
428
+
429
+ // If conditions
430
+ if ( conditions ) {
431
+
432
+ // Parse the conditions
433
+ const {
434
+ expression,
435
+ names,
436
+ values
437
+ } = parseConditions ( conditions , this . table , this . name )
438
+
439
+ if ( Object . keys ( names ) . length > 0 ) {
440
+
441
+ // TODO: alias attribute field names
442
+ // Add names, values and condition expression
443
+ ExpressionAttributeNames = Object . assign ( ExpressionAttributeNames , names )
444
+ ExpressionAttributeValues = Object . assign ( ExpressionAttributeValues , values )
445
+ ConditionExpression = expression
446
+ } // end if names
447
+
448
+ } // end if conditions
449
+
450
+
395
451
// Check for required fields
396
452
Object . keys ( required ) . forEach ( field =>
397
453
required [ field ] && ! data [ field ] && error ( `'${ field } ' is a required field` )
@@ -555,7 +611,7 @@ class Entity {
555
611
) . trim ( )
556
612
557
613
// Merge attribute values
558
- const attr_values = Object . assign ( values , ExpressionAttributeValues )
614
+ ExpressionAttributeValues = Object . assign ( values , ExpressionAttributeValues )
559
615
560
616
// Generate the payload
561
617
const payload = Object . assign (
@@ -566,13 +622,16 @@ class Entity {
566
622
ExpressionAttributeNames : Object . assign ( names , ExpressionAttributeNames )
567
623
} ,
568
624
typeof params === 'object' ? params : { } ,
569
- Object . keys ( attr_values ) . length > 0 ? { ExpressionAttributeValues : attr_values } : { }
625
+ Object . keys ( ExpressionAttributeValues ) . length > 0 ? { ExpressionAttributeValues } : { } ,
626
+ ConditionExpression ? { ConditionExpression } : { }
570
627
) // end assign
571
628
572
629
// console.log(payload)
573
630
574
631
return payload
575
632
633
+ // TODO: Check why primary/secondary GSIs are using if_not_exists
634
+
576
635
} // end updateSync
577
636
578
637
@@ -607,6 +666,65 @@ class Entity {
607
666
// Merge defaults
608
667
const data = normalizeData ( this . DocumentClient ) ( schema . attributes , linked , Object . assign ( { } , defaults , item ) )
609
668
669
+ // Extract valid options
670
+ const {
671
+ conditions, // ConditionExpression
672
+ capacity, // ReturnConsumedCapacity (none, total, or indexes)
673
+ metrics, // ReturnItemCollectionMetrics: (size or none)
674
+ returnValues, // Return Values (none, all_old, updated_old, all_new, updated_new)
675
+ ..._args
676
+ } = options
677
+
678
+ // Remove other valid options from options
679
+ const args = Object . keys ( _args ) . filter ( x => ! [ 'execute' , 'parse' ] . includes ( x ) )
680
+
681
+ // Error on extraneous arguments
682
+ if ( args . length > 0 )
683
+ error ( `Invalid delete options: ${ args . join ( ', ' ) } ` )
684
+
685
+ // Verify metrics
686
+ if ( metrics !== undefined
687
+ && ( typeof metrics !== 'string' || ! [ 'NONE' , 'SIZE' ] . includes ( metrics . toUpperCase ( ) ) ) )
688
+ error ( `'metrics' must be one of 'NONE' OR 'SIZE'` )
689
+
690
+ // Verify capacity
691
+ if ( capacity !== undefined
692
+ && ( typeof capacity !== 'string' || ! [ 'NONE' , 'TOTAL' , 'INDEXES' ] . includes ( capacity . toUpperCase ( ) ) ) )
693
+ error ( `'capacity' must be one of 'NONE','TOTAL', OR 'INDEXES'` )
694
+
695
+ // Verify returnValues
696
+ // TODO: Check this, conflicts with dynalite
697
+ if ( returnValues !== undefined
698
+ && ( typeof returnValues !== 'string'
699
+ || ! [ 'NONE' , 'ALL_OLD' , 'UPDATED_OLD' , 'ALL_NEW' , 'UPDATED_NEW' ] . includes ( returnValues . toUpperCase ( ) ) ) )
700
+ error ( `'returnValues' must be one of 'NONE', 'ALL_OLD', 'UPDATED_OLD', 'ALL_NEW', or 'UPDATED_NEW'` )
701
+
702
+ let ExpressionAttributeNames // init ExpressionAttributeNames
703
+ let ExpressionAttributeValues // init ExpressionAttributeValues
704
+ let ConditionExpression // init ConditionExpression
705
+
706
+ // If conditions
707
+ if ( conditions ) {
708
+
709
+ // Parse the conditions
710
+ const {
711
+ expression,
712
+ names,
713
+ values
714
+ } = parseConditions ( conditions , this . table , this . name )
715
+
716
+ if ( Object . keys ( names ) . length > 0 ) {
717
+
718
+ // TODO: alias attribute field names
719
+ // Add names, values and condition expression
720
+ ExpressionAttributeNames = names
721
+ ExpressionAttributeValues = values
722
+ ConditionExpression = expression
723
+ } // end if names
724
+
725
+ } // end if filters
726
+
727
+
610
728
// Check for required fields
611
729
Object . keys ( required ) . forEach ( field =>
612
730
required [ field ] !== undefined && ! data [ field ] && error ( `'${ field } ' is a required field` )
@@ -631,6 +749,12 @@ class Entity {
631
749
} ) : acc
632
750
} , { } )
633
751
} ,
752
+ ExpressionAttributeNames ? { ExpressionAttributeNames } : null ,
753
+ ExpressionAttributeValues ? { ExpressionAttributeValues } : null ,
754
+ ConditionExpression ? { ConditionExpression } : null ,
755
+ capacity ? { ReturnConsumedCapacity : capacity . toUpperCase ( ) } : null ,
756
+ metrics ? { ReturnItemCollectionMetrics : metrics . toUpperCase ( ) } : null ,
757
+ returnValues ? { ReturnValues : returnValues . toUpperCase ( ) } : null ,
634
758
typeof params === 'object' ? params : { }
635
759
)
636
760
0 commit comments