moleculer
moleculer copied to clipboard
Default actions / sub actions
Hi everyone, I am dabbing just now into Moleculer coming from Seneca, and I have to say that it is a much more modern and practical approach to the world of microservices. I have a couple of questions/suggestions though.
-
Sub-actions One of the coolest things about the Seneca framework is that it makes the single services transparent to the clients, and it does so by "hiding" them via patterns. For example, you might write a service that handles
entity: user, action: add, and then, when you want to extend for a specific use case, you just create ANOTHER service that handlesentity: user, action: add, type: admin; the broker knows that, when there is a propertytype: adminin the pattern, then the most appropriate service is the second, and it calls that one instead of the first one. Now, I understand that Moleculer uses a slightly different approach, but I believe that the chance of using this pattern would make it even more flexible to develop using pattern-based microservices. And now my point: I've seen that #69 already mentioned method/action groups. Is there a smart way to create sub/actions that may simulate the behaviour I have described above? After all, NATS and AMQP already support this use case via wildcards, and it shouldn't be hard to implement it for other transports too. -
Default actions This idea is still based on the pattern-driven development; basically, in a namespace, there may be services with a very specific function, which then wouldn't need a
service.actionpattern, simply because they may have just ONE action they respond to. Is there a way to add a "default" action to a service? So that it will respond to all requests to that service with justserviceand notservice.default(for example). E.g.
name: "test",
actions: {
default: {
params: {
value: "string"
},
handler(ctx) {
return `${ctx.params.value}`;
}
}
}
Calling it would be as easy as: broker.call("test", {value: "hello"})
This would make patterns much more readable without adding unneeded "boilerplate" code.
Thanks and congrats again for the great framework!
Daniele
Hi Daniele,
Thank you for your idea. I think we should discuss them with other Moleculer users.
By the way, I've made a pattern matching middleware for Moleculer. It's just a PoC but maybe you can try it.: https://github.com/icebob/moleculer-pattern I don't know that it works with 0.13 version, as well. But if need, I can update it.
Hi Daniele,
Thank you for your idea. I think we should discuss them with other Moleculer users.
By the way, I've made a pattern matching middleware for Moleculer. It's just a PoC but maybe you can try it.: https://github.com/icebob/moleculer-pattern I don't know that it works with 0.13 version, as well. But if need, I can update it.
Hi icebob,
Thank you for your prompt reply! I'll test that middleware and let you know. Waiting to know what the others think, too!