Closed GustavoContreiras-Feegow closed 2 years ago
My controller
import { CacheTTL, Controller, Req } from '@nestjs/common'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { Crud, CrudController, CrudRequest, Override, ParsedRequest } from '@nestjsx/crud'; import { Request } from 'express'; import PermissionUtils from 'utils/permission_utils'; import { defaultCrudOptions } from '../../constants'; import { Professional } from '../../entities/professional.entity'; import { ProfessionalsService } from './professionals.service'; @Crud({ ...defaultCrudOptions, model: { type: Professional }, query: { ...defaultCrudOptions.query, join: { tratamento: { allow: ['nome'], exclude: ['id'], eager: false // false: LEFT JOIN, true: INNER JOIN } } } }) @Controller('profissionais') @ApiBearerAuth() @ApiTags('Profissionais') export class ProfessionalsController implements CrudController<Professional> { constructor(public service: ProfessionalsService) { } get base(): CrudController<Professional> { return this; } @Override('getManyBase') @CacheTTL(60 * 60 * 6) // 6 hours getMany( @Req() request: Request, @ParsedRequest() crudRequest: CrudRequest, ) { return PermissionUtils.verify(request, 'GET', 'profissionais') .then(() => { return this.base.getManyBase(crudRequest) }) } @Override('getOneBase') @CacheTTL(60 * 60 * 6) // 6 hours getOne( @Req() request: Request, @ParsedRequest() crudRequest: CrudRequest, ) { return PermissionUtils.verify(request, 'GET', 'profissionais/{id}') .then(() => this.base.getOneBase(crudRequest)) } }
My entity
import { ApiProperty } from '@nestjs/swagger'; import { IsInt, IsOptional } from 'class-validator'; import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm'; import { Treatment } from './treatment.entity'; @Entity('profissionais') export class Professional { @ApiProperty() @PrimaryGeneratedColumn() id: number; @ApiProperty() @Column({ name: 'NomeProfissional' }) nome: string; @ApiProperty() @Column({ name: 'TratamentoID' }) @IsOptional({ always: true }) @IsInt() tratamentoId: number; @ApiProperty() @OneToOne(() => Treatment) @JoinColumn({ name: 'TratamentoID' }) tratamento: Treatment; }
My request http://localhost:9002/api/profissionais/123123?join=tratamento%7C%7Cnome&cache=0' \
http://localhost:9002/api/profissionais/123123?join=tratamento%7C%7Cnome&cache=0' \
Response received
{ "id": 123123, "nome": "GUSTAVO", "tratamentoId": 1, "tratamento": { "id": 1, "nome": "" } }
The property 'id' of the joined object 'tratamento' should not be shown!
Just found the problem... "id" is a @PrimaryGeneratedColumn and this can't be hide
@PrimaryGeneratedColumn
My controller
My entity
My request
http://localhost:9002/api/profissionais/123123?join=tratamento%7C%7Cnome&cache=0' \
Response received
The property 'id' of the joined object 'tratamento' should not be shown!