schematics
schematics copied to clipboard
Using async method and `ParseIntPipe` for generated resources controllers and services[restful] .
Is there an existing issue that is already proposing this?
- [X] I have searched the existing issues
Is your feature request related to a problem? Please describe it
For now, nest g mo cat [restful]
will generated controllers like this:
@Get(':id')
findOne(@Param('id') id: string) {
return this.catService.findOne(+id);
}
There is two problems I think:
- use
+
to convert string to number - the method is sync, but usually we will get resources from db which be async
Describe the solution you'd like
It would better to solve problem one using ParseIntPipe
and make methods async.
@Get(':id')
async findOne(@Param('id', ParseIntPipe) id: number) {
return await this.catService.findOne(id);
}
Teachability, documentation, adoption, migration strategy
No response
What is the motivation / use case for changing the behavior?
as related to a problem
part say.
you're problably using an old version. @nestjs/cli@9
doesn't generate methods with await
in it
https://github.com/nestjs/schematics/blob/6d3a1be4df72725bf16e5765406755b2f521ac82/src/lib/resource/files/ts/name.controller.ts#L25-L27
But yeah, using ParseIntPipe
is better
try npx nest
instead of just nest
you're problably using an old version.
@nestjs/cli@9
doesn't generate methods withawait
in it
https://github.com/nestjs/schematics/blob/6d3a1be4df72725bf16e5765406755b2f521ac82/src/lib/resource/files/ts/name.controller.ts#L25-L27
But yeah, using
ParseIntPipe
is bettertry
npx nest
instead of justnest
Sorry for misunderstanding caused by my typo. I have corrected it.
I mean it's better to generate async method in controller and service bucause usually we will get resources from db which be async (just my opinion) .
there's nothing sync here, findOne
returns a promise, thus it's async. I prefer not having return await
because is redundant (as there's no catch in there)
would you like to open a PR to add those async
on them?
would you like to open a PR to add those
async
on them?
sorry for late, I'd like to do~
there's nothing sync here,
findOne
returns a promise, thus it's async. I prefer not havingreturn await
because is redundant (as there's no catch in there)
Hi, I have open a PR for this issue, but for async
part only.
https://github.com/nestjs/schematics/pull/1163
I haven't use WebSocketGateway or graphQL Resolver yet, but I see their method is also async in the doc, so I add async
for them. If there is some problem, please let me know, and I will try to fix them.
And about the ParseIntPipe
part.
I have no idea if it should change them, because:
If user has enabled auto-transformation, the ValidationPipe
will perform conversion of primitive types, it should be
@Get(':id')
findOne(@Param('id') id: number) {
return this.catService.findOne(id);
}
But if user with auto-transformation
disabled, Explicit conversion will be better :
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
return await this.catService.findOne(id);
}
So is there some way to know if user has auto-transformation
enabled?
Could you give me some advice? and I'd like to open another PR.
got you
I'd leave those +id
then
Let's track this here https://github.com/nestjs/schematics/pull/1163