mongo-cursor-pagination
Advanced tools
Comparing version
{ | ||
"name": "mongo-cursor-pagination", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Make it easy to return cursor-paginated results from a Mongo collection", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -34,3 +34,4 @@ # mongo-cursor-pagination | ||
-query {Object} The mongo query to pass to Mongo. | ||
-limit {Number} The page size. Default: 100 | ||
-limit {Number} The page size. Must be between 1 and 100 (though can be overridden by | ||
setting MAX_LIMIT). | ||
-fields {Object} Fields to query in the Mongo object format, e.g. {_id: 1, timestamp :1}. | ||
@@ -127,2 +128,4 @@ The default is to query all fields. | ||
The prevent a user from querying too many documents at once, you can set property `MAX_LIMIT` on the library (e.g. `MongoPaging.MAX_LIMIT = 50;`). | ||
## Limitiations | ||
@@ -138,2 +141,4 @@ | ||
* 1.1.0 Add `lib.MAX_LIMIT` global setting to clamp | ||
* 1.0.0 Initial release | ||
@@ -140,0 +145,0 @@ |
@@ -371,2 +371,40 @@ var MongoClient = require('mongodb').MongoClient; | ||
}); | ||
describe('limits', () => { | ||
beforeEach(() => { | ||
sync.await(db.collection('test_paging_limits').insertMany([{ | ||
counter: 6 | ||
}, { | ||
counter: 5 | ||
}, { | ||
counter: 4 | ||
}, { | ||
counter: 3 | ||
}, { | ||
counter: 2 | ||
}, { | ||
counter: 1 | ||
}], sync.defer())); | ||
}); | ||
it('should clamp lower limit', () => { | ||
var res = sync.await(paging.find(db.collection('test_paging_limits'), { | ||
limit: -1 | ||
}, sync.defer())); | ||
expect(res.results.length).toBe(1); | ||
}); | ||
it('should clamp upper limit', () => { | ||
var originalMaxLimit = paging.MAX_LIMIT; | ||
paging.MAX_LIMIT = 2; | ||
var res = sync.await(paging.find(db.collection('test_paging_limits'), { | ||
limit: 999 | ||
}, sync.defer())); | ||
expect(res.results.length).toBe(2); | ||
paging.MAX_LIMIT = originalMaxLimit; | ||
}); | ||
}); | ||
}); |
@@ -12,3 +12,4 @@ var _ = require('underscore'); | ||
* -query {Object} The mongo query to pass to Mongo. | ||
* -limit {Number} The page size. Default: 100 | ||
* -limit {Number} The page size. Must be between 1 and 100 (though can be overridden by | ||
* setting MAX_LIMIT). | ||
* -fields {Object} Fields to query in the Mongo object format, e.g. {_id: 1, timestamp :1}. | ||
@@ -28,11 +29,14 @@ * The default is to query all fields. | ||
function find(collection, params, done) { | ||
if (_.isString(params.limit)) params.limit = parseInt(params.limit); | ||
if (params.previous) params.previous = urlSafeDecode(params.previous); | ||
if (params.next) params.next = urlSafeDecode(params.next); | ||
params = _.defaults(params, { | ||
query: {}, | ||
limit: 100, | ||
limit: module.exports.MAX_LIMIT, | ||
paginatedField: '_id' | ||
}); | ||
if (_.isString(params.limit)) params.limit = parseInt(params.limit); | ||
if (params.previous) params.previous = urlSafeDecode(params.previous); | ||
if (params.next) params.next = urlSafeDecode(params.next); | ||
if (params.limit < 1) params.limit = 1; | ||
if (params.limit > module.exports.MAX_LIMIT) params.limit = module.exports.MAX_LIMIT; | ||
@@ -145,3 +149,4 @@ var fields; | ||
module.exports = { | ||
find | ||
find, | ||
MAX_LIMIT: 100 | ||
}; |
26803
5.48%497
7.81%146
3.55%