如何在Knex.js中动态创建where条件?
How to create where conditions dynamically in Knex.js?
我需要根据收到的参数用where子句创建一个查询,但我不知道如何在where子句中包含一些参数。在下面的代码中,四个第一个条件(customerId、companyId、invoiceId 和 open)运行良好,但我不知道如何包含其他条件(closed、openedFrom 和 openedTo)。
const getByParams = (req, res) => {
const conditions = {}
if (req.query.customerId) conditions['wo.customerId'] = req.query.customerId
if (req.query.companyId) conditions['wo.companyId'] = req.query.companyId
if (req.query.invoiceId) conditions['wo.invoiceId'] = req.query.invoiceId
if (req.query.open) conditions['wo.finishedAt'] = null
const onlyClosed = req.query.closed ? 'wo.finishedAt != null' : ''
const openedFrom = req.query.openedFrom ? `wo.openingAt >= ${req.query.openedFrom}` : ''
const openedTo = req.query.openedTo ? `wo.openingAt <= ${req.query.openedTo}` : ''
app.db('workOrders as wo')
.select('wo.*',
'wos.serviceId',
'wos.cost',
'srv.name as serviceName')
.leftJoin('workOrderServices as wos', 'wo.id', 'wos.workOrderId')
.leftJoin('services as srv', 'wos.serviceId', 'srv.id')
.whereNull('wo.canceledAt')
.where(conditions)
.then(wo => {
const json = JSON.stringify(wo)
const newJson = convertJson(json)
res.status(200).send(newJson)
})
.catch(err => res.status(500).send(err))
}
参考https://knexjs.org/#Builder-where
试试这个:
.where((builder) => {
if (req.query.customerId)
builder.where('wo.customerId', req.query.customerId);
if (req.query.companyId)
builder.where('wo.companyId', req.query.companyId);
if (req.query.invoiceId)
builder.where('wo.invoiceId', req.query.invoiceId);
if (req.query.open)
builder.whereNull('wo.finishedAt');
// so on..
});
而不是
.where(conditions)
如果所有过滤器都是AND 'equals'语句,另一种方式是
.where({
...(req.query.customerId && {'wo.customerId': req.query.customerId})
...(req.query.companyId && {'wo.companyId': req.query.companyId})
...(req.query.invoiceId && {'wo.invoiceId': req.query.invoiceId})
});
我需要根据收到的参数用where子句创建一个查询,但我不知道如何在where子句中包含一些参数。在下面的代码中,四个第一个条件(customerId、companyId、invoiceId 和 open)运行良好,但我不知道如何包含其他条件(closed、openedFrom 和 openedTo)。
const getByParams = (req, res) => {
const conditions = {}
if (req.query.customerId) conditions['wo.customerId'] = req.query.customerId
if (req.query.companyId) conditions['wo.companyId'] = req.query.companyId
if (req.query.invoiceId) conditions['wo.invoiceId'] = req.query.invoiceId
if (req.query.open) conditions['wo.finishedAt'] = null
const onlyClosed = req.query.closed ? 'wo.finishedAt != null' : ''
const openedFrom = req.query.openedFrom ? `wo.openingAt >= ${req.query.openedFrom}` : ''
const openedTo = req.query.openedTo ? `wo.openingAt <= ${req.query.openedTo}` : ''
app.db('workOrders as wo')
.select('wo.*',
'wos.serviceId',
'wos.cost',
'srv.name as serviceName')
.leftJoin('workOrderServices as wos', 'wo.id', 'wos.workOrderId')
.leftJoin('services as srv', 'wos.serviceId', 'srv.id')
.whereNull('wo.canceledAt')
.where(conditions)
.then(wo => {
const json = JSON.stringify(wo)
const newJson = convertJson(json)
res.status(200).send(newJson)
})
.catch(err => res.status(500).send(err))
}
参考https://knexjs.org/#Builder-where
试试这个:
.where((builder) => {
if (req.query.customerId)
builder.where('wo.customerId', req.query.customerId);
if (req.query.companyId)
builder.where('wo.companyId', req.query.companyId);
if (req.query.invoiceId)
builder.where('wo.invoiceId', req.query.invoiceId);
if (req.query.open)
builder.whereNull('wo.finishedAt');
// so on..
});
而不是
.where(conditions)
如果所有过滤器都是AND 'equals'语句,另一种方式是
.where({
...(req.query.customerId && {'wo.customerId': req.query.customerId})
...(req.query.companyId && {'wo.companyId': req.query.companyId})
...(req.query.invoiceId && {'wo.invoiceId': req.query.invoiceId})
});