【问题标题】:Nestjs: Cast complex Entity to DtoNestjs:将复杂实体转换为 Dto
【发布时间】:2021-12-15 04:06:34
【问题描述】:

我正在尝试将复杂实体转换为 Dto。我想从 DTO 中删除实体中的一些值。

我的实体看起来像这样(为简单起见,省略了大部分实体):

export class ResultEntity {
    constructor(
        public id: string,
        public userId: string,
        public results: CalculationOutputEntity[] = [],
        public item: Item = new Item(),
    ) { }
}

export class Item {
    constructor(
        public privat: Private = new Private(),
        public state: State = new State(),
        public combined: number = 0,
    ) { }
}

export class State {
    constructor(
        public numberA: number = 0,
        public numberB: number = 0,
        public numberC: number = 0,
        public numberD: number = 0,
    ) { }
}

我找到了一个非常适合简单实体的解决方案:Cast entity to dto 如果我采用自定义拦截器的这个建议,删除 id 或整个 item 属性就可以了。

我尝试了两种可能性来定义我的 DTO:

  1. 它只返回 id 和整个项目。
@Exclude()
export class AdvisoryResultDto {
    @Expose()
    public id: string;
    public userId: string;
    public results: CalculationOutputDto[];
    @Expose()
    public item: Item;
}
  1. 此解决方案返回除 item 属性之外的所有内容。
export class AdvisoryResultDto {
    public id: string;
    public userId: string;
    public results: CalculationOutputDto[];
    @Exclude()
    public item: Item;
}

现在的问题是我只想删除 item 属性中的某些值。因此,例如私有字段和状态属性编号B。

我希望在嵌套类中也可以使用 Exclude() 删除单个值。

例如:

export class AdvisoryResultDto {
    public id: string;
    public userId: string;
    public results: CalculationOutputDto[];
    public item: Item;
}

export class Item {
    @Exclude()
    public privat: PrivateDto;
    public state: StateDto;
    public combined: number;
}

export class StateDto {
    public numberA: number;
    @Exclude()
    public numberB: number;
    public numberC: number;
    public numberD: number;
}

不幸的是,我找不到关于这个确切问题的任何信息。手动接管映射也不是一个解决方案,因为CalculationOutput 要复杂得多,并且嵌套了许多类和属性。

我只是错过了一小步,还是无法使用 class-transformer 中的 ExposeExclude?如果不是,我将不胜感激另一个解决方案。

【问题讨论】:

    标签: mapping entity nestjs interceptor dto


    【解决方案1】:

    为什么不创建一个构造函数,然后只使用实体中所需的值来初始化 DTO?只需将实体对象传递给 DTO 的构造函数并从 DTO 中删除字段。

    【讨论】:

      【解决方案2】:

      所以我在 class-transformer 包的文档中找到了解决方案:https://github.com/typestack/class-transformer#working-with-nested-objects

      我需要将@Type-Decorator 添加到嵌套类中

      解决办法:

      @Expose()
      export class AdvisoryResultDto {
          public id: string;
          public userId: string;
          public results: CalculationOutputDto[];
          @Type(() => Item)
          public item: Item;
      }
      
      export class Item {
          @Exclude()
          @Type(() => PrivateDto)
          public privat: PrivateDto;
          @Expose()
          @Type(() => StateDto)
          public state: StateDto;
          public combined: number;
      }
      
      export class StateDto {
          public numberA: number;
          @Exclude()
          public numberB: number;
          public numberC: number;
          public numberD: number;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-05
        • 1970-01-01
        • 2019-04-22
        • 2020-07-23
        • 2016-06-10
        • 1970-01-01
        • 2019-05-01
        • 2018-09-14
        相关资源
        最近更新 更多