Skip to content
This repository was archived by the owner on Sep 4, 2024. It is now read-only.

Commit 3e254fd

Browse files
committed
fix(repository): exclude hidden properties in response body
closes #3
1 parent 9368548 commit 3e254fd

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/sequelize/sequelize.repository.base.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class SequelizeRepository<
6767
options?: AnyObject,
6868
): Promise<T> {
6969
const data = await this.sequelizeModel.create(entity, options);
70-
return data.toJSON();
70+
return this.excludeHiddenProps(data.toJSON());
7171
}
7272

7373
// `updateById` is not implemented separately because the existing one in
@@ -105,7 +105,7 @@ export class SequelizeRepository<
105105
...options,
106106
});
107107
return data.map(entity => {
108-
return entity.toJSON();
108+
return this.excludeHiddenProps(entity.toJSON());
109109
});
110110
}
111111

@@ -134,7 +134,7 @@ export class SequelizeRepository<
134134
throw new EntityNotFoundError(this.entityClass, id);
135135
}
136136
// TODO: include relations in object
137-
return data.toJSON() as T & Relations;
137+
return this.excludeHiddenProps(data.toJSON());
138138
}
139139

140140
replaceById(
@@ -385,4 +385,22 @@ export class SequelizeRepository<
385385
}
386386
return sequelizeDefinition;
387387
}
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+
}
388406
}

0 commit comments

Comments
 (0)