Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toString debugging when using Bookshelf #609

Closed
rarkins opened this issue Dec 30, 2014 · 3 comments
Closed

toString debugging when using Bookshelf #609

rarkins opened this issue Dec 30, 2014 · 3 comments
Labels

Comments

@rarkins
Copy link

rarkins commented Dec 30, 2014

I'm using bookshelf->knex->pg and need to dig deeper into some of the SQL queries that are being generated. I would like to see the toString() outputs for every query that passes through knex, i.e. without any $1, $2 etc. Is there a way to enable this without a monkey patch, or a patch which can do this simply?

@johanneslumpe
Copy link
Contributor

You could listen to the query event of your knex instance and then interpolate the bindings:

var bindingRegex = /\?/g;
knexInstance.on('query', function (query) {
  var params = query.bindings.slice();
  var q = query.sql.replace(bindingRegex, function () {
    return params.unshift();
  });

  console.log(new Date().toISOString(), '\n', q, '\n');
});

Is this what you are looking for?

@tgriesser
Copy link
Member

Debugging is pretty high on the priority list of things that need work.

I'm currently working on retooling a lot of the internals of knex & bookshelf to make things a lot simpler with an emphasis on debugging and robust query generation. Should see some new things that make this available without temporary patches showing up in the next month or two,

The snippet @johanneslumpe shared should definitely help in the meantime

@rarkins
Copy link
Author

rarkins commented Dec 31, 2014

Hi, and thanks for the quick and positive responses. FYI I modified this a little to correct a typo plus make it copy-and-pasteable:

  knexInstance.on('query', function (query) {
    var params = query.bindings.slice();
    var q = query.sql.replace(/\?/g, function () {
      return '\'' + params.shift() + '\'';
    });
    console.log(new Date().toISOString(), '\n', q, '\n');
  });

@rarkins rarkins closed this as completed Dec 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants