Skip to content

Commit

Permalink
feat: avoid populating hidden Collection fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Char2sGu committed Jun 19, 2021
1 parent d3692a2 commit 57aed6e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class BooksService /* extends ... */ {

## Populating Relations

By default, `Collection` fields of the entity will be populated mandatorily. The user can populate relations by passing the `expand` query param like `book.owner.profile`, and you could specify which fields is allowed to be expanded by specifying the `expand.in` option when creating the query DTO.
By default, the unhidden `Collection` fields of the entity will be populated mandatorily. The user can populate relations by passing the `expand` query param like `book.owner.profile`, and you could specify which fields is allowed to be expanded by specifying the `expand.in` option when creating the query DTO.

**You could populate any relations when handling the request and don't need to worry there will be extra relations expanded unexpectedly in the response. All the entities will be processed before responding to ensure only the relations mentioned in the `expand` query param are marked as _populated_ to shape the response, therefore, although `Collection` fields are populated mandatorily, they will be only an array of primary keys in the response if they are not mentioned in the `expand` query param.**

Expand Down
7 changes: 4 additions & 3 deletions src/services/mikro-crud-service.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export class MikroCrudServiceFactory<

this.collectionFields = this.entityMeta.relations
.filter(
({ reference }) =>
reference == ReferenceType.ONE_TO_MANY ||
reference == ReferenceType.MANY_TO_MANY
({ reference, hidden }) =>
!hidden &&
(reference == ReferenceType.ONE_TO_MANY ||
reference == ReferenceType.MANY_TO_MANY)
)
.map(({ name }) => name as NonFunctionPropertyNames<Entity>);
}
Expand Down

0 comments on commit 57aed6e

Please sign in to comment.