【问题标题】:How can I view the query `knex` builds?如何查看查询“knex”构建?
【发布时间】:2018-04-30 08:47:09
【问题描述】:

出于调试目的,我想查看 SQL 查询 knex 正在执行。例如,我想查看knex 为这段代码生成的 SQL:

 knex('statistics')
    .del()
    .where({
        'stats': 'reddit',
    }); 

【问题讨论】:

    标签: javascript knex.js


    【解决方案1】:

    http://knexjs.org/#Interfaces-toSQL

    knex('statistics')
        .del()
        .where({
            'stats': 'reddit',
        }).toSQL().toNative()
    

    【讨论】:

    • 获取查询字符串为toNative().sql
    • 获取该查询的参数绑定是toNative().bindings
    【解决方案2】:
    knex('statistics')
        .del()
        .where({
            'stats': 'reddit',
        }).toString();
    

    【讨论】:

    • 我不认为这是对已接受答案的改进。
    • 伙计们,我试图找到获取原始 SQL 的方法。 toString().toSQL().toNative() 相同,但更短。你真的认为只有一个正确答案吗?
    • 这适用于 knex 0.21
    【解决方案3】:

    在我的情况下 toSQL()... 不会产生“已解析”的 SQL 字符串,只有 toString() 有效,我不确定这是否取决于查询生成器的具体用法。

    【讨论】:

      【解决方案4】:

      要打印所有查询,然后在初始化对象上传递debug: true 标志,将为所有查询打开debugging

      knex({
          client: 'mysql',
          debug: true,
          connection: {
              host: '127.0.0.1',
              port: 3306,
              user: 'root',
              password: '',
              database: ''
          }
      })
      

      参考:https://knexjs.org/#Installation-debug

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2014-03-15
        • 1970-01-01
        相关资源
        最近更新 更多