From 57aed6e9fc101885bab6f0376e8c10c947de37f4 Mon Sep 17 00:00:00 2001 From: TheNightmareX Date: Sat, 19 Jun 2021 12:41:53 +0800 Subject: [PATCH] feat: avoid populating hidden `Collection` fields --- README.md | 2 +- src/services/mikro-crud-service.factory.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6c024d3..3947057 100644 --- a/README.md +++ b/README.md @@ -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.** diff --git a/src/services/mikro-crud-service.factory.ts b/src/services/mikro-crud-service.factory.ts index c543d76..f28fe1d 100644 --- a/src/services/mikro-crud-service.factory.ts +++ b/src/services/mikro-crud-service.factory.ts @@ -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); }