Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

working

  • Loading branch information...
commit cbf78514bf0ae1686422a731f3d27f7f9c473e59 1 parent bc5c7c9
Edward Hotchkiss authored
Showing with 20 additions and 4 deletions.
  1. +8 −4 lib/mongoose-paginate.js
  2. +12 −0 test/index.test.js
View
12 lib/mongoose-paginate.js
@@ -8,16 +8,20 @@ var mongoose = require('mongoose');
/*!
@method paginate
@param {Object} query Mongoose Query Object
- @param {Number} perPage
+ @param {Number} pageNumber
+ @param {Number} resultsPerPage
Extend Mongoose Models to paginate queries
*/
-mongoose.Model.paginate = function(query, start, end, callback){
+
+mongoose.Model.paginate = function(q, pageNumber, resultsPerPage, callback){
callback = callback || function(){};
+ var skipFrom = pageNumber * resultsPerPage;
+ var query = this.find(q).skip(skipFrom).limit(resultsPerPage);
query.exec(function(error, results) {
if (error) {
- console.error(error);
+ callback(error, null);
} else {
- console.log(results);
+ callback(null, results);
}
});
}
View
12 test/index.test.js
@@ -107,6 +107,18 @@ vows.describe('pagination module basic test')
})
.addBatch({
+ 'when paginating BlogEntry querying for all documents, with page 1, 10 per page':{
+ topic:function(){
+ BlogEntry.paginate({}, 1, 10, this.callback);
+ },
+ 'there should be no errors and results.length should be 10':function(error, results){
+ assert.equal(error, null);
+ assert.equal(results.length, 10);
+ }
+ }
+})
+
+.addBatch({
'when deleting all of our 100 dummy documents with our test mongodb string':{
topic:function(){
teardown(this.callback);
Please sign in to comment.
Something went wrong with that request. Please try again.