moleculer-cls
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Async Local Storage for Moleculer

Add support for async local storage in moleculer action handlers.

Current Version (v2.1.0)

Install

npm install --save moleculer-cls

Usage

// moleculer.config.js

const context = require('moleculer-cls');

module.exports = {
  ...
  middlewares: [
    context.middleware
  ]
}
// service.js

const context = require('moleculer-cls');
const process = require('processor.js');

module.exports = {
  actions: {
    hello: {
      params: {
        "name": "string"
      },
      handler: async (ctx) => {
        context.set('request_id', ctx.requestID);

        return await process(ctx.params);
      }
    }
  }
}
// processor.js

const context = require('moleculer-cls');

module.exports = async ({name}) => {
  console.log(`Processing ${context.get('request_id')}`);

  return Promise.resolve(`Hello ${name}`);
}

License

MIT

Package Sidebar

Install

npm i moleculer-cls

Weekly Downloads

46

Version

2.1.0

License

MIT

Unpacked Size

3.73 kB

Total Files

6

Last publish

Collaborators

  • cupsadarius