schematics icon indicating copy to clipboard operation
schematics copied to clipboard

Using async method and `ParseIntPipe` for generated resources controllers and services[restful] .

Open notzheng opened this issue 1 year ago • 7 comments

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:

  1. use + to convert string to number
  2. 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.

notzheng avatar Jul 20 '22 10:07 notzheng

you're problably using an old version. @nestjs/cli@9 doesn't generate methods with await in it

image

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

micalevisk avatar Jul 20 '22 11:07 micalevisk

you're problably using an old version. @nestjs/cli@9 doesn't generate methods with await in it

image

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

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) .

notzheng avatar Jul 20 '22 11:07 notzheng

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)

micalevisk avatar Jul 20 '22 12:07 micalevisk

would you like to open a PR to add those async on them?

micalevisk avatar Aug 26 '22 02:08 micalevisk

would you like to open a PR to add those async on them?

sorry for late, I'd like to do~

notzheng avatar Aug 28 '22 19:08 notzheng

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)

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.

notzheng avatar Aug 31 '22 15:08 notzheng

got you

I'd leave those +id then

micalevisk avatar Aug 31 '22 15:08 micalevisk

Let's track this here https://github.com/nestjs/schematics/pull/1163

kamilmysliwiec avatar Dec 27 '22 10:12 kamilmysliwiec