Limit
This section discusses how limit
and limitToLast
type works.
Block invalid Number Type
FirelordJS block decimal, 0 and negative number, however FirelordJS does not block number
type.
This is because number
type is a common type, and if your value come from an expression, then it is unavoidable a number
type.
Exceptions
FirelordJS does not check the runtime value of the input.
Because invalid number like negative number and decimal number is most likely result from our logic bug, and exception need to be thrown to remind us something is wrong with your code.
The Zero 🦤
Firestore throws on 0, like how it throws on other invalid number, this is not a very good design decision.
While negative number and decimal number is a result of buggy code, 0 however is not.
This is because 0 can be resulted from a correct calculation, getting 0 amount of items is not something that is absurd.
Which mean eventhough assigning 0 directly is pointless, but assigning 0 as a result of computation is meaningful.
However Firestore throw on 0, we need another strategy to deal with it.(Currently FirelodJS does not handle computed 0 value.)
Can we replace the 0 with 1? The answer is no and this is never an option.
This is because by changing 0 to 1, it may affects calcultion and resulting in hard to catch bug.
Another solution that we can try, we can return empty array upon 0.
But this may create other problem, example the type QuerySnapshot
comes with metadatas like readTime
(in admin only) and we need to provide correct value for metadatas and we need to do this without impact the API significantly.
Details like this is what the library will works on in the future.
LimitToLast
limitToLast
type works like limit
type, but with one extra catch: at least one orderBy
clause is needed(undocumented exception).
Multiple Clause
Only the last one is effective, this is also how the official sdk work.
query(colRef, limit(1), limit(2)) // reduce to query(colRef, limitToLast(2))
query(colRef, limitToLast(1), limitToLast(2)) // reduce to query(colRef, limitToLast(2))
query(colRef, limit(1), limitToLast(2)) // reduce to query(colRef, limitToLast(2))