Created
April 18, 2019 14:32
-
-
Save hieulm/1aab3083c0bb58d93bdd65f4cf306446 to your computer and use it in GitHub Desktop.
part sort object from query string in javascript
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
const records = await eventColl | |
.aggregate([ | |
{ | |
$match: { | |
company_id, | |
soft_delete: false, | |
}, | |
}, | |
{ | |
$sort: sortObj, | |
}, | |
{ | |
$skip: (parseInt(page, 10) - 1) * parseInt(limit, 10), | |
}, | |
{ | |
$limit: parseInt(limit, 10), | |
}, | |
]) | |
.toArray(); |
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
const querystring = require('querystring'); | |
module.exports = query => | |
Promise.resolve( | |
querystring | |
.unescape(query.sort_by) | |
.split(',') | |
.map(e => e.split(' ')) | |
.reduce( | |
(o, currentObj) => | |
Object.assign(o, { | |
[currentObj[0]]: currentObj[1] === 'asc' ? 1 : -1, | |
}), | |
{} | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment