Skip to content

Instantly share code, notes, and snippets.

@molexx
Created August 26, 2015 17:48
Show Gist options
  • Save molexx/2f6b3b7d132b9fbf23cd to your computer and use it in GitHub Desktop.
Save molexx/2f6b3b7d132b9fbf23cd to your computer and use it in GitHub Desktop.
cls-mongoose index.js addition for Models
var wrapLastParamInCLSBind = function(clsns, container, functionName) {
shimmer.wrap(container, functionName, function (original) {
return function() {
var lastArg = arguments[arguments.length - 1];
if (lastArg && ('function' == typeof lastArg)) {
var clsBoundLastArg = clsns.bind(lastArg);
arguments[arguments.length - 1] = clsBoundLastArg;
}
return original.apply(this, arguments);
};
});
};
wrapLastParamInCLSBind(ns, mongoose.Model, 'find');
wrapLastParamInCLSBind(ns, mongoose.Model, 'update');
wrapLastParamInCLSBind(ns, mongoose.Model, 'distinct');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment