New release is coming soon! If you want to try out the latest features, simply run npm i -s moleculer@next. The docs for the latest version are available here.
// It uses the global 3000 timeout await broker.call("greeter.normal"); // It uses the 5000 timeout from action definition await broker.call("greeter.slow"); // It uses 1000 timeout from calling option await broker.call("greeter.slow", null, { timeout: 1000 });
多个调用
同时调用多个动作也是可能的。 要做到这一点,请使用 broker.mcall 或 ctx.mcall。 mcall with Array <Object\>
await broker.mcall( [ { action: 'posts.find', params: { author: 1 }, options: { /* Calling options for this call. */} }, { action: 'users.find', params: { name: 'John' } } ], { // Common calling options for all calls. meta: { token: '63f20c2d-8902-4d86-ad87-b58c9e2333c2' } } );
mcall with Object and options.meta
await broker.mcall( { posts: { action: 'posts.find', params: { author: 1 }, options: { /* Calling options for this call. */} }, users: { action: 'users.find', params: { name: 'John' } } }, { // Common calling options for all calls. meta: { token: '63f20c2d-8902-4d86-ad87-b58c9e2333c2' } } );
settled option in broker.mcall
The mcall method has a new settled option to receive all Promise results. If settled: true, the mcall returns a resolved Promise in any case and the response contains the statuses and responses of all calls. Note that, without this option you won’t know how many (and which) calls were rejected.
module.exports = { name: "storage", actions: { save(ctx) { // Save the received stream to a file const s = fs.createWriteStream(`/tmp/${ctx.meta.filename}`); ctx.params.pipe(s); } } };
Hooks can be assigned to a specific action (by indicating action name), all actions (*) in service or by indicating a wildcard (e.g., create-*). The latter will be applied to all actions whose name starts with create-. Action names can also be combined using a pipe symbol (e.g., create|update)
module.exports = { name: "posts", mixins: [DbService] hooks: { before: { // Define a global hook for all actions // The hook will call the `resolveLoggedUser` method. "*": "resolveLoggedUser",
// Define multiple hooks for action `remove` remove: [ functionisAuthenticated(ctx) { if (!ctx.user) thrownewError("Forbidden"); }, functionisOwner(ctx) { if (!this.checkOwner(ctx.params.id, ctx.user.id)) thrownewError("Only owner can remove it."); } ], // Applies to all actions that start with "create-" "create-*": [ asyncfunction (ctx){} ], // Applies to all actions that end with "-user" "*-user": [ asyncfunction (ctx){} ], // Applies to all actions that start with "create-" or end with "-user" "create-*|*-user": [ asyncfunction (ctx){} ], } },
module.exports = { name: "users", mixins: [DbService] hooks: { after: { // Define a global hook for all actions to remove sensitive data "*": function(ctx, res) { // Remove password delete res.password;
// Please note, must return result (either the original or a new) return res; }, get: [ // Add a new virtual field to the entity async function (ctx, res) { res.friends = await ctx.call("friends.count", { query: { follower: res._id }});
return res; }, // Populate the `referrer` field asyncfunction (ctx, res) { if (res.referrer) res.referrer = await ctx.call("users.get", { id: res._id });
return res; } ], // Applies to all actions that start with "create-" "create-*": [ asyncfunction (ctx, res){} ], // Applies to all actions that end with "-user" "*-user": [ asyncfunction (ctx, res){} ], }, error: { // Global error handler "*": function(ctx, err) { this.logger.error(`Error occurred when '${ctx.action.name}' action was called`, err);
// Throw further the error throw err; }, // Applies to all actions that start with "create-" "create-*": [ asyncfunction (ctx, err){} ], // Applies to all actions that end with "-user" "*-user": [ asyncfunction (ctx, err){} ], } } };
before hooks: global (*) -> service level -> action level.
after hooks: action level -> service level -> global (*).
When using several hooks it might be difficult visualize their execution order. However, you can set the logLevel to debug to quickly check the execution order of global and service level hooks.
INFO - Before all hook INFO - Before hook INFO - Before action hook INFO - Action handler INFO - After action hook INFO - After hook INFO - After all hook