serialization - NestJS从snake_case到camelCase的序列化

标签 serialization nestjs class-transformer mikro-orm

我想为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_casecamel_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/

相关文章:

java - 从二进制文件读取并将某些信息转换为字符串

nestjs - Prisma 客户端类型系统与服务方法创建强耦合

nestjs - 如何让 EventPattern 在 Nest JS 中工作

mysql - Typeorm 插入数据,即使它存在

typescript - 如何在 NestJS Controller 中使用 DTO 的验证组?

typescript - 在 nestjs 中使用类验证器验证环境变量?

swagger - NestJS 映射类型 - 嵌套对象

asp.net-mvc - 在 ASP.Net MVC 和 ExtJS 之间序列化和反序列化日期

django - DRF如何获取按类别分组的项目

java - Java 中的序列化,类型代码 00 无效