deta / deta-python

deta's official sdk
https://deta.space/docs/en/build/reference/sdk/base
MIT License
153 stars 25 forks source link

Deta Base "last" can be used to filter without having the full key #34

Closed naxty closed 2 years ago

naxty commented 3 years ago

Hi,

I'm currently implementing some pagination feature. In the code I found the "last" parameter in the _fetch method.

https://github.com/deta/deta-python/blob/bcf296d581a3303214332ee8ad3eb1253a085124/deta/base.py#L138

While implementing I have played around with the "last" parameter and I'm not sure if I discovered a bug or a feature. For me, it sounded like I have to provide the full provide key in order to jump to the "last" reference. But it's possible to provide just a value and then last will be used to retrieve the next values after the provided value. Check the example below where I pass "12" and retrieve other values.

test_area = deta.Base("test_area")
test_area.put({"key": "1", "value": "1"})
test_area.put({"key": "11", "value": "1"})
test_area.put({"key": "2", "value": "1"})
test_area.put({"key": "21", "value": "1"})
test_area.put({"key": "33", "value": "1"})

resp = test_area._fetch(last="12")
# (200, {'paging': {'size': 3}, 'items': [{'key': '2', 'value': '1'}, {'key': '21', 'value': '1'}, {'key': '33', 'value': '1'}]})

As a developer I would expect that last is actually the real key and that it throws an exception if the value doesn't exists or at least have some hint in the documentation. But maybe I'm wrong. Thanks for the amazing project!

aavshr commented 3 years ago

hello @naxty sorry for the late response.

As you observed, the base http api retrieves items with keys that come after the last key (keys are sorted lexicographically) regardless if the last key does not exist.

It behaves like this SQL query

SELECT * FROM items WHERE key>'lastKey';