我有 BaseController (摘要) 包含管道 CanGetPipe
export abstract class CanGetPipe implements PipeTransform {
protected abstract readonly repository: Repository<IEntry>;
...
并混合它
export const makeCanGetPipe = (repoName: any) =>
mixin(
class extends CanGetPipe {
protected readonly repository = getConnection().getRepository<IEntity>(repoName);
},
);
BaseControlle 的 Get 方法如下所示:
@Get('/:id')
@UsePipes(makeCanGetPipe(REPOSITORY_NAME_FROM_CHILD_CONTROLLER))
getOne(@ValidateData() validate: ValidateParam, @Param('id') id: number) {
return this.Service.get(id, validate);
}
我如何在子 Controller 中设置REPOSITORY_NAME_FROM_CHILD_CONTROLLER
而不是从BaseController继承?
或者,我如何访问分配给 BaseController 的管道中子 Controller 的存储库(服务、依赖项)?
最佳答案
我认为你可以使用以下组合:
-
@SetMetadata('repoName', YOUR_VALUE)
在您 child 的类(class) - 代替管道 - 使用 Guard
const repoName = this.reflector.get<string>('repoName', context.getClass());
这应该可以解决问题,您将能够为您的 BaseGuard 获得自定义存储库:)
关于typescript - NestJS如何在管道中访问抽象 Controller 中的存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67125715/