nestjs-paginate icon indicating copy to clipboard operation
nestjs-paginate copied to clipboard

Serialize the data array

Open ppetzold opened this issue 3 years ago • 2 comments

Original: https://github.com/ppetzold/nestjs-paginate/issues/72#issuecomment-908314018

With serialization, I mean filtering and transforming the response data. Using class-transformer, for example.

For now, in my code, I have implemented an interceptor (also could be a class or helper function) to catch the Paginated and call the plainToClass() for data param. Here I can provide a small example for the better understanding:

// controller

const paginatedResults = await paginate(...) // Item[]
return {
    ...paginatedResults,
    data: plainToClass(ItemResponseDto, paginatedResults.data)
}
// ItemResponseDto with class-transformer

@Exclude()
export class ItemResponseDto {
  @Expose()
  id: string;
...
}

Having written this, I think this logic should be the responsibility of this module. So, nevermind 😅

ppetzold avatar Sep 09 '21 10:09 ppetzold

I was able to workaround that using the following code image

ricbermo avatar Dec 10 '21 18:12 ricbermo

Yup. That would be an example for the query builder solution I was referring to in #114

This issue here tho refers more to the incorporation of all feature of class-transformer. Besides hiding/exposing properties, it has decorators for additional data transformation etc.

ppetzold avatar Dec 11 '21 09:12 ppetzold