Add pagination with cursor
New Feature / Enhancement Checklist
- [x] I am not disclosing a vulnerability.
- [x] I am not just asking a question.
- [x] I have searched through existing issues.
Current Limitation
Query results pagination can currently only be achieved by using limit and skip. A MongoDB query however also returns a cursor that can be used to paginate through query results.
Feature / Enhancement Description
- Add the query result cursor to response of a query as meta data.
- Add a methods to interact with the cursor.
For example:
let q = new Parse.Query();
q.equalTo(...);
const response = await q.find();
const page1 = response.results;
const cursor = response.cursor;
q = Parse.Query.fromCursor(cursor);
q.skip(100);
q.limit(100);
const page2 = await q.find();
Or alternatively introduce an entirely new command set for cursor operations, for example:
c = Parse.QueryCursor(cursor);
c.moveToPosition(100);
const page2 = await c.return(100);
Returning meta data together with the response is related to a pending change of the response object for a sustainable modification of the response JSON structure, see https://github.com/parse-community/parse-server/pull/7440#discussion_r678916891. Returning a cursor together with results is one use case that justifies the change of the query response from a ParseObject array to this more versatile form:
{
results: [...],
meta: {
cursor: ...,
explain: ...
}
}
Example Use Case
See the current challenge of pagination in Parse Dashboard in https://github.com/parse-community/parse-dashboard/issues/1551.
Alternatives / Workarounds
Paginate using limit and skip with the major downside of each pagination request issuing a new query instead of using the existing cached query results on the database side. The other challenge is that each new query is done over a potentially changing data set, so the limit / skip parameters become inaccurate.
Thanks for opening this issue!
- 🎉 We are excited about your ideas for improvement!
Cursor for postgres: https://www.postgresql.org/docs/current/plpgsql-cursors.html