3

我想为 NestJS 控制器实现 JSON 请求/响应主体的自动序列化/反序列化,准确地说,自动将snake_case请求主体 JSON 键转换为camelCase在我的控制器处理程序处接收,反之亦然。

我发现使用class-transformer's @Expose({ name: 'selling_price' }),如下例所示(我使用的是 MikroORM):

// recipe.entity.ts
@Entity()
export class Recipe extends BaseEntity {
  @Property()
  name: string;
  
  @Expose({ name: 'selling_price' })
  @Property()
  sellingPrice: number;
}
// recipe.controller.ts
@Controller('recipes')
export class RecipeController {
  constructor(private readonly service: RecipeService) {}

  @Post()
  async createOne(@Body() data: Recipe): Promise<Recipe> {
    console.log(data);
    return this.service.createOne(data);
  }
}
// example request body
{
    "name": "Recipe 1",
    "selling_price": 50000
}
// log on the RecipeController.createOne handler method
{ name: 'Recipe 1',
  selling_price: 50000 }

// what I wanted on the log
{ name: 'Recipe 1',
  sellingPrice: 50000 }

可以看到@Expose注释完美地工作,但更进一步,我希望能够将其转换为实体上的属性名称:sellingPrice,因此我可以将解析的请求正文直接传递给我的服务和我的存储库方法this.recipeRepository.create(data)。当前条件是该sellingPrice字段将为空,因为存在该selling_price字段。如果我不使用@Expose,则需要写入请求 JSON camelCase,这不是我喜欢的。

我可以执行 DTO 和构造函数以及分配字段,但我认为这是相当重复的,并且由于我的命名偏好,我将有很多字段要转换:snake_case在 JSON 和数据库列以及camelCase所有 JS/TS 部分上。

有没有办法可以干净利落地做这个把戏?也许已经有解决方案了。snake_case也许是一个可以全部转换为的全局拦截器,camel_case但我也不确定如何实现一个。

谢谢!

4

1 回答 1

1

您可以使用mapResult()ORM 中的方法,该方法负责将原始数据库结果(对您而言是 snake_case)映射到实体属性名称(对您而言是 camelCase):

const meta = em.getMetadata().get('Recipe');
const data = {
  name: 'Recipe 1',
  selling_price: 50000,
};
const res = em.getDriver().mapResult(data, meta);
console.log(res); // dumps `{ name: 'Recipe 1', sellingPrice: 50000 }`

此方法基于实体元数据进行操作,将键从fieldName(默认为基于所选命名策略的值)更改。

于 2020-07-20T08:44:10.887 回答