来自Response的NestJS类型过滤器关系

来自Response的NestJS类型过滤器关系,nestjs,typeorm,Nestjs,Typeorm,在使用Repository.save(myEntity)时,如何确保只返回实体本身而不返回实体的关系 给定这个示例实体 @Entity() 出口级香精{ @许多( 类型=>咖啡, 咖啡=>咖啡口味, ) 咖啡:咖啡[]; } @Entity() 出口级咖啡{ @JoinTable() @许多( 类型=>味道, 风味=>风味咖啡, ) 风味:风味[]; } 还有这个示例服务 异步创建(createCoffeeDto:createCoffeeDto){ const flavors=等待承诺(

在使用
Repository.save(myEntity)
时,如何确保只返回实体本身而不返回实体的关系

给定这个示例实体

@Entity()
出口级香精{
@许多(
类型=>咖啡,
咖啡=>咖啡口味,
)
咖啡:咖啡[];
}
@Entity()
出口级咖啡{
@JoinTable()
@许多(
类型=>味道,
风味=>风味咖啡,
)
风味:风味[];
}
还有这个示例服务


异步创建(createCoffeeDto:createCoffeeDto){
const flavors=等待承诺(
createCoffeeDto.flavors.map(name=>this.preloadFlavorByName(name)),
);
const coffee=this.coffeeRepository.create({
…创建Coffeedto,
口味,
});
返回此.coffeeRepository.save(coffee);
}
如何过滤响应,使其只返回
Coffee
实体,而不返回
Flavor
实体。例如,这个

{
“id”:0,
“名称”:“字符串”,
“品牌”:“字符串”,
“建议”:0,
}
不是这个

{
“id”:0,
“名称”:“字符串”,
“品牌”:“字符串”,
“建议”:0,
“口味”:[
{
“id”:0,
“名称”:“字符串”,
“咖啡”:[
无效的
]
}
]
}

一种方法是使用类转换器。

首先定义响应类,如下所示:

export class CreateCoffeeResponseDto {
   id: number;
   name: string;
   brand: string;
   recommendations: number;
}
然后,从服务返回时,您可以:

return plainToClass(CoffeeCreateResponseDto, await this.coffeeRepository.save(coffee));
此外,如果在关系上放置{cascade:true}options对象,则可以使用.save()调用完成所有操作。

cascade允许隐式更新关系;使用
类转换器
gem确实有效,但我想知道TypeORM本身是否有某种方法。例如,如果关联被更新,我想返回更新后的关联,但如果只更新了主对象,我只想控制它……我基本上想对返回的关联子集进行更细粒度的控制,因为有时可能会有许多嵌套的关联层)