@@ -55,8 +55,7 @@ export type Options = {
5555
5656type RelationshipInfo = {
5757 type : string ;
58- idField : string ;
59- idFieldType : string ;
58+ idFields : FieldInfo [ ] ;
6059 isCollection : boolean ;
6160 isOptional : boolean ;
6261} ;
@@ -773,22 +772,24 @@ class RequestHandler extends APIHandlerBase {
773772 if ( relationInfo . isCollection ) {
774773 createPayload . data [ key ] = {
775774 connect : enumerate ( data . data ) . map ( ( item : any ) => ( {
776- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
775+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
777776 } ) ) ,
778777 } ;
779778 } else {
780779 if ( typeof data . data !== 'object' ) {
781780 return this . makeError ( 'invalidRelationData' ) ;
782781 }
783782 createPayload . data [ key ] = {
784- connect : { [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , data . data . id ) } ,
783+ connect : {
784+ [ this . makeIdKey ( relationInfo . idFields ) ] : data . data . id ,
785+ } ,
785786 } ;
786787 }
787788
788789 // make sure ID fields are included for result serialization
789790 createPayload . include = {
790791 ...createPayload . include ,
791- [ key ] : { select : { [ relationInfo . idField ] : true } } ,
792+ [ key ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
792793 } ;
793794 }
794795 }
@@ -829,7 +830,7 @@ class RequestHandler extends APIHandlerBase {
829830 where : this . makeIdFilter ( typeInfo . idFields , resourceId ) ,
830831 select : {
831832 ...typeInfo . idFields . reduce ( ( acc , field ) => ( { ...acc , [ field . name ] : true } ) , { } ) ,
832- [ relationship ] : { select : { [ relationInfo . idField ] : true } } ,
833+ [ relationship ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
833834 } ,
834835 } ;
835836
@@ -861,7 +862,7 @@ class RequestHandler extends APIHandlerBase {
861862 updateArgs . data = {
862863 [ relationship ] : {
863864 connect : {
864- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , parsed . data . data . id ) ,
865+ [ this . makeIdKey ( relationInfo . idFields ) ] : parsed . data . data . id ,
865866 } ,
866867 } ,
867868 } ;
@@ -885,7 +886,7 @@ class RequestHandler extends APIHandlerBase {
885886 updateArgs . data = {
886887 [ relationship ] : {
887888 [ relationVerb ] : enumerate ( parsed . data . data ) . map ( ( item : any ) => ( {
888- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
889+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
889890 } ) ) ,
890891 } ,
891892 } ;
@@ -945,20 +946,22 @@ class RequestHandler extends APIHandlerBase {
945946 if ( relationInfo . isCollection ) {
946947 updatePayload . data [ key ] = {
947948 set : enumerate ( data . data ) . map ( ( item : any ) => ( {
948- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
949+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
949950 } ) ) ,
950951 } ;
951952 } else {
952953 if ( typeof data . data !== 'object' ) {
953954 return this . makeError ( 'invalidRelationData' ) ;
954955 }
955956 updatePayload . data [ key ] = {
956- set : { [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , data . data . id ) } ,
957+ set : {
958+ [ this . makeIdKey ( relationInfo . idFields ) ] : data . data . id ,
959+ } ,
957960 } ;
958961 }
959962 updatePayload . include = {
960963 ...updatePayload . include ,
961- [ key ] : { select : { [ relationInfo . idField ] : true } } ,
964+ [ key ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
962965 } ;
963966 }
964967 }
@@ -1015,14 +1018,9 @@ class RequestHandler extends APIHandlerBase {
10151018 continue ;
10161019 }
10171020
1018- // TODO: Multi id relationship support
1019- const idField = fieldTypeIdFields . length > 1 ? 'id' : fieldTypeIdFields [ 0 ] . name ;
1020- const idFieldType = fieldTypeIdFields . length > 1 ? 'string' : fieldTypeIdFields [ 0 ] . type ;
1021-
10221021 this . typeMap [ model ] . relationships [ field ] = {
10231022 type : fieldInfo . type ,
1024- idField,
1025- idFieldType,
1023+ idFields : fieldTypeIdFields ,
10261024 isCollection : ! ! fieldInfo . isArray ,
10271025 isOptional : ! ! fieldInfo . isOptional ,
10281026 } ;
@@ -1257,7 +1255,7 @@ class RequestHandler extends APIHandlerBase {
12571255 return ;
12581256 }
12591257 for ( const [ relation , relationInfo ] of Object . entries ( typeInfo . relationships ) ) {
1260- args [ mode ] = { ...args [ mode ] , [ relation ] : { select : { [ relationInfo . idField ] : true } } } ;
1258+ args [ mode ] = { ...args [ mode ] , [ relation ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } } ;
12611259 }
12621260 }
12631261
0 commit comments