moleculerjs / moleculer

:rocket: Progressive microservices framework for Node.js
https://moleculer.services/
MIT License
6.16k stars 586 forks source link

Consider JS-based REPL (instead of shell-like) #252

Closed futpib closed 6 years ago

futpib commented 6 years ago

I often find myself running both moleculer REPL and a node one on two separate terminals, this makes for a really productive development environment in my opinion. But this leaves no (easy) way to transfer data back and forth.

What if instead moleculer REPL was built on node.js repl module with an extra exported global (or multiple globals)? Let's assume one global variable M for example.

Now

Call an action

call greeter.welcome --name John
# or
call greeter.welcome '{"name": "John"}'

Work with action result

Not available.

JS alternative

This will get better when node repl gets await support.

Call an action

M.call('greeter.welcome', { name: 'John' })

Work with action result

_.then(r => r.toUpperCase()) // `_` is an alias for previous expression value
icebob commented 6 years ago

Good idea. I tried and you can use it with +1 extra line:

const { ServiceBroker } = require("moleculer");

const broker = new ServiceBroker({ logger: true });
broker.createService({
    name: "greeter",
    actions: {
        hello(ctx) {
            return "Hello!";
        },
    }
});

broker.start().then(() => require("repl").start("mol $ ").context.broker = broker);

Output:

mol $ broker.call("greeter.hello").then(console.log)
mol $ Hello!