Skip to content

Instantly share code, notes, and snippets.

@brianc
Created January 23, 2012 15:40
Show Gist options
  • Save brianc/1663864 to your computer and use it in GitHub Desktop.
Save brianc/1663864 to your computer and use it in GitHub Desktop.
monkey-patch node-postgres to log queries
var Query = require('pg').Query;
var actualSubmit = Query.prototype.submit;
Query.prototype.submit = function() {
console.log(this.text);
actualSubmit.apply(this, arguments);
}
@tugorez
Copy link

tugorez commented May 28, 2018

How about this ? This actually logs the final query.

const Query = require('pg').Query;
const submit = Query.prototype.submit;
Query.prototype.submit = function() {
  const text = this.text;
  const values = this.values;
  const query = values.reduce((q, v, i) => q.replace(`$${i + 1}`, v), text);
  console.log(query);
  submit.apply(this, arguments);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment