Closed SebTota closed 2 years ago
Ended up figuring this out. Casting the iterator to a list works in getting the results, but it doesn't forward the cursor for some reason so you have to do something like l_motorcycles = [m for m in motorcycles]
. This is my implementation in the end:
@router.get('/motorcycles', response_model=MotorcycleListResponse)
def get_motorcycles(limit: int = 9, show_sold: bool = False, pagination_cursor: str = None):
motorcycles_controller = MotorcycleController.collection
if pagination_cursor:
motorcycles_controller = motorcycles_controller.cursor(pagination_cursor)
elif not show_sold:
motorcycles_controller = motorcycles_controller.filter(sold=False)
motorcycles_controller = motorcycles_controller.fetch(limit)
motorcycles = [m.to_dict() for m in motorcycles_controller]
cursor = motorcycles_controller.cursor
# I HATE the fact that firestore has no good way to know if there are any results left in your pagination results or a simple count(*) so this is a workaround for that. Granted, it's a costly workaround because all page loads will result in another unnecessary query.
if len(motorcycles) < limit or len(list(MotorcycleController.collection.cursor(cursor).fetch(1))) == 0:
cursor = None
return MotorcycleListResponse(num_items=len(motorcycles),
items=motorcycles,
pagination_cursor=cursor)
Hello, I am using pagination to load a list of items and I noticed that the cursor only works on the first time it is used (the second query). After that it just keeps returning the same item. Ex:
The first call,
/items
, to this API route returnsitem_1
withpagination_cursor='abc123'
The second call,/items?pagination_cursor=abc123
, returnsitem_2
withpagination_cursor='abc123'
(notice the same cursor hash) The third call,/items?pagination_cursor=abc123
, returns the sameitem_2
with the same cursor id. What is expected is that the db query returnsitem_3
with (I'm assuming) a new cursor hash.I'm not really sure what I could be doing wrong here and I would prefer not to have to use the
start_at
methods because I don't have a specific key I would want to sort the results on. Is what i am trying to accomplish possible with FireO?