Created
February 10, 2014 18:47
-
-
Save cortfritz/8921761 to your computer and use it in GitHub Desktop.
sailsjs blueprint is strangely sensitive to query parameters set in a policy
This file contains 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
/** | |
* queryLimited | |
* | |
* @module :: queryLimited | |
* @description :: policy ensures the existence of query variables limit and skip and keeps them in reasonable bounds | |
* @docs :: http://sailsjs.org/#!documentation/policies | |
* | |
*/ | |
module.exports = function queryLimited(req, res, next) { | |
var defaultLimit = 10 | |
var limit = req.query.limit || defaultLimit | |
var skip = req.query.skip || 0 | |
if( isNaN(limit)){ | |
limit = defaultLimit | |
} | |
else if (limit > 1000){ | |
limit = 1000 | |
} | |
else if (limit < 1){ | |
limit = 1 | |
} | |
if (isNaN(skip)){ | |
skip = 0 | |
} | |
else if (skip > 1000000){ | |
skip = 1000000 | |
} | |
else if (skip < 0){ | |
skip = 1 | |
} | |
var lastPage = skip - limit | |
if( lastPage < 0){ | |
lastPage = 0 | |
} | |
console.log(__filename,'query before', req.query) | |
/* | |
* uncommeting this will make the blueprint for /modelname return empty array | |
* | |
req.query.page = { | |
limit: limit | |
, skip: skip | |
, nextPage: skip + limit | |
, lastPage: lastPage | |
} | |
* | |
*/ | |
req.query.limit = limit | |
req.query.skip = skip | |
req.query.nextPage = skip + limit | |
/* | |
* uncommeting this will make the blueprint for /modelname return empty array | |
* it apparently does not matter what I name the query variable | |
* | |
req.query.lastPage = previousSkip | |
* | |
*/ | |
console.log(__filename,'query after', req.query) | |
next() | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment