Moleculer Header courtesy of MoleculerJs

Moleculer Mixins

Razvan Predescu
3 min readApr 26, 2019

--

A short introduction.

Mixins are a form of code reusability

Reusability is an important aspect of software development, and it takes different shapes, depending on the context. In this article, we will learn Moleculer’s way of reusing code through mixins.

From the official documentation:

“Mixins are a flexible way to distribute reusable functionalities for Moleculer services. The Service constructor merges these mixins with the current schema. It is to extend another service to your service. When a service uses mixins, all properties in the mixin will be “mixed” into the current service.

https://moleculer.services/docs/0.13/services.html#Mixins

Before Mixins

Before creating Mixins, you need to scaffold a basic Moleculer API as described in a previous tutorial here:

A new Hello World service

After scaffolding a Moleculer project as described above, let's create a new service which we will reuse later as an internal Mixin (as opposed to an external one that can be imported from an npm package).

Name the new service world.service.js and write a helloWorld() action that displays the famous “Hello World” message. Add the file to the same folder as the previously generated greetings.service.js.

Now you can access the World’s endpoint like this:

The Mixin

We would now include World service’s functionality within the default Greeter service with the following two lines of code:

// Import the World service
const WorldService = require("./world.service");
...
// Add the World service as a mixin
mixins: [WorldService]

Here is how the updated Greeter service looks:

Because mixins work as a kind of inheritance, we would now be able to access the World’s helloWorld endpoint from within the Greeter service itself.

This doesn’t look like adding too much value to our project because we could already access helloWorld from within World service itself, but it makes sense if you import the functionality from npm packages instead.

The advantage in using Mixins to share functionality within the same project is that Greeter service will take advantage of almost any of the World’s features like settings, dependencies, hooks, metadata, events, and methods.

Another example of Mixins value is when using moleculer-db related library for persistence and database operations. The most popular way of using moleculer-db functionality in Moleculer projects is through mixins. Check this SandBox for a live example:

Here are more details about how Mixin functionality is merging services: https://moleculer.services/docs/0.13/services.html#Merge-algorithm.

As a simple example, notice that you can now also use World’s service sayHelloWorld method within Greeter service itself anywhere you wish.

/**
* Service created lifecycle event handler
*/
created() {
// sayHelloWorld() is a World's service method which is called // from Greeter service
console.log(this.sayHelloWorld());
}

Conclusion

The Mixin functionality is a valuable way to inherit and share functionality within Moleculer projects. It is more powerful than just reusing Javascript modules because it uses a convention based merging mechanism that empowers the beautiful structure of Moleculer services.

Check out the full source code for this tutorial from Github and don’t forget to leave any kind of feedback ’cause I want to learn from you too :).

Best!

--

--