Add support for async local storage in moleculer action handlers.
- support moleculer ^0.14.x
- accept nodejs: >=18
npm install --save moleculer-cls
// 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}`);
}
MIT