CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 63 forks source link

Ability to view generated sql ? #64

Closed scolestock closed 7 years ago

scolestock commented 7 years ago

I'm curious whether there is a way to view the generated sql query, as the underlying mysql library provides for? thanks much !

var post  = {id: 1, title: 'Hello MySQL'};
var query = connection.query('INSERT INTO posts SET ?', post, function (error, results, fields) {
  if (error) throw error;
  // Neat! 
});
console.log(query.sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL' 
CodeFoodPixels commented 7 years ago

This will be added with the work being done in the mysql-wrapper branch. I'm working on rewriting the tests at the moment to get decent coverage.

BorodinDK commented 5 years ago

@lukeb-uk Are there any updates on this issue?

CodeFoodPixels commented 5 years ago

If you set returnArgumentsArray to true in the connection options (https://github.com/lukeb-uk/node-promise-mysql#connectionoptions-object) you will get an array back where the last argument is the return value from the query function call. You can do .sql on that to get the sql.

Untested code, but it should give you an idea:

let connection;

    mysql.createConnection({
        user: 'root',
        password: 'password',
        database: 'employees',
        returnArgumentsArray: true
    }).then((conn) => {
        connection = conn;

        return connection.query('select * from employees limit 0, 10');
    }).then(([data, fields, query]) => {
        console.log(`The SQL for the query was: ${query.sql}\n`);

        connection.end();
    });