我想为NestJS Controller 实现JSON请求/响应主体的自动序列化/反序列化,准确地说,是自动转换snake_case
请求正文 JSON key 到 camelCase
在我的 Controller 处理程序处收到,反之亦然。
我发现是使用 class-transformer
的 @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
但我也不确定如何实现。谢谢!
最佳答案
您可以使用 mapResult()
来自 ORM 的方法,它负责将原始数据库结果(所以对于你来说是 snake_case)映射到实体属性名称(对于你来说是驼峰式大小写):
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
更改 key (默认为基于所选命名策略的值)。
关于serialization - NestJS从snake_case到camelCase的序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62980087/