nestjsx / nestjs-typeorm-paginate

:page_with_curl: Pagination response object function + types for typeorm + nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Map paginated items to response dto?

egdelwonk opened this issue · comments

commented

Is there a way to map the paginated items to a response dto ?

    public async findAll(
        options: IPaginationOptions,
        organizationId: string
    ): Promise<Pagination<User>> {
        const users = await paginate<User>(this.userRepository, options, {
            where: {
                organization: { id: organizationId },
            },
        });

        users.items = users.items.map(user => UserResponse.fromUser(user));
        return users;
    }

user.items is read-only; is there a canonical way of doing this?

commented

Nevermind, lol.

Changing the type worked.

    public async findAll(
        options: IPaginationOptions,
        organizationId: string
    ): Promise<Pagination<UserResponse>> {
        return await paginate<User>(this.userRepository, options, {
            where: {
                organization: { id: organizationId },
            },
        });
    }

Excuse me @egdelwonk but I don't understand what you corrected in your second post.
If I replace User with UserDTO as the return type in my code, it doesn't work and I have an error.
Can you explain what you did?

@marmotz I couldn't figure it out either so I ended up manually creating a Pagination object :(

  async paginate(options: IPaginationOptions, findOptions: any): Promise<Pagination<ResponsePaymentDTO>> {
    const paginationObject = await paginate<any>(this.paymentRepository, options, findOptions);
    return new Pagination<ResponsePaymentDTO>(
      paginationObject.items.map((payment) => mapper.map(payment, ResponsePaymentDTO, Payment)),
      paginationObject.meta
    )
  }