英文:
NestJS custom transform pipe with parameters and injected TypeOrm dataSource
问题
我正在尝试编写用于实体ID验证的自定义管道。想法是接受实体类作为参数,从路由获取ID,并验证是否存在具有该ID的实体。
所以我有一个端点:
@Post('track/:id')
addTrack(
@Param('id', ParseUUIDPipe, new EntityIdValidationPipe(Track)) id: string,
) {
return this.favsService.addTrack(id);
}
这是自定义管道:
@Injectable()
export class EntityIdValidationPipe implements PipeTransform {
constructor(private entityClass: EntityClassOrSchema) {}
async transform(id: string, metadata: ArgumentMetadata): Promise<string> {
// 这里我需要访问TypeOrm的DataSource,
// 但我不知道如何注入它
return id;
}
}
正如你上面看到的,我在验证管道中使用了new
关键字来传递参数。在transform
方法中,我想要做类似这样的事情:
this.dataSource.getRepository(this.entityClass).findOneBy({ id })
但我不能这样做,因为我不知道如何注入TypeOrm的DataSource对象。
所以基本上我的问题是:我如何同时传递参数给自定义的transform管道并在管道中使用DI(依赖注入)?
谢谢。
英文:
I'm trying to write custom pipe for entity id validation. The idea is to accept entity class as parameter, get id from route and validate whether entity with such id exists.
So i have an endpoint:
@Post('track/:id')
addTrack(
@Param('id', ParseUUIDPipe, new EntityIdValidationPipe(Track)) id: string,
) {
return this.favsService.addTrack(id);
}
and here is custom pipe:
@Injectable()
export class EntityIdValidationPipe implements PipeTransform {
constructor(private entityClass: EntityClassOrSchema) {}
async transform(id: string, metadata: ArgumentMetadata): Promise<string> {
// here i need to access TypeOrm's DataSource,
// but i dont know how to inject it
return id;
}
}
As you can see above i'm using validation pipe with new
keyword to pass an argument.
In transform
method i want to do something like this:
this.dataSource.getRepository(this.entityClass).findOneBy({ id })
But i cant do it because i dont know how to inject TypeOrm DataSource object.
So basically my question is: How can i pass parameters to custom transform pipe and use DI in that pipe simultaneously?
Thank you.
答案1
得分: 1
听起来你实际想要做的是创建一个mixin
管道,这是一个返回类引用的函数,以便Nest仍然可以注入数据源。它看起来像这样:
export const EntityIdValidationPipe = (entityClass: EntityClassOrSchema) => {
@Injectable()
class EntityIdValidationMixinPipe {
constructor(@InjectDataSourcec() private readonly dataSource: DataSource) {}
async transform(id: string, metadata: ArgumentMetadata): Promise<string> {
const entity = await this.dataSource.getRepository(entityClass).findOneBy({ id })
// validate entity
return id;
}
return EntityIdValidationMixinPipe;
}
对于其他参考,请查看Nest的AuthGuard()
和FileInterceptor
。
英文:
Sounds like what you are actually wanting to do is create a mixin
pipe, a function that will return a class reference so that Nest can still inject the datasource. It would look something like this:
export const EntityIdValidationPipe = (entityClass: EntityClassOrSchema) => {
@Injectable()
class EntityIdValidationMixinPipe {
constructor(@InjectDataSourcec() private readonly dataSource: DataSource) {}
async transform(id: string, metadata: ArgumentMetadata): Promise<string> {
const entity = await this.dataSource.getRepository(entityClass).findOneBy({ id })
// validate entity
return id;
}
return EntityIdValidationMixinPipe;
}
For other references, check out Nest's AuthGuard()
and FileInterceptor
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论