I have a flask-sqlalchemy application that stores and retrieves records. I use Marshmallow SQLAlchemyAutoSchema to store and retrieve results. I have pagination working pretty well with
class SequenceSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Sequence
sequence_schema = SequenceSchema()
sequences_schema = SequenceSchema(many=True)
def get_sequence(id):
qry = db.session.query(Sequences)
qry = qry.filter(Sequences.Id == id)
res = qry.paginate(
page=page, per_page=current_app.config['QUERY_RESULTS_PAGE_SIZE']
)
return res
I then use the paginated result (res) to get the jsonified results, total number of results, next page, prev. page etc.
However the front-end I need to interface was built with the notion of cursors and cursorIds (it interfaces with Mongo). So the next page Uri is the cursorId of the query so that when the user hits next page all I'm supposed to get is the cursorId and fetch the next page of results.
I modified my query to use ChunkedIteratorResult
res2 = db.session.execute(qry).yield_per(100)
# print first 100 results
print(sequences_schema.dump(res2.scalars().fetchmany())
# print next 100 results
print(sequences_schema.dump(res2.scalars().fetchmany())
So this works for printing while I'm still in the function. But how do I send the cursor as the next page uri so that I can re-access this query and pick up at the next page? Is that even possible?
In my original pagination implementation I set the next page uri so that the page number is sent back to me and I can continue from that as an offset. I'd argue that it's just as effective as using a damn cursor but well ... management.
Any ideas on what I can do?