@@ -1092,6 +1092,57 @@ export class SequelizeCrudRepository<
10921092 }
10931093 }
10941094
1095+ function transformRelatedEntitiesToLoopbackModels (
1096+ entities : T [ ] ,
1097+ entityClass : typeof Entity ,
1098+ ) {
1099+ entities . forEach ( entity => {
1100+ for ( const key in entityClass . definition . relations ) {
1101+ const relation = entityClass . definition . relations [ key ] ;
1102+
1103+ if ( relation && relation . name in entity ) {
1104+ try {
1105+ const TargetLoopbackModel = relation . target ( ) ;
1106+ const relatedEntityOrEntities = entity [ relation . name as keyof T ] ;
1107+
1108+ if ( Array . isArray ( relatedEntityOrEntities ) ) {
1109+ Object . assign ( entity , {
1110+ [ relation . name ] : relatedEntityOrEntities . map (
1111+ relatedEntity => {
1112+ const safeCopy = { ...relatedEntity } ;
1113+ return new TargetLoopbackModel ( safeCopy ) ;
1114+ } ,
1115+ ) ,
1116+ } ) ;
1117+ } else {
1118+ const safeCopy = { ...relatedEntityOrEntities } ;
1119+
1120+ // Handles belongsTo relation which does not include a list of entities
1121+ Object . assign ( entity , {
1122+ [ relation . name ] : new TargetLoopbackModel (
1123+ safeCopy as DataObject < Model > ,
1124+ ) ,
1125+ } ) ;
1126+ }
1127+ } catch ( error ) {
1128+ debug (
1129+ `Error while transforming relation to Loopback model for relation: ${ relation . name } ` ,
1130+ error ,
1131+ ) ;
1132+ }
1133+ }
1134+ }
1135+ } ) ;
1136+ }
1137+
1138+ // Ensure models queried through relations are transformed into Loopback models so that the
1139+ // hidden properties are removed from the response during the "toJSON" transformation.
1140+ // See: https://loopback.io/doc/en/lb4/Model.html#hidden-properties
1141+ transformRelatedEntitiesToLoopbackModels (
1142+ parentEntityInstances ,
1143+ parentEntityClass ,
1144+ ) ;
1145+
10951146 // Validate data type of items in any column having references
10961147 // For eg. convert ["1", "2"] into [1, 2] if `itemType` specified is `number[]`
10971148 parentEntityInstances = parentEntityInstances . map ( entity => {
0 commit comments