fastify-moleculer
enables the use of Moleculer in a Fastify application.
- 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
# Using npm
npm install fastify-moleculer moleculer
# Using pnpm
pnpm add fastify-moleculer moleculer
# Using yarn
yarn add fastify-moleculer moleculer
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 |
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.
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.
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)
.
-
Clone this repository
git clone git@github.com:github.com/ducktors/fastify-moleculer.git
-
Move inside repository folder
cd fastify-moleculer
-
Install dependencies
pnpm install
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
Licensed under MIT.
moleculer
license