将实体投射到 dto

2024-05-11

只是想知道将 NestJS 实体对象转换为 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 并返回如下内容:

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

但我收到的回复仍然包含所有属性:

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

我基本上想删除 DTO 中未定义的所有属性。

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

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

@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});
}

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


因此,根据 Jesse 的精彩回答,我最终使用 @Exclude() 和 @Expose() 创建了 DTO,以删除除公开属性之外的所有属性:

import { IsString, IsEmail } from 'class-validator';
import { Exclude, Expose } from 'class-transformer';

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

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

然后我创建了一个通用转换拦截器,它调用 plainToclass 来转换对象:

import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { plainToClass } from 'class-transformer';

interface ClassType<T> {
    new(): T;
}

@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<Partial<T>, T> {

    constructor(private readonly classType: ClassType<T>) {}

    intercept(context: ExecutionContext, call$: Observable<Partial<T>>, ): Observable<T> {
        return call$.pipe(map(data => plainToClass(this.classType, data)));
    }
}

然后使用这个拦截器将数据转换为任意类型:

@Get('test')
@UseInterceptors(new TransformInterceptor(PhotoSnippetDto))
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'
  });
  return photo;
}

这给了我我想要的:

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

绝对感觉更像巢!我可以在任何需要的地方使用相同的拦截器,并且要更改响应,我只需要更改 DTO。

快乐的时光。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将实体投射到 dto 的相关文章

  • 如何在 PyV8 中加载 Nodejs 模块?

    如何在 PyV8 中加载 Nodejs 模块 我读过所有关于 jsdom 在与 Nodejs 一起运行时有多么出色的内容 如果我在 Python 应用程序中运行 v8 使用 python 获取 Web 资源 然后将生成的 html 字符串提
  • 将 html2pdf 生成的 pdf 发送回服务器

    我必须使用以下命令发送客户端生成的 PDFhtml2pdf到服务器 我已成功将生成的 PDF 转换为 base64 并希望使用axios 这是我的客户端代码 function myFunction var element document
  • Nodejs - 处理和发送多部分请求,

    我的应用程序在 Nodejs 服务器上运行 Node Js 还充当来自应用程序的请求的中间件 代理 因此 从浏览器开始 所有 REST 调用都会转到 NodeJs 然后转到 Java API 我发现处理多部分表单数据的请求时出现问题 我在
  • 如何将 zingchart 实现到 Angular2 中

    我有一个现有的项目 我想在其上实施 zingcharts 我尝试了 3 个不同的教程 主要来自 https blog zingchart com 2016 07 19 zingchart and angular 2 charts back
  • Child_process 处理带有回车符 (\r) 的 STDOUT 流

    我正在编写一个简单的应用程序 它允许工作中的内部系统请求从远程服务器到使用 REST 调用发起的另一个远程服务器的复制过程 使用 rsync 我已经对express框架足够熟悉 并且刚刚开始尝试child process库 并偶然发现了一个
  • 有人在node/socket.io 中成功实现了动态命名空间吗?

    含义 用户对应用程序进行身份验证 gt 应用程序设置socket io连接的命名空间 http www socketioserver com NAMESPACE 并且节点服务器相应地响应无需针对特定名称空间进行硬编码 到那个特定的命名空间
  • nodejs mocha suite 未定义错误

    我正在尝试使用摩卡运行一些测试 但似乎无法克服这个错误 E tdd nodejs cart gt mocha cart test js node js 201 throw e process nextTick error or err Re
  • express 或express-generator:我需要两者吗?

    只是探索 Node js 并遇到了 Express 在 npm 存储库站点上https www npmjs com package express https www npmjs com package express它明确指出安装是 np
  • socket.io 作为客户端

    有什么方法可以将socketio作为客户端运行 不是浏览器 而是nodejs脚本 我需要将数据从服务器广播到一些客户端 浏览器 和另一台linux机器 仅运行nodejs来获取变量 没有浏览器 欢迎任何想法 Regards github上有
  • npm install 命令下载所需包的源位置是什么?

    我试图获取命令 npm install 尝试连接的源位置 URL 并根据 package json 文件获取要下载的依赖包 并将其放置在本地框中 从下面提到的网址 http www tutorialspoint com nodejs nod
  • D3、TS 和 Angular 2

    我正在尝试将 D3 v4 与 Angular 2 Typescript 一起使用 我目前正在研究 D3 v4 我能够遵循 stackoverflow 中类似问题的一些答案 但没有成功 我已经导入了大部分 D3 库及其类型 我使用的是 TS
  • FullCalendar-vue + Typescript:属性“getApi”不存在

    我尝试将 FullCalendar vue 与 Typescript 结合使用 但在访问其 API 时遇到错误 我的日历设置如下
  • 如何使用 typeorm 在 postgres 中保存 json 对象数组

    我正在尝试在 postgres 中保存 jsonb 类型的对象数组 Entity Column type jsonb array true nullable true testJson object 我在邮递员中发送的json testJs
  • 想要动态处理与分页相关的页码显示:ReactJS

    我有一些分页逻辑工作得很好 唯一的问题是我只能让它显示并固定数量的页面可供选择 现在我已经把它放到了 5 页 但我希望它能够根据总记录动态更改 假设我有 100 条记录 每页限制为 10 条 将有 10 页 现在我只能让它以这种方式显示 第
  • 使用 AWS CDK 为 lambda 指定自定义角色

    我意识到它很新 但我没有看到任何语言的任何示例 说明如何为使用 AWS CDK 创建的 lambda 指定角色 我正在尝试这样做 const cdk require aws cdk cdk const lambda require aws
  • 如何在Sequelize中设置查询超时?

    我想看看如何在 Sequelize 中设置查询的超时时间 我查看了 Sequelize 文档以获取一些信息 但我找不到我要找的东西 我发现的最接近的是 pools acquire 选项 但我不想设置传入连接的超时 而是设置正在进行的查询的超
  • 通过扩展和实现的组合来理解 TS 的类型推断/缩小

    我有以下示例代码 class B implements Error name string message string stack undefined string function Foo x any if x instanceof E
  • 使用属性和泛型类型获取“keyof”中项目的类型

    我想定义一个具有泛型类型的接口 该接口必须接受一个对象 其键作为 根字段名称 值作为对象数组 该数组定义一些子字段 其键作为子字段的名称 type 作为字段值的类型 像这样的东西 interface Inputs emails email
  • Node.js 中的异步或步骤

    我无法让我的异步代码与 node js 一起使用 尝试异步和步骤库 代码仅返回第一个函数 似乎没有执行其余函数 我究竟做错了什么 thanks var step require step step function f1 console l
  • 如何处理 MongoDB 的断开连接错误

    我在 Node js 进程中看到了这个未捕获的异常 Uncaught exception Error read ETIMEDOUT at TCP onStreamRead internal stream base commons js 16

随机推荐