Skip to content

Instantly share code, notes, and snippets.

@amessinger
Created March 27, 2017 13:28
Show Gist options
  • Save amessinger/7380f0b19d531d068e59432a1e01dc3e to your computer and use it in GitHub Desktop.
Save amessinger/7380f0b19d531d068e59432a1e01dc3e to your computer and use it in GitHub Desktop.
expressjs + sequelizejs filterable route
app.get('/albums', function (req, res) {
Album
.findAll({
where: getQueryFilters(req.query),
order: ['albumartist']
})
.then((items)=> {
res.json(items);
});
});
function getQueryFilters(query) {
let filters = null;
if (query.filter) {
filters = Object.keys(query.filter).map((attribute)=> {
let filter = {};
let value = query.filter[attribute];
filter[attribute] = { $like: `%${value}%` }
return filter;
});
}
return filters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment