55 DbClientContract ,
66 FieldInfo ,
77 PrismaErrorCode ,
8+ clone ,
89 enumerate ,
910 getIdFields ,
1011 isPrismaClientKnownRequestError ,
@@ -1120,22 +1121,8 @@ class RequestHandler extends APIHandlerBase {
11201121 throw new Error ( `serializer not found for model ${ model } ` ) ;
11211122 }
11221123
1123- const typeInfo = this . typeMap [ model ] ;
1124-
1125- let itemsWithId : any = items ;
1126- if ( typeInfo . idFields . length > 1 && Array . isArray ( items ) ) {
1127- itemsWithId = items . map ( ( item : any ) => {
1128- return {
1129- ...item ,
1130- [ this . makeIdKey ( typeInfo . idFields ) ] : this . makeCompoundId ( typeInfo . idFields , item ) ,
1131- } ;
1132- } ) ;
1133- } else if ( typeInfo . idFields . length > 1 && typeof items === 'object' ) {
1134- itemsWithId = {
1135- ...items ,
1136- [ this . makeIdKey ( typeInfo . idFields ) ] : this . makeCompoundId ( typeInfo . idFields , items ) ,
1137- } ;
1138- }
1124+ const itemsWithId = clone ( items ) ;
1125+ this . injectCompoundId ( model , itemsWithId ) ;
11391126
11401127 // serialize to JSON:API structure
11411128 const serialized = await serializer . serialize ( itemsWithId , options ) ;
@@ -1154,6 +1141,31 @@ class RequestHandler extends APIHandlerBase {
11541141 return result ;
11551142 }
11561143
1144+ private injectCompoundId ( model : string , items : unknown ) {
1145+ const typeInfo = this . typeMap [ lowerCaseFirst ( model ) ] ;
1146+ if ( ! typeInfo ) {
1147+ return ;
1148+ }
1149+
1150+ // recursively traverse the entity to create synthetic ID field for models with compound ID
1151+ enumerate ( items ) . forEach ( ( item : any ) => {
1152+ if ( ! item ) {
1153+ return ;
1154+ }
1155+
1156+ if ( typeInfo . idFields . length > 1 ) {
1157+ item [ this . makeIdKey ( typeInfo . idFields ) ] = this . makeCompoundId ( typeInfo . idFields , item ) ;
1158+ }
1159+
1160+ for ( const [ key , value ] of Object . entries ( item ) ) {
1161+ if ( typeInfo . relationships [ key ] ) {
1162+ // field is a relationship, recurse
1163+ this . injectCompoundId ( typeInfo . relationships [ key ] . type , value ) ;
1164+ }
1165+ }
1166+ } ) ;
1167+ }
1168+
11571169 private toPlainObject ( data : any ) : any {
11581170 if ( data === undefined || data === null ) {
11591171 return data ;
0 commit comments