在具有对象语法的 Knex 查询中使用运算符“喜欢”或“大于”

Hed*_*dge 6 bookshelf.js knex.js

我正在使用 JSON 对象来构建 Knex 查询,如下所示:

{where: {id: '5'}

如何将相同的查询格式(对象语法)与诸如like或 之类的运算符一起使用greater than

Art*_*lho 8

您可以使用where/andWhere。下面的代码显示了仅在user_id = userIdbook_reference = bookName并且result < res 时才会发生的更新。

knex('user_books')
    .where({
      user_id: userId,
      book_reference: bookName
    })
    .andWhere('result', '<', res)
    .update({
      'updated_at': bookshelf.knex.raw('CURRENT_TIMESTAMP'),
      'text_done': true,
    })
Run Code Online (Sandbox Code Playgroud)


Jcl*_*Jcl 2

我认为这是不可能的。查看查询生成器的相关源代码,它看起来像:

_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;
}
Run Code Online (Sandbox Code Playgroud)

它基本上Where只用两个参数调用适当的函数(因此没有运算符,这意味着=