Skip to content

Commit

Permalink
Intercept the module root to patch transaction
Browse files Browse the repository at this point in the history
This commit intercepts the module root to patch the `transaction`
method.  This works because the `knex` module exports a
constructor and the object that the constructor constructs has a
`client` property that is the client being used.  This client
can have its `transaction` method updated in a way to preserve
context.
  • Loading branch information
DominicKramer committed May 10, 2017
1 parent f5cca46 commit 80d3264
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/plugins/plugin-knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ function patchClient(Client, api) {
});
}

function unpatchClient(Client) {
shimmer.unwrap(Client.prototype, 'runner');
}

function patchMysqlIndex(Mysql, api) {
shimmer.wrap(Mysql.prototype, 'transaction', function(original) {
return function() {
var args = Array.prototype.slice.call(arguments).map(function(item) {
return item && is.fn(item) ? api.wrap(item) : item;
});
return original.apply(this, args);
};
});
function interceptKnex(Knex, api) {
return function() {
var result = Knex.apply(this, arguments);
var proto = Object.getPrototypeOf(result.client);
shimmer.wrap(proto, 'transaction', function(original) {
return function() {
var args = Array.prototype.slice.call(arguments).map(function(item) {
return item && is.fn(item) ? api.wrap(item) : item;
});
return original.apply(this, args);
};
});
return result;
};
}

function unpatchMysqlIndex(Mysql) {
shimmer.unwrap(Mysql.prototype, 'transaction');
function unpatchClient(Client) {
shimmer.unwrap(Client.prototype, 'runner');
}

module.exports = [
{
file: '',
versions: VERSIONS,
intercept: interceptKnex
},
{
file: 'lib/client.js',
versions: VERSIONS,
patch: patchClient,
unpatch: unpatchClient
},
{
file: 'lib/dialects/mysql/index.js',
version: VERSIONS,
patch: patchMysqlIndex,
unpatch: unpatchMysqlIndex
}
];

0 comments on commit 80d3264

Please sign in to comment.