Update useCustomCountFn parameter type in documentation and implement… #234
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🚀 Improve Pagination Count Logic & Enhance Custom Count Function
Description:
This PR introduces two improvements to the pagination logic:
Use estimatedDocumentCount() When Query Is Empty
Pagination now uses estimatedDocumentCount() when the query object is empty, and falls back to countDocuments(query) when filters are present.
✨ Pass Query to useCustomCountFn
The useCustomCountFn option now receives the current query as an (optional) parameter, allowing for more flexible custom count logic.
Reasoning & Benefits:
⚡️ Performance:
estimatedDocumentCount() is much faster for unfiltered queries, as it uses collection metadata instead of scanning documents.
🔍 Accuracy:
For filtered queries, countDocuments(query) ensures the count matches the filter criteria.
🛠️ Extensibility:
Passing the query to useCustomCountFn enables advanced custom count scenarios that depend on the current query context.
📚 Best Practices:
Aligns with MongoDB recommendations for counting documents and improves documentation/type annotations.
🛡️ Backward Compatibility:
Existing behavior is preserved for filtered queries and for custom count functions that do not use the new parameter.
Summary of Changes:
🚄 Use estimatedDocumentCount() for empty queries to improve performance.
📝 Pass query as an (optional) parameter to useCustomCountFn.
📚 Update JSDoc/type annotations to reflect the new behavior.
No breaking changes:
If your custom count function does not use the new parameter, it will continue to work as before. 👍