Code coverage report for lib/pagination.js

Statements: 11.36% (5 / 44)      Branches: 0% (0 / 30)      Functions: 0% (0 / 5)      Lines: 11.36% (5 / 44)      Ignored: none     

All files » lib/ » pagination.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 1071   1                     1                                                                             1                                           1                                                                
var	expressPaginate = require('./expressPaginate');
 
module.exports = function(Model, conf) {
	if (!conf) {
		conf = {
			defaultPageSize: 10,
			maxPageSize: 50
		};
	} else {
		conf.defaultPageSize = conf.defaultPageSize ? conf.defaultPageSize : 10;
		conf.maxPageSize = conf.maxPageSize ? conf.maxPageSize : 50;
	}
 
	function paginate(req, res, next, search, options, cb) {
		expressPaginate.setQueryParams(req, conf.defaultPageSize, conf.maxPageSize);
 
		options = options || {};
 
		if (req.query.sortBy) {
			options.sortBy = {};
 
			var splitFields = req.query.sortBy.split(',');
 
			var ascendingSortDirection = '1';
			var directions = req.query.sortDirection || ascendingSortDirection;
			var splitDirections = directions.split(',');
 
			for(var i = 0; splitFields[i]; i++) {
				splitFields[i] = splitFields[i].trim();
				options.sortBy[splitFields[i]] = splitDirections[i] || 1;
			}
		}
 
		if (req.query.before) {
			options.before = req.query.before;
		}
 
		if (req.query.after) {
			options.after = req.query.after;
		}
 
		if (req.query.last) {
			options.last = req.query.last;
		}
 
		if (!search) {
			search = {};
		}
 
		return searchModelAndPaginate(search, options, req, res, next, cb);
	}
 
	function searchModelAndPaginate(search, options, req, res, next, cb) {
		Model.paginate(
			search,
			req.query.currentPage,
			req.query.page,
			req.query.pageSize,
			function(err, newCurrentPage, before, after, pageCount, numPages, total, items) {
				if (err) {
					next(err);
					return;
				}
 
				if (typeof cb === 'function') {
					cb(formatResponse(req, res, newCurrentPage, before, after, pageCount, numPages, total, items));
				} else {
					return res.json(formatResponse(req, res, newCurrentPage, before, after, pageCount, numPages, total, items));
				}
			},
			options
		);
	}
 
	function formatResponse(req, res, currentPage, before, after, pageCount, numPages, total, items) {
		if (before) {
			req.query.before = before;
		}
 
		if (after) {
			req.query.after = after;
		}
 
		req.query.currentPage = currentPage;
 
		var response = {
			page: currentPage,
			hasMore: expressPaginate.hasNextPages(req)(numPages),
			links: {
				first: expressPaginate.firstPage(req),
				prev: expressPaginate.prevPage(req, numPages),
				next: expressPaginate.nextPage(req, numPages),
				last: expressPaginate.lastPage(req, numPages)
			},
			pageCount: pageCount,
			total: total,
			before: before,
			after: after,
			data: items
		};
 
		return response;
	}
 
	return paginate;
};