Permalink
Browse files

Adds mongoose pagination plugin

  • Loading branch information...
0 parents commit 9f8fe323c518e18b604a787adee97c2039dde491 @cgarvis cgarvis committed Oct 11, 2012
Showing with 149 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +4 −0 .travis.yml
  3. +41 −0 README.md
  4. +30 −0 lib/pagination.js
  5. +30 −0 package.json
  6. +43 −0 test/paginate.coffee
@@ -0,0 +1 @@
+node_modules
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.8
+ - 0.6
@@ -0,0 +1,41 @@
+# mongoose-pagination
+
+## Installation
+
+```bash
+$ npm install mongoose-pagination
+```
+
+## Usage
+
+```javascript
+require 'mongoose-pagination'
+
+model
+ .find()
+ .paginate(1, 10)
+ .exec(function(err, docs, total) {
+ console.log('total: ', total, 'docs: ', docs)
+ });
+
+model
+ .find()
+ .paginate(1, 10, function(err, docs, total) {
+ console.log('total: ', total, 'docs: ', docs)
+ });
+```
+
+## Tests
+
+```bash
+$ npm test
+```
+
+## Authors [Christopher Garvis][0] & [Moveline][1]
+
+[0]: http://christophergarvis.com
+[1]: http://www.moveline.com
+
+## License
+
+MIT
@@ -0,0 +1,30 @@
+var mongoose = require('mongoose');
+
+mongoose.Query.prototype.paginate = function paginate (page, limit, cb) {
+ page = page || 1;
+ limit = limit || 10;
+
+ var query = this;
+ var model = this.model;
+ var skipFrom = (page * limit) - limit;
+
+ query = query.skip(skipFrom).limit(limit);
+
+ if(cb) {
+ query.exec(function(err, docs) {
+ if(err) {
+ cb(err, null, null);
+ } else {
+ model.count(query._conditions, function(err, total) {
+ if(err) {
+ cb(err, null, null);
+ } else {
+ cb(null, docs, total);
+ }
+ });
+ }
+ });
+ } else {
+ return this;
+ }
+};
@@ -0,0 +1,30 @@
+{
+ "name": "mongoose-pagination",
+ "version": "1.0.0",
+ "description": "Mongoose Query Pagination",
+ "main": "./lib/pagination.js",
+ "dependencies":{
+ "mongoose":">=2.7.0"
+ },
+ "devDependencies":{
+ "chai": "~1.3.0",
+ "coffee-script": "~1.3.3",
+ "mocha": "~1.6.0"
+ },
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script"
+ },
+ "keywords": [
+ "mongoose",
+ "paginate",
+ "pagination",
+ "sort",
+ "page",
+ "limit"
+ ],
+ "author": "Moveline <www.moveline.com>",
+ "license": "MIT"
+}
@@ -0,0 +1,43 @@
+chai = require 'chai'
+mongoose = require 'mongoose'
+
+should = chai.should()
+
+require '../lib/pagination'
+
+describe 'Mongoose Pagination', ->
+ query = null
+
+ beforeEach ->
+ query = new mongoose.Query
+
+ it 'should add a paginate method to Query\s prototype', ->
+ should.exist(mongoose.Query.prototype.paginate)
+
+ describe 'defaults', ->
+ beforeEach ->
+ query.paginate()
+
+ it 'should skip to 0', ->
+ query.options.should.include.keys 'skip'
+ query.options.skip.should.be.eq 0
+ it 'should limit to 10', ->
+ query.options.should.include.keys 'limit'
+ query.options.limit.should.be.eq 10
+
+ describe 'paginate', ->
+ it 'should skip 10 when page is 2', ->
+ query.paginate 2
+ query.options.should.include.keys 'skip'
+ query.options.skip.should.be.eq 10
+
+ it 'should skip 20 when page is 3', ->
+ query.paginate 3
+ query.options.should.include.keys 'skip'
+ query.options.skip.should.be.eq 20
+
+ it 'should skip 30 when page is 3 and limit is 15', ->
+ query.paginate 3, 15
+ query.options.should.include.keys 'skip', 'limit'
+ query.options.skip.should.be.eq 30
+ query.options.limit.should.be.eq 15

0 comments on commit 9f8fe32

Please sign in to comment.