@@ -28,9 +28,12 @@ import {
2828 SimpleModel ,
2929 SimpleWithCompositePartitionKeyModel ,
3030 SimpleWithPartitionKeyModel ,
31+ SimpleWithRenamedCompositePartitionKeyModel ,
32+ SimpleWithRenamedPartitionKeyModel ,
3133 StringType ,
3234 Type ,
3335} from '../../test/models'
36+ import { ModelWithCombinedDecoratorsModel } from '../../test/models/model-with-combined-decorators.model'
3437import { IdMapper } from '../../test/models/model-with-custom-mapper.model'
3538import { ModelWithEmptyValues } from '../../test/models/model-with-empty-values'
3639import {
@@ -667,10 +670,7 @@ describe('Mapper', () => {
667670
668671 describe ( 'model with autogenerated id' , ( ) => {
669672 it ( 'should create an uuid' , ( ) => {
670- const toDbVal : Attributes < ModelWithAutogeneratedId > = toDb (
671- new ModelWithAutogeneratedId ( ) ,
672- ModelWithAutogeneratedId ,
673- )
673+ const toDbVal = toDb ( new ModelWithAutogeneratedId ( ) , ModelWithAutogeneratedId )
674674 expect ( toDbVal . id ) . toBeDefined ( )
675675 expect ( keyOf ( toDbVal . id ) ) . toBe ( 'S' )
676676 // https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
@@ -680,6 +680,12 @@ describe('Mapper', () => {
680680 } )
681681 } )
682682
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+
683689 describe ( 'model with non string/number/binary keys' , ( ) => {
684690 it ( 'should accept date as HASH or RANGE key' , ( ) => {
685691 const now = new Date ( )
@@ -978,6 +984,13 @@ describe('Mapper', () => {
978984 } )
979985 } )
980986
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+
981994 it ( 'PartitionKey + SortKey' , ( ) => {
982995 const now = new Date ( )
983996 const attrs = createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' , now )
@@ -987,6 +1000,15 @@ describe('Mapper', () => {
9871000 } )
9881001 } )
9891002
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+
9901012 it ( 'should throw when required sortKey is missing' , ( ) => {
9911013 expect ( ( ) => createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' ) ) . toThrow ( )
9921014 } )
@@ -996,17 +1018,27 @@ describe('Mapper', () => {
9961018 it ( 'should throw when model has no defined properties' , ( ) => {
9971019 expect ( ( ) => createToKeyFn ( SimpleModel ) ) . toThrow ( )
9981020 } )
1021+
9991022 it ( 'should throw when given partial has undefined key properties' , ( ) => {
10001023 expect ( ( ) => toKey ( { } , SimpleWithPartitionKeyModel ) ) . toThrow ( )
10011024 expect ( ( ) => toKey ( { id : 'myId' } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
10021025 expect ( ( ) => toKey ( { creationDate : new Date ( ) } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
10031026 } )
1027+
10041028 it ( 'should create key attributes of simple key' , ( ) => {
10051029 const key = toKey ( { id : 'myId' } , SimpleWithPartitionKeyModel )
10061030 expect ( key ) . toEqual ( {
10071031 id : { S : 'myId' } ,
10081032 } )
10091033 } )
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+
10101042 it ( 'should create key attributes of composite key' , ( ) => {
10111043 const partial : Partial < SimpleWithCompositePartitionKeyModel > = { id : 'myId' , creationDate : new Date ( ) }
10121044 const key = toKey ( partial , SimpleWithCompositePartitionKeyModel )
@@ -1015,6 +1047,16 @@ describe('Mapper', () => {
10151047 creationDate : { S : partial . creationDate ! . toISOString ( ) } ,
10161048 } )
10171049 } )
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+
10181060 it ( 'should create key with custom mapper' , ( ) => {
10191061 const partial : ModelWithCustomMapperModel = { id : new Id ( 7 , 2018 ) }
10201062 const key = toKey ( partial , ModelWithCustomMapperModel )
0 commit comments