pietrzakadrian / pietrzakadrian.com-blog-comments

0 stars 0 forks source link

How to create pagination in Nest.js with TypeORM + Swagger - Software Engineering Blog | Adrian Pietrzak #4

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

How to create pagination in Nest.js with TypeORM + Swagger - Software Engineering Blog | Adrian Pietrzak

Adrian Pietrzak is a software engineer from Poland. He mostly works with Node.js, React and React Native.

https://pietrzakadrian.com/blog/how-to-create-pagination-in-nestjs-with-typeorm-swagger

NourMaherT commented 2 years ago

that's what I call clean code!!

waflessnet commented 2 years ago

beautiful code, thank you very much!

kotoyama commented 2 years ago

Seems like it doesn't work with the newest version of nestjs/swagger. I got an error: TypeError: Cannot destructure property 'prototype' of 'type' as it is undefined.

I also found a question with the same problem. Any ideas?

mrsolarius commented 2 years ago

Extremely cleaver! Before see your post, I never think about abstract DTO because I thought it was difficult to generate open API documentation. But create a custom decorator to generate it is an excellent idea!

jaeboklee-modo commented 2 years ago

Wonderful! Thanks.

EdwardMartin123 commented 1 year ago

I had an issue with the swagger docs not recognising UserDto as a model so "data" was empty in the docs example response.

If anyone has the same issue, just add "model" to the ApiExtraModels in the decorator, i.e.

...
 return applyDecorators(
    ApiExtraModels(PageDto, model),
    ApiOkResponse({
      description: "Successfully received model list",
...
k1ngbanana commented 11 months ago

amazing clean code. thanks for sharing.

TraLeeee commented 6 months ago

When i console.log(pageOptionsDto.skip) it just return undefined. Idk why this happening

.skip(pageOptionsDto.skip)
Jarzebowsky commented 6 months ago

@TraLeeee make sure you have entry in your bootstrap file as below. app.useGlobalPipes(new ValidationPipe({ transform: true }));

For full overview check the tutorial again to find a proper place to do that.

TraLeeee commented 6 months ago

@Jarzebowsky I figured out that I need to expose that skip method using @Expose() decorator. And thanks for your reply, at the moment I got this error. My main.ts already add app.useGlobalPipes(new ValidationPipe({ transform: true }));.

Jarzebowsky commented 6 months ago

@TraLeeee if you need any assistance regarding an issue, please just share a repo with me (if possible) so I can help out.

georgi1980 commented 5 months ago

Great one! Thank you!

beqacrc commented 4 months ago
import { applyDecorators, Type } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
import { PaginationAble } from '../dto';

export const ApiPaginatedResponse = <TModel extends Type<any>>(
  model: TModel,
) => {
  return applyDecorators(
    ApiExtraModels(PaginationAble, model),
    //                             ^^^^^
    ApiOkResponse({
      description: 'Successfully received model list',
      schema: {
        allOf: [
          { $ref: getSchemaPath(PaginationAble<TModel>) },
          {
            properties: {
              data: {
                type: 'array',
                items: { $ref: getSchemaPath(model) },
              },
            },
          },
        ],
      },
    }),
  );
};
bohdan-sharkz commented 1 month ago

@pietrzakadrian that's a great tutorial, thanks a lot for your work.

suggestion: in e.g. nest.js docs, when tutorial is made by building some feature, it really helps when file names / files structure are given on top of code snippets. It really helps to understand the structure and makes it much easier to follow the tutorial.

Thanks again, very useful post :+1: