sequelize-cursor-pagination icon indicating copy to clipboard operation
sequelize-cursor-pagination copied to clipboard

Add option to disable count

Open timwangdev opened this issue 2 years ago • 1 comments

Counting all rows in a table with a complex query condition could be very slow. If we could disable totalCount output we would save the query execution time.

timwangdev avatar Jan 17 '23 11:01 timwangdev

I'm experimenting with this code snippet, it would have to query the count but still get the hasPreviousPage and hasNextPage info:

    const startCursor = edges.length > 0 ? edges[0].cursor : null;
    const endCursor = edges.length > 0 ? edges[edges.length - 1].cursor : null;

    const prevPaginationQuery = startCursor ? getPaginationQuery(reverseOrder(order), parseCursor(startCursor)) : null;
    const prevPaginationWhere = prevPaginationQuery ? { [Op.and]: [prevPaginationQuery, where] } : where;
    const prevInstance = startCursor && (await modelClass.findOne({ where: prevPaginationWhere, ...restQueryOptions }));

    const nextPaginationQuery = endCursor ? getPaginationQuery(order, parseCursor(endCursor)) : null;
    const nextPaginationWhere = nextPaginationQuery ? { [Op.and]: [nextPaginationQuery, where] } : where;
    const nextInstance = endCursor && (await modelClass.findOne({ where: nextPaginationWhere, ...restQueryOptions }));

    const pageInfo = {
      startCursor,
      endCursor,
      hasPreviousPage: !!prevInstance,
      hasNextPage: !!nextInstance,
    };

timwangdev avatar Jan 18 '23 02:01 timwangdev