Skip to content

ducktors/fastify-moleculer

Repository files navigation

fastify-moleculer

CI Test npm Coverage Status Maintainability OpenSSF Scorecard

fastify-moleculer enables the use of Moleculer in a Fastify application.

Supports Fastify versions 4.x Supports moleculer version 0.14

Install

npm i fastify-moleculer moleculer

Usage

Require fastify-moleculer and register it as any other plugin, it will add a broker decorator.

serviceFolder & serviceMask options respect the configuration supported by moleculer ServiceBroker

const fastify = require("fastify")();

fastify.register(require("fastify-moleculer"), {
  // put your options here
  serviceFolder: './',          //load all services from the given folder
  serviceMask: '*.service.js'   //load all services whose file nane respect the given mask
});

fastify.get("/", (req, reply) => {
  fastify.broker.emit("hello", { who: 'world' });
});

fastify.listen({ port: 3000 });

For more details see examples

You can use it as is without passing any option, or you can configure it as explained by Moleculer doc.

Hooks

The plugin support a preClose hook to clean up loaded services in order to close correctly the fastify server. To configure this behaviour you can use preClose option:

await fastify.register(require('fastify-moleculer'), {
  preClose: (done) => {
    // do other things
    // fastify.broker. ;
    done();
  }
})

The plugin also adds an onClose hook which closes the socket server when the fastify instance is closed.

Typescript

The broker decorator is typed to moleculer ServiceBroker type. The plugin supports loading Typescript service classes from a given folder optionally overriding the default file mask of .service.(js|ts).

Contribute to this project

  1. Clone this repository

    git clone git@github.com:github.com/ducktors/fastify-moleculer.git

  2. Move inside repository folder

    cd fastify-moleculer

  3. Install dependencies

    pnpm install

How to release

The release is performed by the maintainers of the repository. New versions are managed via changesets.

To release a new version, simply choose which package to bump with pnpm release command:

$ pnpm release

> @ducktors/fastify-moleculer@0.9.0 release /ducktors-workstation/fastify-moleculer
> changeset

License

Licensed under MIT.
moleculer license