@@ -28,6 +28,8 @@ import {
28
28
SimpleModel ,
29
29
SimpleWithCompositePartitionKeyModel ,
30
30
SimpleWithPartitionKeyModel ,
31
+ SimpleWithRenamedCompositePartitionKeyModel ,
32
+ SimpleWithRenamedPartitionKeyModel ,
31
33
StringType ,
32
34
Type ,
33
35
} from '../../test/models'
@@ -667,10 +669,7 @@ describe('Mapper', () => {
667
669
668
670
describe ( 'model with autogenerated id' , ( ) => {
669
671
it ( 'should create an uuid' , ( ) => {
670
- const toDbVal : Attributes < ModelWithAutogeneratedId > = toDb (
671
- new ModelWithAutogeneratedId ( ) ,
672
- ModelWithAutogeneratedId ,
673
- )
672
+ const toDbVal = toDb ( new ModelWithAutogeneratedId ( ) , ModelWithAutogeneratedId )
674
673
expect ( toDbVal . id ) . toBeDefined ( )
675
674
expect ( keyOf ( toDbVal . id ) ) . toBe ( 'S' )
676
675
// https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
@@ -680,6 +679,12 @@ describe('Mapper', () => {
680
679
} )
681
680
} )
682
681
682
+ describe ( 'model with combined decorators' , ( ) => {
683
+ const toDbValue : SimpleWithRenamedPartitionKeyModel = { id : 'idValue' , age : 30 }
684
+ const mapped = toDb ( toDbValue , SimpleWithRenamedPartitionKeyModel )
685
+ expect ( mapped ) . toEqual ( { custom_id : { S : 'idValue' } , age : { N : '30' } } )
686
+ } )
687
+
683
688
describe ( 'model with non string/number/binary keys' , ( ) => {
684
689
it ( 'should accept date as HASH or RANGE key' , ( ) => {
685
690
const now = new Date ( )
@@ -978,6 +983,13 @@ describe('Mapper', () => {
978
983
} )
979
984
} )
980
985
986
+ it ( 'PartitionKey only (custom db name)' , ( ) => {
987
+ const attrs = createKeyAttributes ( metadataForModel ( SimpleWithRenamedPartitionKeyModel ) , 'myId' )
988
+ expect ( attrs ) . toEqual ( {
989
+ custom_id : { S : 'myId' } ,
990
+ } )
991
+ } )
992
+
981
993
it ( 'PartitionKey + SortKey' , ( ) => {
982
994
const now = new Date ( )
983
995
const attrs = createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' , now )
@@ -987,6 +999,15 @@ describe('Mapper', () => {
987
999
} )
988
1000
} )
989
1001
1002
+ it ( 'PartitionKey + SortKey (custom db name)' , ( ) => {
1003
+ const now = new Date ( )
1004
+ const attrs = createKeyAttributes ( metadataForModel ( SimpleWithRenamedCompositePartitionKeyModel ) , 'myId' , now )
1005
+ expect ( attrs ) . toEqual ( {
1006
+ custom_id : { S : 'myId' } ,
1007
+ custom_date : { S : now . toISOString ( ) } ,
1008
+ } )
1009
+ } )
1010
+
990
1011
it ( 'should throw when required sortKey is missing' , ( ) => {
991
1012
expect ( ( ) => createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' ) ) . toThrow ( )
992
1013
} )
@@ -996,17 +1017,27 @@ describe('Mapper', () => {
996
1017
it ( 'should throw when model has no defined properties' , ( ) => {
997
1018
expect ( ( ) => createToKeyFn ( SimpleModel ) ) . toThrow ( )
998
1019
} )
1020
+
999
1021
it ( 'should throw when given partial has undefined key properties' , ( ) => {
1000
1022
expect ( ( ) => toKey ( { } , SimpleWithPartitionKeyModel ) ) . toThrow ( )
1001
1023
expect ( ( ) => toKey ( { id : 'myId' } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
1002
1024
expect ( ( ) => toKey ( { creationDate : new Date ( ) } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
1003
1025
} )
1026
+
1004
1027
it ( 'should create key attributes of simple key' , ( ) => {
1005
1028
const key = toKey ( { id : 'myId' } , SimpleWithPartitionKeyModel )
1006
1029
expect ( key ) . toEqual ( {
1007
1030
id : { S : 'myId' } ,
1008
1031
} )
1009
1032
} )
1033
+
1034
+ it ( 'should create key attributes of simple key (custom db name)' , ( ) => {
1035
+ const key = toKey ( { id : 'myId' } , SimpleWithRenamedPartitionKeyModel )
1036
+ expect ( key ) . toEqual ( {
1037
+ custom_id : { S : 'myId' } ,
1038
+ } )
1039
+ } )
1040
+
1010
1041
it ( 'should create key attributes of composite key' , ( ) => {
1011
1042
const partial : Partial < SimpleWithCompositePartitionKeyModel > = { id : 'myId' , creationDate : new Date ( ) }
1012
1043
const key = toKey ( partial , SimpleWithCompositePartitionKeyModel )
@@ -1015,6 +1046,16 @@ describe('Mapper', () => {
1015
1046
creationDate : { S : partial . creationDate ! . toISOString ( ) } ,
1016
1047
} )
1017
1048
} )
1049
+
1050
+ it ( 'should create key attributes of composite key (custom db name)' , ( ) => {
1051
+ const partial : Partial < SimpleWithRenamedCompositePartitionKeyModel > = { id : 'myId' , creationDate : new Date ( ) }
1052
+ const key = toKey ( partial , SimpleWithRenamedCompositePartitionKeyModel )
1053
+ expect ( key ) . toEqual ( {
1054
+ custom_id : { S : partial . id ! } ,
1055
+ custom_date : { S : partial . creationDate ! . toISOString ( ) } ,
1056
+ } )
1057
+ } )
1058
+
1018
1059
it ( 'should create key with custom mapper' , ( ) => {
1019
1060
const partial : ModelWithCustomMapperModel = { id : new Id ( 7 , 2018 ) }
1020
1061
const key = toKey ( partial , ModelWithCustomMapperModel )
0 commit comments