For debugging purposes, I want to see the SQL query knex is executing. For example, I want to view SQL that knex produces for this code:
knex('statistics')
.del()
.where({
'stats': 'reddit',
});
For debugging purposes, I want to see the SQL query knex is executing. For example, I want to view SQL that knex produces for this code:
knex('statistics')
.del()
.where({
'stats': 'reddit',
});
https://knexjs.org/guide/interfaces.html#other
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toSQL().toNative()
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toString();
.toString() and .toQuery() return the full SQL query as a string
For your example:
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toString();
returns:
delete from "statistics" where "stats" = 'reddit'
Whereas .toSQL().toNative() returns an object:
knex('statistics')
.del()
.where({
'stats': 'reddit',
}).toSQL().toNative();
{
sql: 'select * from "books" where "author" = $1',
bindings: [ 'John Doe' ]
}
To print all queries then passing a debug: true flag on your initialization object will turn on debugging for all queries.
knex({
client: 'mysql',
debug: true,
connection: {
host: '127.0.0.1',
port: 3306,
user: 'root',
password: '',
database: ''
}
})
In my case toSQL()... does not produce the "parsed" SQL string, only toString() works, I'm not sure if this depends on one's specific usage of the Query Builder.
you can use toKnexQuery().toSQL()
console.log(query.toKnexQuery().toSQL())