Mongoose Optimized Paginate (MOP)
Optimized pagination using indexes (no cursor.skip) with fallback. Used with Mongoose + Hapi.js.
Fork of MOP
Getting Started
$ npm install hapi-mongoose-opt-paginate
The MOP plugin returns an object containing a property for accessing the api (paginate function) and a property for accessing the mongoose plugin.
The first step is to include the plugin in your mongoose instance e.g.
var mongoose = pagination = ; mongoose;
Now you can start paginating!
Examples (found it at examples/server.js)
var mongoose = ;var Hapi = ;var Pagination = ; // Create Hapi.js servervar server = ; var mongodb_uri = processenvMONGODB_URI || 'mongodb://localhost:27017/hapi_mongoose_pagination' // Connect to mongodbmongoose; // When successfully connectedmongooseconnection; // If the connection throws an errormongooseconnection; // When the connection is disconnectedmongooseconnection; // If the Node process ends, close the Mongoose connectionprocess; // ** required **mongoose; var TestSchema = mongoose; var Test = mongoose; for var i = 0; i < 100; i++ Test; // returns a function(req, reply, search, options, cb)paginate_test = Pagination; server; server; server; serverstart { console;};
Things to Note
If sorting, make sure a compound index (collection-level) exists for the sort field(s) and _id field in proper order
e.g. So if you're sorting by name, date, and _id (_id is always there by default and follows the same order as the first/primary sort key: name, in this case) and you want optimized pagination, the compound indexes {name: 1, date: 1, _id: 1} (for optimization when primary sort key in ascending) and {name: -1, date: 1, _id: -1} (for optimization when primary sort key in descending) should exist on the collection-level (not schema-level). Opposite compound indexes don't need to be created i.e. {name: -1, date: -1, _id: -1} and {name: 1, date: -1, _id: 1} wouldn't need to be added if the above indexes already exist.
If sorting (with more than by _id since it's always included) and a matching compound index is not found, pagination will fall back to a non-optimized state. If it's just by _id, no worries, there's a default index for _id that always exists.
Todo's
- Write unit and integration tests (Current test be part of forked repo.)
- Complete Documentation (ASAP)
Release History
0.1.2
- Example and Readme update on sample of results object.
0.1.1
- Package tags update to include hapi.js
0.1.0
- Full integration with hapi.js request/reply style.
- Created example
- TODO: Update unit test.
License
MIT