sequelize-cursor-pagination
sequelize-cursor-pagination copied to clipboard
Add option to disable count
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.
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,
};