在插入NestJS之前,有没有一种方法可以转换DTO中的字段?

时间:2020-01-19 01:10:58

标签: node.js nestjs

我们目前正在使用NestJS及其出色的CRUD package以及通过TypeORM在Postgres上使用PostGIS进行一些空间操作。我们希望用户使用WKT上传空间字段(即Polygon),因为它更容易填写简单的文本字段,并且我们想知道DTO是否有一种方法可以执行验证以及从WKT到GeoJSON的转换(这是TypeORM可以毫无问题地在数据库中执行插入的方式),而不必重写整个createOneBase操作并允许在{ {1}}操作。有很多用于从WKT到GeoJSON转换和验证操作的程序包,但是我不确定 NestJS 将这一切塞在一起的方式。

是否有一种方法可以直接在DTO内部实现字段的WKT验证并将其转换为GeoJSON ?或者我应该同时覆盖createManyBaseCreateOneBase操作,以在服务层中执行转换和验证?除了尝试验证和转换同一领域之外,也许还有更好的方法吗?

谢谢!

实体:

CreateManyBase

DTO:

@Entity('suburb')
export class SuburbEntity {
  // ...

  @Column({ nullable: true, type: 'geometry' })
  public polygon: Geometry;

// ...
}

控制器:

export class CreateSuburbDTO {
  // ... 

  @IsOptional()
  @CustomWKTValidator()
  @TransformIntoGeoJSONMagically()
  public readonly polygon: any; // Using type "any" since it's originally a "string" and it has to become a GeoJSON object.

  // ...
}

服务

@Crud({
  model: {
    type: SuburbEntity,
  },
  params: {
    id: {
      field: 'id',
      type: 'uuid',
      primary: true,
    },
  },
  query: {
    maxLimit: 150,
  },
  dto: {
    create: CreateSuburbDTO,
  }
})
@Controller('suburbs')
export class SuburbController implements CrudController<SuburbEntity> {
  constructor(public service: SuburbService) {}
}

0 个答案:

没有答案