在具有对象语法的 Knex 查询中使用运算符 'like` 或 'greater than'
Use operators 'like` or 'greater than' in Knex query with object syntax
我正在使用 JSON 个对象来构建 Knex 查询,如下所示:
{where: {id: '5'}
如何将相同的查询格式(对象语法)与 like
或 greater than
等运算符一起使用?
我认为这不可能。查看the relevant source code of the query builder,它看起来像:
_objectWhere(obj) {
const boolVal = this._bool();
const notVal = this._not() ? 'Not' : '';
for (const key in obj) {
this[boolVal + 'Where' + notVal](key, obj[key]);
}
return this;
}
这基本上只用两个参数调用适当的 Where
函数(因此没有运算符,这意味着 =
)
您可以使用where/andWhere。下面的代码显示了仅在 user_id = userId 和 book_reference = bookName 且 结果<结果。
knex('user_books')
.where({
user_id: userId,
book_reference: bookName
})
.andWhere('result', '<', res)
.update({
'updated_at': bookshelf.knex.raw('CURRENT_TIMESTAMP'),
'text_done': true,
})
我正在使用 JSON 个对象来构建 Knex 查询,如下所示:
{where: {id: '5'}
如何将相同的查询格式(对象语法)与 like
或 greater than
等运算符一起使用?
我认为这不可能。查看the relevant source code of the query builder,它看起来像:
_objectWhere(obj) {
const boolVal = this._bool();
const notVal = this._not() ? 'Not' : '';
for (const key in obj) {
this[boolVal + 'Where' + notVal](key, obj[key]);
}
return this;
}
这基本上只用两个参数调用适当的 Where
函数(因此没有运算符,这意味着 =
)
您可以使用where/andWhere。下面的代码显示了仅在 user_id = userId 和 book_reference = bookName 且 结果<结果。
knex('user_books')
.where({
user_id: userId,
book_reference: bookName
})
.andWhere('result', '<', res)
.update({
'updated_at': bookshelf.knex.raw('CURRENT_TIMESTAMP'),
'text_done': true,
})