Skip to content

Instantly share code, notes, and snippets.

@tomsarduy
Last active September 21, 2016 20:21
Show Gist options
  • Save tomsarduy/3c15cfee01e1ab021df838a5b0516d9e to your computer and use it in GitHub Desktop.
Save tomsarduy/3c15cfee01e1ab021df838a5b0516d9e to your computer and use it in GitHub Desktop.
_ = require('lodash');
/**
* Parse Query from Express req format to oDATA
* Parameters supported: $filter, $select, $orderby, $top, $skip
* Change the application status of the application
* @param req.query
* @return object
*/
exports.parseQuery = function(query) {
var result = {};
if (query) {
if (query.count) {
result.$top = query.count;
}
if (query.page){
result.$skip = (query.page-1)*query.count;
}
if (query.sorting){
result.$orderby = "";
_.forEach(query.sorting, (value, key) => {
key = _.replace(key, '.', '/');
result.$orderby += key + " " + value + ", ";
});
result.$orderby = result.$orderby.slice(0, -2);
}
if (query.filter) {
result.$filter = "";
_.forEach(query.filter, (value, key) => {
key = _.replace(key, '.', '/');
result.$filter += "substringof(" + key + ",'" + decodeURIComponent(value) + "') and ";
});
// remove last "and"
result.$filter = result.$filter.slice(0, -5);
}
if (query.select) {
result.$select = decodeURIComponent(query.select);
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment