@@ -67,7 +67,7 @@ export class SequelizeRepository<
67
67
options ?: AnyObject ,
68
68
) : Promise < T > {
69
69
const data = await this . sequelizeModel . create ( entity , options ) ;
70
- return data . toJSON ( ) ;
70
+ return this . excludeHiddenProps ( data . toJSON ( ) ) ;
71
71
}
72
72
73
73
// `updateById` is not implemented separately because the existing one in
@@ -105,7 +105,7 @@ export class SequelizeRepository<
105
105
...options ,
106
106
} ) ;
107
107
return data . map ( entity => {
108
- return entity . toJSON ( ) ;
108
+ return this . excludeHiddenProps ( entity . toJSON ( ) ) ;
109
109
} ) ;
110
110
}
111
111
@@ -134,7 +134,7 @@ export class SequelizeRepository<
134
134
throw new EntityNotFoundError ( this . entityClass , id ) ;
135
135
}
136
136
// TODO: include relations in object
137
- return data . toJSON ( ) as T & Relations ;
137
+ return this . excludeHiddenProps ( data . toJSON ( ) ) ;
138
138
}
139
139
140
140
replaceById (
@@ -385,4 +385,22 @@ export class SequelizeRepository<
385
385
}
386
386
return sequelizeDefinition ;
387
387
}
388
+
389
+ /**
390
+ * Remove hidden properties specified in model from response body. (See: https://github.com/sourcefuse/loopback4-sequelize/issues/3)
391
+ * @param entity normalized entity. You can use `entity.toJSON()`'s value
392
+ * @returns normalized entity excluding the hiddenProperties
393
+ */
394
+ private excludeHiddenProps ( entity : T & Relations ) : T & Relations {
395
+ const hiddenProps = this . entityClass . definition . settings . hiddenProperties ;
396
+ if ( ! hiddenProps ) {
397
+ return entity ;
398
+ }
399
+
400
+ for ( const propertyName of hiddenProps as Array < keyof typeof entity > ) {
401
+ delete entity [ propertyName ] ;
402
+ }
403
+
404
+ return entity ;
405
+ }
388
406
}
0 commit comments