Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ducktors/fastify-moleculer

Fastify plugin for moleculerjs
https://github.com/ducktors/fastify-moleculer

fastify fastify-plugin moleculer moleculerjs

Last synced: 16 days ago
JSON representation

Fastify plugin for moleculerjs

Awesome Lists containing this project

README

        

# fastify-moleculer

[![Build](https://github.com/ducktors/fastify-moleculer/actions/workflows/build.yaml/badge.svg)](https://github.com/ducktors/fastify-moleculer/actions/workflows/build.yaml) [![Test](https://github.com/ducktors/fastify-moleculer/actions/workflows/test.yaml/badge.svg)](https://github.com/ducktors/fastify-moleculer/actions/workflows/test.yaml) [![npm](https://img.shields.io/npm/v/fastify-moleculer)](https://www.npmjs.com/package/fastify-moleculer) [![Coverage Status](https://coveralls.io/repos/github/ducktors/fastify-moleculer/badge.svg?branch=main)](https://coveralls.io/github/ducktors/fastify-moleculer?branch=main) [![Maintainability](https://api.codeclimate.com/v1/badges/8415332abe3ff865131d/maintainability)](https://codeclimate.com/github/ducktors/fastify-moleculer/maintainability) [![OpenSSF Scorecard](https://api.securityscorecards.dev/projects/github.com/ducktors/fastify-moleculer/badge)](https://securityscorecards.dev/viewer/?uri=github.com/ducktors/fastify-moleculer)

`fastify-moleculer` enables the use of [Moleculer](https://moleculer.services/) 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

```bash
# 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](https://moleculer.services/docs/0.14/broker)

```js
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](https://github.com/ducktors/fastify-moleculer/tree/master/examples)

You can use it as is without passing any option, or you can configure it as explained by Moleculer [doc](https://moleculer.services/docs/0.14/configuration).

### 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:

```javascript
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](https://moleculer.services/docs/0.14/broker) 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 [email protected]: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](https://github.com/changesets/changesets).

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

```
$ pnpm release

> @ducktors/[email protected] release /ducktors-workstation/fastify-moleculer
> changeset

```

## License

Licensed under [MIT](./LICENSE).

[`moleculer` license](https://github.com/moleculerjs/moleculer/blob/master/LICENSE)