5
5
DbClientContract ,
6
6
FieldInfo ,
7
7
PrismaErrorCode ,
8
+ clone ,
8
9
enumerate ,
9
10
getIdFields ,
10
11
isPrismaClientKnownRequestError ,
@@ -1120,22 +1121,8 @@ class RequestHandler extends APIHandlerBase {
1120
1121
throw new Error ( `serializer not found for model ${ model } ` ) ;
1121
1122
}
1122
1123
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 ) ;
1139
1126
1140
1127
// serialize to JSON:API structure
1141
1128
const serialized = await serializer . serialize ( itemsWithId , options ) ;
@@ -1154,6 +1141,31 @@ class RequestHandler extends APIHandlerBase {
1154
1141
return result ;
1155
1142
}
1156
1143
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
+
1157
1169
private toPlainObject ( data : any ) : any {
1158
1170
if ( data === undefined || data === null ) {
1159
1171
return data ;
0 commit comments