Skip to content
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

⭐ feat(add): Added support for customCountFn. Set useCustomCount… #112

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Returns promise
- `[customLabels]` {Object} - Developers can provide custom labels for manipulating the response data.
- `[pagination]` {Boolean} - If `pagination` is set to false, it will return all docs without adding limit condition. (Default: True)
- `[useEstimatedCount]` - Enable [estimatedDocumentCount](https://docs.mongodb.com/manual/reference/method/db.collection.estimatedDocumentCount/) for larger datasets. Does not count based on given query, so the count will match entire collection size. (Default: False)
- `[useCustomCountFn]` - Enable custom function for count datasets. (Default: False)
- `[forceCountFn]` {Boolean} - Set this to true, if you need to support \$geo queries. (Default: False)
- `[read]` {Object} - Determines the MongoDB nodes from which to read. Below are the available options.
- `[pref]`: One of the listed preference options or aliases.
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @param {Number} [options.page=1]
* @param {Number} [options.limit=10]
* @param {Boolean} [options.useEstimatedCount=true] - Enable estimatedDocumentCount for larger datasets. As the name says, the count may not abe accurate.
* @param {Function} [options.useCustomCountFn=false] - use custom function for count datasets.
* @param {Object} [options.read={}] - Determines the MongoDB nodes from which to read.
* @param {Function} [callback]
*
Expand Down Expand Up @@ -43,6 +44,7 @@ const defaultOptions = {
options: {},
pagination: true,
useEstimatedCount: false,
useCustomCountFn: false,
forceCountFn: false,
};

Expand All @@ -65,6 +67,7 @@ function paginate(query, options, callback) {
sort,
pagination,
useEstimatedCount,
useCustomCountFn,
forceCountFn,
} = options;

Expand Down Expand Up @@ -118,6 +121,8 @@ function paginate(query, options, callback) {
} else {
if (useEstimatedCount === true) {
countPromise = this.estimatedDocumentCount().exec();
} else if (typeof useCustomCountFn === 'function') {
countPromise = useCustomCountFn();
} else {
countPromise = this.countDocuments(query).exec();
}
Expand Down
36 changes: 36 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,42 @@ describe('mongoose-paginate', function () {
});
});

it('count Custom Fn works', function (done) {
Book.paginate(
{},
{
useCustomCountFn: function () {
return 100;
},
},
function (err, result) {
expect(err).to.be.null;
expect(result).to.be.an.instanceOf(Object);
assert.isNumber(result.totalDocs, 'totalDocs is a number');
expect(result.totalDocs).to.equal(100);
done();
}
);
});

it('count Custom Fn with Promise return works', function (done) {
Book.paginate(
{},
{
useCustomCountFn: function () {
return Promise.resolve(100);
},
},
function (err, result) {
expect(err).to.be.null;
expect(result).to.be.an.instanceOf(Object);
assert.isNumber(result.totalDocs, 'totalDocs is a number');
expect(result.totalDocs).to.equal(100);
done();
}
);
});

after(function (done) {
mongoose.connection.db.dropDatabase(done);
});
Expand Down