Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize dynamic query params

I'm sending query params as JSON format in req.query.p from my front-end MVC framework , the point is that this could be a dynamic key and value, for example:

req.query.p = {nombre : 'juan'}

or

req.query.p = {pais : 'chile'}

So I need the key, and the value to put them in the where statement, something like this

exports.select = function(req, res){
    console.log('=> GET | Obtener peliculas'.bold.get);
        db.Pelicula
            .findAndCountAll({
                limit : req.query.limit,
                offset : req.query.offset,
                where : req.query.p ? [req.query.p.KEY + " = ?", req.query.p.VAL] : null
            })
            .success(function(resp){
                console.log(JSON.stringify(resp.rows, null, 4).bold.get);
                res.json({peliculas : resp.rows, meta : { total : resp.count}});
            });
}
like image 226
jFranciscov Avatar asked Sep 29 '14 18:09

jFranciscov


1 Answers

The where parameter can be an object, so you can just pass where: req.query.p

like image 197
Jan Aagaard Meier Avatar answered Nov 02 '22 04:11

Jan Aagaard Meier