0

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
Dally
  • 1,281
  • 4
  • 18
  • 37

1 Answers1

0

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)
fguchelaar
  • 4,779
  • 23
  • 36
  • Unfortunately, that doesn't work. The only variables available are ```rows``` and ```fields```. – Dally May 17 '23 at 18:24
  • 1
    hmm.. sorry. Maybe format the sql in a separate statement and then log en execute it? `connection.format(...)` – fguchelaar May 17 '23 at 19:44