Map paginated items to response dto?
egdelwonk opened this issue · comments
Will 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?
Will 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 },
},
});
}
Renaud LITTOLFF commented
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?
Ayman Kamal commented
@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
)
}