Current context for LoopBack applications, based on node-continuation-local-storage
Go to file
josieusa 996a49f7da Drop continuation-local-storage, use cls-hooked
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).
2017-01-03 13:12:28 +01:00
.github Update paid support URL 2016-12-06 03:08:19 -08:00
browser Rename to loopback-context/LoopBackContext 2016-07-29 09:36:34 +02:00
example Add app using loopback-context 2016-11-25 14:28:01 -05:00
server Drop continuation-local-storage, use cls-hooked 2017-01-03 13:12:28 +01:00
test Drop continuation-local-storage, use cls-hooked 2017-01-03 13:12:28 +01:00
.eslintignore Initial commit 2016-07-28 15:11:12 +02:00
.eslintrc Initial commit 2016-07-28 15:11:12 +02:00
.gitignore Initial commit 2016-07-28 15:11:12 +02:00
.travis.yml Drop continuation-local-storage, use cls-hooked 2017-01-03 13:12:28 +01:00
CHANGES.md 1.0.0 2016-08-10 10:46:08 +02:00
CONTRIBUTING.md Initial commit 2016-07-28 15:11:12 +02:00
README.md Drop continuation-local-storage, use cls-hooked 2017-01-03 13:12:28 +01:00
package.json Drop continuation-local-storage, use cls-hooked 2017-01-03 13:12:28 +01:00

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

  1. Add per-request middleware to your server/middleware-config.json:
{
  "initial": {
    "loopback-context#per-request": {
    }
  }
}
  1. 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.