
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
moleculer-decorators-extra
Advanced tools
Decorators for moleculer. Fork of moleculer-decorators@1.3.0
Fork of https://github.com/ColonelBundy/moleculer-decorators with @CronJob
decorator.
Works with Cron
mixin class taken form https://www.npmjs.com/package/moleculer-cron.
Example usage:
import { Action, CronJob, Service } from 'moleculer-decorators-extra';
const Cron = require('moleculer-cron');
@Service({
...opts,
mixins: [Cron]
})
export class MyService extends moleculer.Service {
@CronJob({
cronTime: '* * * * * *'
// The same options as for `moleculer-cron`
})
async theJob() {
console.dir('I am the job that runs every second');
}
}
Decorators for moleculer, Tested & accurate as of 0.14
constructOverride: false // True by default, This will override any properties defined in @Service if defined in the constructor as well.
skipHandler: true // false by default, this will let a mixin override the handler in an action. (action options)
These are defined in @Service
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 {
// Optional constructor
constructor() {
this.settings = { // Overrides above by default, to prevent this, add "constructOverride: false" to @Service
port: 3001
}
}
// Without constructor (typescript)
settings = {
port: 3001
}
@Action()
Login(ctx) {
...
}
@Action({
skipHandler: true // Any options will be merged with the mixin's action.
})
Login3() { // this function will never be called since a mixin will override it, unless you specify skipHandler: false.
}
// With options
// No need for "handler:{}" here
@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() { // Reserved for moleculer, fired when started
...
}
created() { // Reserved for moleculer, fired when created
...
}
stopped() { // Reserved for moleculer, fired when stopped
...
}
}
broker.createService(ServiceName);
broker.start();
Simply export the service instead of starting a broker manually.
It must be a commonjs module.
module.exports = ServiceName
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');
// create new service factory, inheriting from moleculer native Service
class CustomService extends moleculer.Service {
constructor(broker, schema) {
super(broker, schema)
}
foo() {
return 'bar';
}
}
// pass your custom service factory to broker options
const broker = new moleculer.ServiceBroker({
ServiceFactory: CustomService
});
@Service()
class ServiceName extends CustomService { // extend your service from your custom service factory
@Action()
Bar(ctx) {
return this.foo();
}
}
broker.createService(CustomService);
broker.start();
Moleculer Decorators is available under the MIT license.
FAQs
Decorators for moleculer. Fork of moleculer-decorators@1.3.0
We found that moleculer-decorators-extra demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.