@@ -28,6 +28,8 @@ import {
2828 SimpleModel ,
2929 SimpleWithCompositePartitionKeyModel ,
3030 SimpleWithPartitionKeyModel ,
31+ SimpleWithRenamedCompositePartitionKeyModel ,
32+ SimpleWithRenamedPartitionKeyModel ,
3133 StringType ,
3234 Type ,
3335} from '../../test/models'
@@ -667,10 +669,7 @@ describe('Mapper', () => {
667669
668670 describe ( 'model with autogenerated id' , ( ) => {
669671 it ( 'should create an uuid' , ( ) => {
670- const toDbVal : Attributes < ModelWithAutogeneratedId > = toDb (
671- new ModelWithAutogeneratedId ( ) ,
672- ModelWithAutogeneratedId ,
673- )
672+ const toDbVal = toDb ( new ModelWithAutogeneratedId ( ) , ModelWithAutogeneratedId )
674673 expect ( toDbVal . id ) . toBeDefined ( )
675674 expect ( keyOf ( toDbVal . id ) ) . toBe ( 'S' )
676675 // https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
@@ -680,6 +679,12 @@ describe('Mapper', () => {
680679 } )
681680 } )
682681
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+
683688 describe ( 'model with non string/number/binary keys' , ( ) => {
684689 it ( 'should accept date as HASH or RANGE key' , ( ) => {
685690 const now = new Date ( )
@@ -978,6 +983,13 @@ describe('Mapper', () => {
978983 } )
979984 } )
980985
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+
981993 it ( 'PartitionKey + SortKey' , ( ) => {
982994 const now = new Date ( )
983995 const attrs = createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' , now )
@@ -987,6 +999,15 @@ describe('Mapper', () => {
987999 } )
9881000 } )
9891001
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+
9901011 it ( 'should throw when required sortKey is missing' , ( ) => {
9911012 expect ( ( ) => createKeyAttributes ( metadataForModel ( SimpleWithCompositePartitionKeyModel ) , 'myId' ) ) . toThrow ( )
9921013 } )
@@ -996,17 +1017,27 @@ describe('Mapper', () => {
9961017 it ( 'should throw when model has no defined properties' , ( ) => {
9971018 expect ( ( ) => createToKeyFn ( SimpleModel ) ) . toThrow ( )
9981019 } )
1020+
9991021 it ( 'should throw when given partial has undefined key properties' , ( ) => {
10001022 expect ( ( ) => toKey ( { } , SimpleWithPartitionKeyModel ) ) . toThrow ( )
10011023 expect ( ( ) => toKey ( { id : 'myId' } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
10021024 expect ( ( ) => toKey ( { creationDate : new Date ( ) } , SimpleWithCompositePartitionKeyModel ) ) . toThrow ( )
10031025 } )
1026+
10041027 it ( 'should create key attributes of simple key' , ( ) => {
10051028 const key = toKey ( { id : 'myId' } , SimpleWithPartitionKeyModel )
10061029 expect ( key ) . toEqual ( {
10071030 id : { S : 'myId' } ,
10081031 } )
10091032 } )
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+
10101041 it ( 'should create key attributes of composite key' , ( ) => {
10111042 const partial : Partial < SimpleWithCompositePartitionKeyModel > = { id : 'myId' , creationDate : new Date ( ) }
10121043 const key = toKey ( partial , SimpleWithCompositePartitionKeyModel )
@@ -1015,6 +1046,16 @@ describe('Mapper', () => {
10151046 creationDate : { S : partial . creationDate ! . toISOString ( ) } ,
10161047 } )
10171048 } )
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+
10181059 it ( 'should create key with custom mapper' , ( ) => {
10191060 const partial : ModelWithCustomMapperModel = { id : new Id ( 7 , 2018 ) }
10201061 const key = toKey ( partial , ModelWithCustomMapperModel )
0 commit comments