javascript 我可以有条件地向我的 knex 查询添加 where() 子句吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27232415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:11:36  来源:igfitidea点击:

Can I conditionally add a where() clause to my knex query?

javascriptnode.jsknex.js

提问by eponymous23

I want to add a where()clause in my query, but conditionally. Specifically, I want it added only if a sepecific querystring parameter is passed in the URL. Is this possible, and if so, how would I go about doing it?

我想where()在我的查询中添加一个子句,但有条件地. 具体来说,我希望只有在 URL 中传递了特定的查询字符串参数时才添加它。这是可能的,如果是这样,我将如何去做?

router.get('/questions', function (req, res) {
    knex('questions')
        .select('question', 'correct', 'incorrect')
        .limit(50)
        .where('somecolumn', req.query.param) // <-- only if param exists
        .then(function (results) {
            res.send(results);
        });
});

采纳答案by Simon Briche

You can store your query in a variable, apply your conditional where clause and then execute it, like this :

您可以将查询存储在变量中,应用条件 where 子句然后执行它,如下所示:

router.get('/questions', function(req, res) {
  var query = knex('questions')
              .select('question', 'correct', 'incorrect')
              .limit(50);

  if(req.query.param == some_condition)
    query.where('somecolumn', req.query.param) // <-- only if param exists
  else
    query.where('somecolumn', req.query.param2) // <-- for instance

  query.then(function(results) {
    //query success
    res.send(results);
  })
  .then(null, function(err) {
    //query fail
    res.status(500).send(err);
  });
});

回答by Itai Noam

Yes. Use modify.

是的。使用修改

As applied to your example:

适用于您的示例:

router.get('/questions', function (req, res) {
    knex('questions')
        .select('question', 'correct', 'incorrect')
        .limit(50)
        .modify(function(queryBuilder) {
            if (req.query.param) {
                queryBuilder.where('somecolumn', req.query.param);
            }
        })   
        .then(function (results) {
            res.send(results);
        });
});

回答by Merey Nurlan

You can actually use query builder inside .where() like so:

您实际上可以在 .where() 中使用查询构建器,如下所示:

.where((qb) => {condition == true ? do something if true : do something if false })

IMO @ItaiNoam answer should be correct with .modify()

IMO @ItaiNoam 答案应该是正确的 .modify()

回答by Josh Lankford

You can do it by checking if your query string is present and running a different query.

您可以通过检查您的查询字符串是否存在并运行不同的查询来实现。

router.get('/questions', function(req, res) {
  if (req.query.yourQueryString) {
    // Run your more specific select
  } else {
    knex('questions').select('question', 'correct', 'incorrect').limit(50).where(
        'somecolumn', req.query.param) // <-- only if param exists
      .then(function(results) {
        res.send(results);
      });
  }
}
});