Node-mysql2: How do you print MySQL query statements with mysql2/promise?

Created on 20 Mar 2018  路  2Comments  路  Source: sidorares/node-mysql2

I can see that node-mysql offers the ability to print the statement by providing query.sql, however when I try that with native promises, it returns undefined, e.g.:

connection.query('SELECT * FROM mytable WHERE id = 1')
.then(query => {
  console.log(query.sql)
})

returns undefined. Am I missing anything?

Thank you!

question

Most helpful comment

at the moment this is not possible, promise api only gives you rows + fields
We could potentially return Query object as well so you would be able to do something like this

   const [rows, fields, query] = await conn.query('select ? as test', [123]);
   console.log(query.sql);

I think right now recommended way would be create string first and later use it:

  const sql = conn.format('select 1 as test', [123]);
  console.log(sql)
  const [rows] = await conn.query(sql);

All 2 comments

at the moment this is not possible, promise api only gives you rows + fields
We could potentially return Query object as well so you would be able to do something like this

   const [rows, fields, query] = await conn.query('select ? as test', [123]);
   console.log(query.sql);

I think right now recommended way would be create string first and later use it:

  const sql = conn.format('select 1 as test', [123]);
  console.log(sql)
  const [rows] = await conn.query(sql);

@sidorares
That's why I made this PR #555 a year ago.
This comment (EDIT 4 part) is exactly what you're talking about. Any plans to implement?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DirkWolthuis picture DirkWolthuis  路  3Comments

MHDante picture MHDante  路  4Comments

jomel picture jomel  路  8Comments

patrikx3 picture patrikx3  路  7Comments

HugoMuller picture HugoMuller  路  3Comments