Skip to content

ducktors/fastify-moleculer

Repository files navigation

fastify-moleculer

Build Test npm Coverage Status Maintainability OpenSSF Scorecard

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

Features

  • Full TypeScript support
  • Compatible with Fastify 4.x
  • Compatible with Moleculer 0.14
  • Automatic service discovery and loading
  • Built-in cleanup hooks
  • Zero configuration required

Installation

# Using npm
npm install fastify-moleculer moleculer

# Using pnpm
pnpm add fastify-moleculer moleculer

# Using yarn
yarn add fastify-moleculer moleculer

Configuration

Plugin Options

Option Type Default Description
serviceFolder string undefined Directory to load services from
serviceMask string *.service.(js|ts) File pattern for service discovery
preClose (done: () => void) => void undefined Hook called before broker shutdown

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