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