Node + MySQL - How to get the raw query with bindings

652 Views Asked by At

is it possible to get the raw SQL query with its bindings either prior or after execution to see what's about to, or has just executed?

Example:

const id = 4

const connection = await fastify.mysql.getConnection()
const [rows] = await connection.query(
    `SELECT age FROM users WHERE id = :id`, { id: id },
)
connection.release()
return rows

Is there a way to return:

SELECT age FROM users WHERE id = 4
1

There are 1 best solutions below

2
fguchelaar On

Reading up on this subject, I expect it would be possible to get the sql by destructuring the result like this:

const [rows, fields, query] = await connection.query(
    `SELECT age FROM users WHERE id = :id`, { id: id },
)

...

console.log(query.sql)