996a49f7da
Rework the module to use the new cls-hooked module (which uses AsyncWrap available since Node v4.5) instead of old continuation-local-storage (based on very unreliable async-listener). |
||
---|---|---|
.github | ||
browser | ||
example | ||
server | ||
test | ||
.eslintignore | ||
.eslintrc | ||
.gitignore | ||
.travis.yml | ||
CHANGES.md | ||
CONTRIBUTING.md | ||
README.md | ||
package.json |
README.md
loopback-context
Current context for LoopBack applications, based on cls-hooked.
USAGE WARNING
Only if you use this package, it's recommended to not run your app using slc run
or node .
Run using (recommended):
node -r cls-hooked .
This uses the -r
option in order to require cls-hooked
before your app (see warnings below for more info).
If you wish to use strong-supervisor
, you would need to pass node options to slc run
, which currently has issues, according to strong-supervisor#56.
A less reliable, less recommended way, which instead should be compatible with strong-supervisor
, would be to add this Javascript line at the first line of your app, and no later than that (the order of require statements matters):
require('cls-hooked')
Warning: to rely on the order of require
statements is error-prone.
INSTALL WARNING
Only if you use this package, do NOT install your app using npm install
.
Install using:
npm config set engine-strict true
npm install
This keeps you from using Node < v4.5.
WARNING
We recommend AGAINST using the loopback-context module until there is a stable solution to the issue below!
The module node-continuation-local-storage is known to have many problems, see e.g. issue #59. As a result, loopback-context does not work in many situations, as can be seen from issues reported in LoopBack's issue tracker.
The new alternative cls-hooked is known to possibly inherit these problems if it's not imported before everything else, that's why you are required to follow the advice above if using this.
Usage
- Add
per-request
middleware to yourserver/middleware-config.json
:
{
"initial": {
"loopback-context#per-request": {
}
}
}
- Then you can access the context from your code:
var LoopBackContext = require('loopback-context');
// ...
MyModel.myMethod = function(cb) {
var ctx = LoopBackContext.getCurrentContext();
ctx.get('key');
ctx.set('key', { foo: 'bar' });
});
See the official LoopBack documentation for more details.