@@ -55,8 +55,7 @@ export type Options = {
55
55
56
56
type RelationshipInfo = {
57
57
type : string ;
58
- idField : string ;
59
- idFieldType : string ;
58
+ idFields : FieldInfo [ ] ;
60
59
isCollection : boolean ;
61
60
isOptional : boolean ;
62
61
} ;
@@ -773,22 +772,24 @@ class RequestHandler extends APIHandlerBase {
773
772
if ( relationInfo . isCollection ) {
774
773
createPayload . data [ key ] = {
775
774
connect : enumerate ( data . data ) . map ( ( item : any ) => ( {
776
- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
775
+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
777
776
} ) ) ,
778
777
} ;
779
778
} else {
780
779
if ( typeof data . data !== 'object' ) {
781
780
return this . makeError ( 'invalidRelationData' ) ;
782
781
}
783
782
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
+ } ,
785
786
} ;
786
787
}
787
788
788
789
// make sure ID fields are included for result serialization
789
790
createPayload . include = {
790
791
...createPayload . include ,
791
- [ key ] : { select : { [ relationInfo . idField ] : true } } ,
792
+ [ key ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
792
793
} ;
793
794
}
794
795
}
@@ -829,7 +830,7 @@ class RequestHandler extends APIHandlerBase {
829
830
where : this . makeIdFilter ( typeInfo . idFields , resourceId ) ,
830
831
select : {
831
832
...typeInfo . idFields . reduce ( ( acc , field ) => ( { ...acc , [ field . name ] : true } ) , { } ) ,
832
- [ relationship ] : { select : { [ relationInfo . idField ] : true } } ,
833
+ [ relationship ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
833
834
} ,
834
835
} ;
835
836
@@ -861,7 +862,7 @@ class RequestHandler extends APIHandlerBase {
861
862
updateArgs . data = {
862
863
[ relationship ] : {
863
864
connect : {
864
- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , parsed . data . data . id ) ,
865
+ [ this . makeIdKey ( relationInfo . idFields ) ] : parsed . data . data . id ,
865
866
} ,
866
867
} ,
867
868
} ;
@@ -885,7 +886,7 @@ class RequestHandler extends APIHandlerBase {
885
886
updateArgs . data = {
886
887
[ relationship ] : {
887
888
[ relationVerb ] : enumerate ( parsed . data . data ) . map ( ( item : any ) => ( {
888
- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
889
+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
889
890
} ) ) ,
890
891
} ,
891
892
} ;
@@ -945,20 +946,22 @@ class RequestHandler extends APIHandlerBase {
945
946
if ( relationInfo . isCollection ) {
946
947
updatePayload . data [ key ] = {
947
948
set : enumerate ( data . data ) . map ( ( item : any ) => ( {
948
- [ relationInfo . idField ] : this . coerce ( relationInfo . idFieldType , item . id ) ,
949
+ [ this . makeIdKey ( relationInfo . idFields ) ] : item . id ,
949
950
} ) ) ,
950
951
} ;
951
952
} else {
952
953
if ( typeof data . data !== 'object' ) {
953
954
return this . makeError ( 'invalidRelationData' ) ;
954
955
}
955
956
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
+ } ,
957
960
} ;
958
961
}
959
962
updatePayload . include = {
960
963
...updatePayload . include ,
961
- [ key ] : { select : { [ relationInfo . idField ] : true } } ,
964
+ [ key ] : { select : { [ this . makeIdKey ( relationInfo . idFields ) ] : true } } ,
962
965
} ;
963
966
}
964
967
}
@@ -1015,14 +1018,9 @@ class RequestHandler extends APIHandlerBase {
1015
1018
continue ;
1016
1019
}
1017
1020
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
-
1022
1021
this . typeMap [ model ] . relationships [ field ] = {
1023
1022
type : fieldInfo . type ,
1024
- idField,
1025
- idFieldType,
1023
+ idFields : fieldTypeIdFields ,
1026
1024
isCollection : ! ! fieldInfo . isArray ,
1027
1025
isOptional : ! ! fieldInfo . isOptional ,
1028
1026
} ;
@@ -1257,7 +1255,7 @@ class RequestHandler extends APIHandlerBase {
1257
1255
return ;
1258
1256
}
1259
1257
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 } } } ;
1261
1259
}
1262
1260
}
1263
1261
0 commit comments