diff --git a/lib/client.js b/lib/client.js index cca5e66e8..1c7e03bdf 100644 --- a/lib/client.js +++ b/lib/client.js @@ -531,6 +531,9 @@ Client.prototype.query = function (config, values, callback) { } this.queryQueue.push(query) + process.nextTick(() => { + this.emit('query', query) + }) this._pulseQueryQueue() return result } diff --git a/lib/native/client.js b/lib/native/client.js index 6859bc2cc..e5e9e2405 100644 --- a/lib/native/client.js +++ b/lib/native/client.js @@ -221,6 +221,9 @@ Client.prototype.query = function (config, values, callback) { } this._queryQueue.push(query) + process.nextTick(() => { + this.emit('query', query) + }) this._pulseQueryQueue() return result } diff --git a/test/integration/client/api-tests.js b/test/integration/client/api-tests.js index c274bbd36..3bc4d4d50 100644 --- a/test/integration/client/api-tests.js +++ b/test/integration/client/api-tests.js @@ -247,3 +247,39 @@ suite.test('null and undefined are both inserted as NULL', function (done) { }) ) }) + +suite.test('"query" event fired on query called', function (done) { + const pool = new pg.Pool() + pool.connect( + assert.calls(function (err, client, release) { + assert(!err) + const queryText = 'SELECT 1' + client.query(queryText, () => { + pool.end(done) + release() + }) + assert.emits(client, 'query', function (query) { + assert(query instanceof pg.Query) + assert.equal(query.text, queryText) + }) + }) + ) +}) + +suite.test('Check that client query event fired before query events', function (done) { + const pool = new pg.Pool() + pool.connect( + assert.calls(function (err, client, release) { + assert(!err) + const queryText = 'SELECT 1' + client.query(queryText, () => { + pool.end(done) + release() + }) + client.once('query', query => { + assert.emits(query, 'row') + assert.emits(query, 'end') + }) + }) + ) +})