Created
March 27, 2017 13:28
-
-
Save amessinger/7380f0b19d531d068e59432a1e01dc3e to your computer and use it in GitHub Desktop.
expressjs + sequelizejs filterable route
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
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