Dan*_*Dan 2 javascript knex.js
出于调试目的,我希望看到SQL查询knex正在执行。例如,我想查看knex为该代码生成的SQL :
knex('statistics')
.del()
.where({
'stats': 'reddit',
});
Run Code Online (Sandbox Code Playgroud)
Ale*_*sev 21
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toString();
Run Code Online (Sandbox Code Playgroud)
Jer*_*ier 17
.toString()并将.toQuery()完整的 SQL 查询作为字符串返回
对于你的例子:
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toString();
Run Code Online (Sandbox Code Playgroud)
返回:
delete from "statistics" where "stats" = 'reddit'
Run Code Online (Sandbox Code Playgroud)
而.toSQL().toNative()返回一个对象:
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toSQL().toNative();
Run Code Online (Sandbox Code Playgroud)
{
sql: 'select * from "books" where "author" = $1',
bindings: [ 'John Doe' ]
}
Run Code Online (Sandbox Code Playgroud)
要打印所有查询,然后debug: true在初始化对象上传递一个标志将为所有查询打开debugging。
knex({
client: 'mysql',
debug: true,
connection: {
host: '127.0.0.1',
port: 3306,
user: 'root',
password: '',
database: ''
}
})
Run Code Online (Sandbox Code Playgroud)
参考: https: //knexjs.org/#Installation-debug
http://knexjs.org/#Interfaces-toSQL
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toSQL().toNative()
Run Code Online (Sandbox Code Playgroud)