Current context for LoopBack applications, based on node-continuation-local-storage
Go to file
josieusa 1047af4e76 Document in README.md and test correct usage of cls-hooked 2016-09-09 14:37:24 +02:00
browser Merge upstream/master into master to sync with upstream repo 2016-09-09 14:27:29 +02:00
server Use cls-hooked instead of continuation-local-storage, 2016-09-09 14:37:24 +02:00
test Document in README.md and test correct usage of cls-hooked 2016-09-09 14:37:24 +02: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 Enable Travis CI 2016-08-02 14:41:24 +02:00
CHANGES.md Document in README.md and test correct usage of cls-hooked 2016-09-09 14:37:24 +02:00
CONTRIBUTING.md Initial commit 2016-07-28 15:11:12 +02:00
README.md Document in README.md and test correct usage of cls-hooked 2016-09-09 14:37:24 +02:00
package.json Document in README.md and test correct usage of cls-hooked 2016-09-09 14:37:24 +02:00

README.md

loopback-context

Current context for LoopBack applications, based on cls-hooked.

USAGE WARNING

Only if you use this package, do NOT run your app using slc run or node .

Run using:

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.

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 < v0.4.5.

TEST WARNING

Do NOT test this package using mocha ..

Test using:

npm test

This adds the -r option to mocha command, needed in order to pass tests.

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.