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

Is it possible to paginate an array of objects instead of a repository?

qafoori opened this issue · comments

Im using Fuse.Js (check it out here). I have a condition to check if there is a search query to search between the results.

So if there will be no query, I can easily use nestjs-typeorm-paginate. but as you can see my code, I don't know how it would be possible to use nestjs-typeorm-paginate inside a Fuse.Js result.

post.repository.ts:

async getAllPosts(filterPostDto: FilterPostDto): Promise<Pagination<Post> | any> {
    const { query, limit, page } = filterPostDto;
    const pageValue = page ? +page : 1;
    const limitValue = limit ? +limit : 10;
    
    const paginateOptions: IPaginationOptions = {
      limit: limitValue > 50 ? 50 : limitValue,
      page: pageValue,
      route: '/post'
    }

    if (!query) {
      // here i can easily use "nestjs-typeorm-paginate" 
      return paginate<Post>(this, paginateOptions);
    }

    const list = await this.find();
    const fuse = new Fuse(list, {
      keys: ['title'],
      minMatchCharLength: 1,
    });
    const searchResult = FuseResultToPureResult(fuse.search(query), list); 

    // here is my question! how can i pass "searchResult" constant to "nestjs-typeorm-paginate" in order to limit theme?
    // the "searchResult" would be something like this:
    /*[
        {
          title: 'some title',
          id: 1
        },
        {
          title: 'some another title',
          id: 2
        },
        .
        .
        .
      ]  */
  }

Techincally yes? I mean, the package is pretty tied to typeorm and I got pretty confused from your title :p but yes you can create a pagination response from just an array of objects, there are 2 methods. The first returning your own Pagination object

import { Pagination } from 'nestjs-typeorm-paginate'

return new Pagination<Post>(searchResults, { totalItems: searchResults.length })

Or the createPagination method

import { createPaginationObject } from 'nestjs-typeorm-pagination'

return createPaginationObject(searchResults, { totalItems: searchResults.length })

However, of you're expecting these methods to paginate the array itself, it's not possible to do that. You will have to include your pagination parameters within the fuse query itself but I'm guessing fuse is using elastic search?

@bashleigh , I don't know fuse uses elastic search or not. I tried your both suggested methods. they did not work. finally I wrote my own pagination function which follows IPaginationOptions and Pagination in nestjs-typeorm-paginate:

export const PaginateResultsFromFuseResults = (results: any[], options: IPaginationOptions): Pagination<any> => {
  const { limit, page, route } = options;
  const P_times_L = +page * +limit;
  const sliced = results.slice(P_times_L - +limit, P_times_L);
  const totalPages = totalCounter(results.length, +limit);
  const previous = +page - 1 <= 0 ? '' : `${route}?page=${+page - 1}&limit=${+limit}`;
  const next = +page + 1 >= totalPages ? '' : `${route}?page=${+page + 1}&limit=${+limit}`;

  return {
    items: sliced,
    meta: {
      currentPage: +page,
      totalPages,
      itemCount: sliced.length,
      itemsPerPage: +limit,
      totalItems: results.length
    },
    links: {
      first: `${route}?limit=${+limit}`,
      previous,
      next,
      last: `${route}?page=${totalPages}&limit=${+limit}`
    }
  }

}

I know this is not the best way. but so far I have not found another way. can anyone suggest a better way?

Can't see why you didn't get it work that way but least you've got it working now!