Last active
May 18, 2016 15:08
-
-
Save nanyaks/8e88aa55edbf2a78e40bf76a8cf6e518 to your computer and use it in GitHub Desktop.
DS pagination using the response service.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listPayout: function (req, res) { | |
var pagination = { | |
page: parseInt(req.query.page) || 1, | |
limit: parseInt(req.query.perPage) || 10 | |
}; | |
var criteria = { | |
isDeleted: false | |
}; | |
AppService.paginate('modelName', criteria, pagination).spread(function (err, count, results){ | |
if (err) { return AppService.sendResponse(res, err); } | |
var data = { | |
message: "Records retrieved successfully", | |
data: results | |
}; | |
return AppService.sendPaginatedResponse(res, count, pagination, data); | |
}) | |
.catch(function (err){ | |
return ValidationService.jsonResolveError(err, res); | |
}); | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @function sendResponse | |
* | |
* @description - send a response | |
* @param res - Glogal sails object `res` | |
* @param obj - Object containing reponse data | |
*/ | |
sendResponse: function (res, obj) { | |
return ResponseService.json(obj.code, res, obj.message, obj.data, obj.meta); | |
}, | |
/** | |
* @function sendPaginatedResponse | |
* | |
* @description - send paginated response | |
* @param res - Glogal sails object `res` | |
* @param count - Object count | |
* @param pagination - Pagination options | |
* @param payload - Data payload | |
*/ | |
sendPaginatedResponse: function (res, count, pagination, payload) { | |
var self = this; | |
var data = { | |
code : (payload.code) ? payload.code : 200, | |
data : payload.data, | |
message : (payload.data.length) ? payload.message : "No records found", | |
meta : self.paginationMetadata(count, pagination) | |
}; | |
return self.sendResponse(res, data); | |
}, | |
/** | |
* @function paginationMetadata | |
* | |
* @description - Generate pagination metadata | |
* @param count - object count | |
* @param pagination - pagination options | |
*/ | |
paginationMetadata: function (count, pagination) { | |
var numberOfPages = Math.ceil(count / pagination.limit); | |
var nextPage = parseInt(pagination.page) + 1; | |
var metadata = { | |
page : pagination.page, | |
perPage : pagination.limit, | |
previousPage : (pagination.page > 1) ? parseInt(pagination.page) - 1 : false, | |
nextPage : (numberOfPages >= nextPage) ? nextPage : false, | |
pageCount : numberOfPages, | |
total : count | |
}; | |
return metadata; | |
}, | |
/** | |
* @function paginate | |
* | |
* @description - Make a query and return paginated | |
* @param model - model to query from | |
* @param criteria - search criteria | |
* @param options - search pagination options | |
*/ | |
paginate: function (model, criteria, options) { | |
var self = this; | |
return self.retrievePaginationData(model, criteria, options).spread(function (err, count, result){ | |
if (err) { return [err]; } | |
return [null, count, result]; | |
}); | |
}, | |
/** | |
* @function retrievePaginationData | |
* | |
* @description - Make a query and return promise array | |
* @param model - model to query from | |
* @param criteria - search criteria | |
* @param options - search pagination options | |
*/ | |
retrievePaginationData: function (model, criteria, paginationOptions) { | |
var self = this; | |
return sails.models[model].count(criteria).then(function (count){ | |
if (!count) { | |
return [{code: 200, message: self.toTitleCase(model) + "\'s not found", data: []}]; | |
} | |
var promiseQuery = sails.models[model].find(criteria).paginate(paginationOptions); | |
return [null, count, promiseQuery]; | |
}) | |
.catch(function (err){ | |
return [err]; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment