Skip to content

Instantly share code, notes, and snippets.

@icebob
Created December 14, 2017 13:41
Show Gist options
  • Save icebob/ec8385da7c9cea2ae9ebc72df2292d1c to your computer and use it in GitHub Desktop.
Save icebob/ec8385da7c9cea2ae9ebc72df2292d1c to your computer and use it in GitHub Desktop.
Simple event storing service for Moleculer
"use strict";
let ServiceBroker = require("../src/service-broker");
let broker = new ServiceBroker({
logger: true,
logLevel: "debug",
});
broker.createService({
name: "event-store",
events: {
"ES.**"(payload, sender, event) {
this.store({
event,
payload
}).then(() => {
this.broker.emit(event.slice(3), payload);
});
}
},
methods: {
store(event) {
this.logger.info(`STORE '${event.event}' event.`);
// Do something...
return Promise.resolve(event);
}
}
});
broker.createService({
name: "target",
events: {
"user.created"(payload) {
this.logger.info("User created event RECEIVED!", payload);
}
}
});
broker.start()
.then(() => broker.emit("ES.user.created", { name: "John" }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment