将实体投射到 dto [英] Cast entity to dto

查看:52
本文介绍了将实体投射到 dto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道将 NestJS 实体对象转换为 DTO 的最佳方法.

Just wondering the best way to convert a NestJS entity object to a DTO.

假设我有以下内容:

import { IsString, IsNumber, IsBoolean } from 'class-validator';
import { Exclude } from 'class-transformer';

export class PhotoSnippetDto {
  @IsNumber()
  readonly id: number;

  @IsString()
  readonly name: string;

  constructor(props) {
    Object.assign(this, props);
  }
}

export class Photo {

  @IsNumber()
  id: number;

  @IsString()
  name: string;

  @IsString()
  description: string;

  @IsString()
  filename: string;

  @IsNumber()
  views: number;

  @IsBoolean()
  isPublished: boolean;

  @Exclude()
  @IsString()
  excludedPropery: string;

  constructor(props) {
    Object.assign(this, props);
  }
}

@Controller()
export class AppController {

  @Get()
  @UseInterceptors(ClassSerializerInterceptor)
  root(): PhotoSnippetDto {
    const photo = new Photo({
      id: 1,
      name: 'Photo 1',
      description: 'Photo 1 description',
      filename: 'photo.png',
      views: 10,
      isPublished: true,
      excludedPropery: 'Im excluded'
    });

    return new PhotoSnippetDto(photo);
  }

}

我期待 ClassSerializerInterceptor 将照片对象序列化到 DTO 并返回如下内容:

I was expecting the ClassSerializerInterceptor to serialize the photo object to the DTO and return something like this:

{
  id: 1,
  name: 'Photo 1'
}

但我仍然收到包含所有属性的响应:

But I'm getting a response containing all the properties still:

{
  id = 1,
  name = 'Photo 1',
  description = 'Photo 1 description',
  filename = 'file.png',
  views = 10,
  isPublished = true
}

我基本上想去掉所有未在 DTO 中定义的属性.

I basically want to strip out all properties that are not defined in the DTO.

我知道在使用 @Exclude() 时 ClassSerializerInterceptor 工作得很好,我也希望它也删除未定义的属性.

I know the ClassSerializerInterceptor works perfectly when using @Exclude(), I was just also expecting it to remove undefined properties also.

我很好奇解决这个问题的最佳方法是什么?我知道我可以这样做:

I'm curious as to the best way to go about this? I know I could do something like:

@Get('test')
@UseInterceptors(ClassSerializerInterceptor)
test(): PhotoSnippetDto {
  const photo = new Photo({
    id: 1,
    name: 'Photo 1',
    description: 'Photo 1 description',
    filename: 'photo.png',
    views: 10,
    isPublished: true,
    excludedPropery: 'Im excluded'
  });
  const { id, name } = photo;
  return new PhotoSnippetDto({id, name});
}

但如果我想在响应中添加另一个属性,我必须做的不仅仅是将新属性添加到类中.我想知道是否有更好的嵌套方式"来做到这一点.

But if I ever want to add another property to the response I'd have to do more than just add the new property to the class.. I'm wondering if there's a better 'Nest way' of doing it.

推荐答案

一种可能的选择是用 @Exclude@Expose 装饰器标记你的 DTO 对象,然后使用 plainToClass 进行转换:

One possible option is to mark your DTO object with the @Exclude and @Expose decorators and then do a conversion with plainToClass:

@Exclude()
export class PhotoSnippetDto {
   @Expose()
   @IsNumber()
   readonly id: number;

   @Expose()
   @IsString()
   readonly name: string;
}

假设您已经按照上述方式进行了装饰,那么您可以执行以下操作:const dto = plainToClass(PhotoSnippetDto, photo);

Assuming you've decorated as above you can then do: const dto = plainToClass(PhotoSnippetDto, photo);

结果对象是您期望的形式,只有 idname 显示在最终对象上.如果您稍后决定公开更多属性,您可以简单地将它们添加到您的 DTO 并使用 @Expose 标记它们.

The resulting object is in the form you expect with only id and name showing up on the final object. If you decide later to expose more properties you can simply add them to your DTO and tag them with @Expose.

这种方法还允许您从使用 Object.assign

This approach also allows you to remove the constructor from your DTO that is using Object.assign

这篇关于将实体投射到 dto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆