Moleculer Decorators
Decorators for moleculer, Tested & accurate as of 0.14
Available options
constructOverride: false
skipHandler: true
These are defined in @Service
Example usage
const moleculer = require('moleculer');
const { Service, Action, Event, Method } = require('moleculer-decorators');
const web = require('moleculer-web');
const broker = new moleculer.ServiceBroker({
logger: console,
logLevel: "debug",
});
@Service({
mixins: [web],
settings: {
port: 3000,
routes: [
...
]
}
})
class ServiceName extends moleculer.Service {
constructor() {
this.settings = {
port: 3001
}
}
settings = {
port: 3001
}
@Action()
Login(ctx) {
...
}
@Action({
skipHandler: true
})
Login3() {
}
@Action({
cache: false,
params: {
a: "number",
b: "number"
}
})
Login2(ctx) {
...
}
@Event({
group: 'group_name'
})
'event.name'(payload, sender, eventName) {
...
}
@Event()
'event.name'(payload, sender, eventName) {
...
}
@Method
authorize(ctx, route, req, res) {
...
}
started() {
...
}
created() {
...
}
stopped() {
...
}
}
broker.createService(ServiceName);
broker.start();
Usage with moleculer-runner
Simply export the service instead of starting a broker manually.
It must be a commonjs module.
module.exports = ServiceName
Usage with custom ServiceFactory class
Moleculer allows you to define your own ServiceFactory class, from which your services should inherit.
All you have to do, is pass your custom ServiceFactory to broker options and also extend your services from this class
const moleculer = require('moleculer');
const { Service, Action } = require('moleculer-decorators');
class CustomService extends moleculer.Service {
constructor(broker, schema) {
super(broker, schema)
}
foo() {
return 'bar';
}
}
const broker = new moleculer.ServiceBroker({
ServiceFactory: CustomService
});
@Service()
class ServiceName extends CustomService {
@Action()
Bar(ctx) {
return this.foo();
}
}
broker.createService(CustomService);
broker.start();
License
Moleculer Decorators is available under the MIT license.