Skip to content

Commit

Permalink
more tests, new formula
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Hotchkiss committed Dec 16, 2011
1 parent 4f4e0e6 commit e79f4bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/mongoose-paginate.js
Expand Up @@ -15,7 +15,8 @@ var mongoose = require('mongoose');


mongoose.Model.paginate = function(q, pageNumber, resultsPerPage, callback){ mongoose.Model.paginate = function(q, pageNumber, resultsPerPage, callback){
callback = callback || function(){}; callback = callback || function(){};
var skipFrom = pageNumber * resultsPerPage; // formula 1, hah.
var skipFrom = (pageNumber * resultsPerPage) - resultsPerPage;
var query = this.find(q).skip(skipFrom).limit(resultsPerPage); var query = this.find(q).skip(skipFrom).limit(resultsPerPage);
query.exec(function(error, results) { query.exec(function(error, results) {
if (error) { if (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author":"Edward Hotchkiss <e@ingk.com>", "author":"Edward Hotchkiss <e@ingk.com>",
"name":"mongoose-paginate", "name":"mongoose-paginate",
"description":"Mongoose ORM (NodeJS) Document Query Pagination", "description":"Mongoose ORM (NodeJS) Document Query Pagination",
"version":"0.0.2", "version":"0.0.3",
"repository":{ "repository":{
"type":"git", "type":"git",
"url":"git://github.com/edwardhotchkiss/mongoose-paginate.git" "url":"git://github.com/edwardhotchkiss/mongoose-paginate.git"
Expand Down
7 changes: 4 additions & 3 deletions test/index.test.js
Expand Up @@ -107,13 +107,14 @@ vows.describe('pagination module basic test')
}) })


.addBatch({ .addBatch({
'when paginating BlogEntry querying for all documents, with page 1, 10 per page':{ 'when paginating BlogEntry querying for all documents, with page 2, 10 per page':{
topic:function(){ topic:function(){
BlogEntry.paginate({}, 1, 10, this.callback); BlogEntry.paginate({}, 2, 10, this.callback);
}, },
'there should be no errors and results.length should be 10':function(error, results){ 'there should be no errors and results.length should be 10, and the first result should contain the correct # (11)':function(error, results){
assert.equal(error, null); assert.equal(error, null);
assert.equal(results.length, 10); assert.equal(results.length, 10);
assert.equal(/#11/.test(results[0].title), true);
} }
} }
}) })
Expand Down

0 comments on commit e79f4bf

Please sign in to comment.