mirror of
https://github.com/knex/knex.git
synced 2025-12-16 09:38:25 +00:00
Post processing hook for query result (#2261)
This commit is contained in:
parent
0ccd591bcb
commit
9658c78d4f
@ -163,6 +163,13 @@ assign(Client.prototype, {
|
|||||||
return sql;
|
return sql;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
postProcessResponse(resp) {
|
||||||
|
if (this.config.postProcessResponse) {
|
||||||
|
return this.config.postProcessResponse(resp);
|
||||||
|
}
|
||||||
|
return resp;
|
||||||
|
},
|
||||||
|
|
||||||
wrapIdentifier(value) {
|
wrapIdentifier(value) {
|
||||||
if (this.config.wrapIdentifier) {
|
if (this.config.wrapIdentifier) {
|
||||||
return this.config.wrapIdentifier(value, this.wrapIdentifierImpl);
|
return this.config.wrapIdentifier(value, this.wrapIdentifierImpl);
|
||||||
|
|||||||
@ -132,19 +132,24 @@ assign(Runner.prototype, {
|
|||||||
return queryPromise
|
return queryPromise
|
||||||
.then((resp) => {
|
.then((resp) => {
|
||||||
const processedResponse = this.client.processResponse(resp, runner);
|
const processedResponse = this.client.processResponse(resp, runner);
|
||||||
|
const postProcessedResponse = this.client
|
||||||
|
.postProcessResponse(processedResponse);
|
||||||
|
|
||||||
this.builder.emit(
|
this.builder.emit(
|
||||||
'query-response',
|
'query-response',
|
||||||
processedResponse,
|
postProcessedResponse,
|
||||||
assign({__knexUid: this.connection.__knexUid}, obj),
|
assign({__knexUid: this.connection.__knexUid}, obj),
|
||||||
this.builder
|
this.builder
|
||||||
);
|
);
|
||||||
|
|
||||||
this.client.emit(
|
this.client.emit(
|
||||||
'query-response',
|
'query-response',
|
||||||
processedResponse,
|
postProcessedResponse,
|
||||||
assign({__knexUid: this.connection.__knexUid}, obj),
|
assign({__knexUid: this.connection.__knexUid}, obj),
|
||||||
this.builder
|
this.builder
|
||||||
);
|
);
|
||||||
return processedResponse;
|
|
||||||
|
return postProcessedResponse;
|
||||||
}).catch(Promise.TimeoutError, error => {
|
}).catch(Promise.TimeoutError, error => {
|
||||||
const { timeout, sql, bindings } = obj;
|
const { timeout, sql, bindings } = obj;
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,42 @@ module.exports = function(knex) {
|
|||||||
|
|
||||||
describe('Additional', function () {
|
describe('Additional', function () {
|
||||||
|
|
||||||
|
describe("Custom response processing", () => {
|
||||||
|
|
||||||
|
before('setup custom response handler', () => {
|
||||||
|
knex.client.config.postProcessResponse = (response) => {
|
||||||
|
response.callCount = response.callCount ? (response.callCount + 1) : 1;
|
||||||
|
return response;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
after('restore client configuration', () => {
|
||||||
|
knex.client.config.postProcessResponse = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should process normal response', () => {
|
||||||
|
return knex('accounts').limit(1).then(res => {
|
||||||
|
expect(res.callCount).to.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should process raw response', () => {
|
||||||
|
return knex.raw('select * from ??', ['accounts']).then(res => {
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should process response done in transaction', () => {
|
||||||
|
return knex.transaction(trx => {
|
||||||
|
return trx('accounts').limit(1).then(res => {
|
||||||
|
expect(res.callCount).to.equal(1);
|
||||||
|
return res;
|
||||||
|
});
|
||||||
|
}).then(res => {
|
||||||
|
expect(res.callCount).to.equal(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should forward the .get() function from bluebird', function() {
|
it('should forward the .get() function from bluebird', function() {
|
||||||
return knex('accounts').select().limit(1).then(function(accounts){
|
return knex('accounts').select().limit(1).then(function(accounts){
|
||||||
var firstAccount = accounts[0];
|
var firstAccount = accounts[0];
|
||||||
@ -464,6 +500,7 @@ module.exports = function(knex) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
|
knex.removeListener('query-response', onQueryResponse);
|
||||||
expect(queryCount).to.equal(4);
|
expect(queryCount).to.equal(4);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@ -487,6 +524,7 @@ module.exports = function(knex) {
|
|||||||
expect(true).to.equal(false); //Should not be resolved
|
expect(true).to.equal(false); //Should not be resolved
|
||||||
})
|
})
|
||||||
.catch(function() {
|
.catch(function() {
|
||||||
|
knex.removeListener('query-error', onQueryError);
|
||||||
expect(queryCount).to.equal(2);
|
expect(queryCount).to.equal(2);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user