mirror of
https://github.com/knex/knex.git
synced 2025-12-27 23:18:41 +00:00
Add 'query-response' event. Fixes #1203
This commit is contained in:
parent
a42766afb1
commit
840d2d1299
@ -120,8 +120,10 @@ assign(Runner.prototype, {
|
||||
}
|
||||
|
||||
return queryPromise
|
||||
.then(function(resp) {
|
||||
return runner.client.processResponse(resp, runner)
|
||||
.then((resp) => {
|
||||
var processedResponse = this.client.processResponse(resp, runner);
|
||||
this.client.emit('query-response', processedResponse, assign({__knexUid: this.connection.__knexUid}, obj), this.builder)
|
||||
return processedResponse;
|
||||
}).catch(Promise.TimeoutError, error => {
|
||||
throw assign(error, {
|
||||
message: `Defined query timeout of ${obj.timeout}ms exceeded when running query.`,
|
||||
|
||||
@ -219,6 +219,11 @@ function makeTxClient(trx, client, connection) {
|
||||
client.emit('query-error', err, obj)
|
||||
})
|
||||
|
||||
trxClient.on('query-response', function(response, obj, builder) {
|
||||
trx.emit('query-response', response, obj, builder)
|
||||
client.emit('query-response', response, obj, builder)
|
||||
})
|
||||
|
||||
var _query = trxClient.query;
|
||||
trxClient.query = function(conn, obj) {
|
||||
var completed = trx.isCompleted()
|
||||
|
||||
@ -132,6 +132,10 @@ module.exports = function makeKnex(client) {
|
||||
knex.emit('query-error', err, obj)
|
||||
})
|
||||
|
||||
client.on('query-response', function(response, obj, builder) {
|
||||
knex.emit('query-response', response, obj, builder)
|
||||
})
|
||||
|
||||
client.makeKnex = function(client) {
|
||||
return makeKnex(client)
|
||||
}
|
||||
|
||||
@ -289,6 +289,25 @@ module.exports = function(knex) {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('Event: query-response', function() {
|
||||
var queryCount = 0;
|
||||
|
||||
knex.on('query-response', function(response, obj, builder) {
|
||||
queryCount++;
|
||||
});
|
||||
|
||||
return knex('accounts').select()
|
||||
.then(function() {
|
||||
return knex.transaction(function(tr) {
|
||||
return tr('accounts').select(); //Transactions should emit the event as well
|
||||
})
|
||||
})
|
||||
.then(function() {
|
||||
expect(queryCount).to.equal(2);
|
||||
})
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user