如何在 knex.select() 中添加 if 语句?

How to add if statement in knex.select()?

我有这样的代码

const attr = req.query.attr
knex.select(knex.raw('foo_name as name'))

我想要的是 select 该列,如果它满足 attr 的条件,如果不满足则 select 什么都不做。 类似于:

knex.select(if(attr == foo){knex.raw()})

您可以将选择分配给其他变量,然后将其传递给 kenx.select

const selection = attr == foo ? knex.raw('bla') : knex.raw('');
await knex.select(selection);