Skip to content

Update useCustomCountFn parameter type in documentation and implement… #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025

Conversation

AlexSmirnov9107
Copy link

@AlexSmirnov9107 AlexSmirnov9107 commented Jun 7, 2025

🚀 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. 👍

@aravindnc aravindnc merged commit 42aeb9b into aravindnc:main Jun 10, 2025
3 checks passed
@aravindnc
Copy link
Owner

@AlexSmirnov9107 Thanks. Can you please add a test case for this too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants