我正在使用构建 APINest.js和MySQL。由于敏捷性和 DRY 原则,我正在创建一个 OOP 结构,它为给定实体(来自 TypeORM)设置所有基本 CRUD 端点。主要目标是避免为不同的实体编写相同的通用方法。
为了实现这一目标,我使用了一种策略TypeScript 泛型。我仍然需要创建所有常用文件(.controller.ts
, .service.ts
, .module.ts
, .entity.ts
)对于每个实体,但我不必编写它的方法。相反,我只是扩展了两个类:RestController
and RestService
。这些类已经实现了常用方法,但我必须传递一些T types作为参数,以便 TypeORM 可以将正确的存储库注入到服务中。
问题: The @UsePipes
当我在父类中使用装饰器时,它没有被调用(RestController
),但是当我覆盖 de 时它可以正常工作休息控制器在子类中创建方法(SubcategoriesController
).
休息控制器.ts:
import { Get, Post, Body, Param, Put, Delete, UsePipes, ValidationPipe } from '@nestjs/common';
import { RestService } from './rest.service';
import { ObjectLiteral } from 'typeorm';
export abstract class RestController<T, C = T, U = T> {
constructor(protected service: RestService<T, C, U>) {}
@Get()
async index(): Promise<T[]> {
return this.service.getAll();
}
@Post('create')
@UsePipes(ValidationPipe) //HERE!
async create(@Body() data: C): Promise<T> {
return this.service.create(data as C);
}
}
休息.服务.ts:
import { Repository, UpdateResult, DeleteResult, Entity, DeepPartial } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
export interface RestClass<T, C = T, U = T> {
// Properties
repository: Repository<T>;
// Default Methods
getAll(): Promise<T[]>;
create(model: T | C | U): Promise<T>;
}
export class RestService<T, C = T, U = T> implements RestClass<T, C, U> {
constructor(
public repository: Repository<T>,
) {}
getAll = async () => {
return await this.repository.find({relations:: this.repository.metadata.ownRelations.map(r => r.propertyName)});
}
create = async (model: C) => {
return await this.repository.save(model as C);
}
}
以下是我如何设置真实的实体端点,扩展上述类:
子类别.controller.ts:
import { Controller, Get, Post, UsePipes, ValidationPipe, Body } from '@nestjs/common';
import { SubcategoriesService } from './subcategories.service';
import { Subcategory } from './subcategory.entity';
import { RestController } from '../rest.controller';
import { CreateSubcategoryDTO } from './dto/createSubcategory.dto';
//NOTE THE TYPE PARAMS IN <>
@Controller('subcategories')
export class SubcategoriesController extends RestController<Subcategory, CreateSubcategoryDTO> {
constructor(public service: SubcategoriesService) {
super(service);
}
}
子类别.service.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Subcategory } from './subcategory.entity';
import { Repository } from 'typeorm';
import { RestService } from '../rest.service';
import { CreateSubcategoryDTO } from './dto/createSubcategory.dto';
//NOTE THE TYPE PARAMS IN <>
@Injectable()
export class SubcategoriesService extends RestService<Subcategory, CreateSubcategoryDTO> {
constructor(
@InjectRepository(Subcategory) repository: Repository<Subcategory>,
) {
super(repository);
}
}
创建子类别.dto.ts
import { IsString, Length, IsInt } from 'class-validator';
export class CreateSubcategoryDTO {
@IsString()
@Length(5, 60)
name: string;
@IsString()
@Length(0, 140)
summary: string;
@Length(0, 140)
icon: string;
@IsInt()
category: number;
}
可以看到父类接受3种类型参数:
-
T
:实体
-
C
:CreateDTO,可选
-
U
:UpdateDTO,可选
然而,上面的代码完美地创建了端点它不验证有效负载 in the /create
,正如预期的那样验证管道.
如果我覆盖 the createSubcategoriesController 中的方法,并在其中添加 UsePipes,it works!
我认为这可能是指 Nests 生命周期的错误,它可能不支持在抽象类中使用 Pipes。
有人有主意吗?
P.S。没有转译错误、lint 警告或运行时异常。