Skip to content

'query' event at Client #2022

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions lib/native/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions test/integration/client/api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
)
})