【发布时间】:2019-01-14 23:21:08
【问题描述】:
我需要在intercept 函数内访问一个服务(由Nest TypeOrmModule 提供)(重要提示:不是constructor 参数!!!)因为这取决于传递的选项(在这种情况下为entity)。
服务注入令牌由getRepositoryToken 函数提供。
export class PaginationInterceptor {
constructor(private readonly entity: Function) {}
intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
// Here I want to inject the TypeORM repository.
// I know how to get the injection token, but not HOW to
// get the "injector" itself.
const repository = getRepositoryToken(this.entity);
// stuff...
return call$;
}
}
Nest 中有“服务容器”的概念吗?这个github issue 对我没有帮助。
示例用法(控制器操作):
@Get()
@UseInterceptors(new PaginationInterceptor(Customer))
async getAll() {
// stuff...
}
【问题讨论】:
标签: node.js typescript nestjs typeorm