Kaltsoon / sequelize-cursor-pagination

➡️ Cursor-based pagination queries for Sequelize models
87 stars 27 forks source link

Add option to disable count #67

Open timwangdev opened 1 year ago

timwangdev commented 1 year ago

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 commented 1 year ago

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,
    };