@12degrees/stateful-fastify
v0.3.1
Published
A fastify plugin for the stateful library
Downloads
778
Keywords
Readme
@12degrees/stateful/fastify
A fastify plugin for implementing a state machine.
Requirements
A fastify server.
Installation
Install with npm:
npm install @12degrees/stateful-fastify
Install with pnpm:
pnpm add --filter "@scope/project" @12degrees/stateful-fastify
Usage
Register the stateful plugin in your fastify server instace
Register the plugin with your fastify instance:
import plugin from "@12degrees/stateful-fastify";
import Fastify from "fastify";
const start = async () => {
const fastify = await Fastify();
// Register state-machine-fastify plugin
await fastify.register(plugin, { ... });
try {
await fastify.listen({
port: 3000,
host: "0.0.0.0",
});
} catch (error) {
fastify.log.error(error);
}
};
start();
Register the stateful plugin in your entity controller
# ThingController.ts
import statefulPlugin from "@12degrees/stateful-fastify";
...
const plugin = async (
fastify: FastifyInstance,
options: Records<string, any>
) => {
...
fastify.get("/things, {
...
});
fastify.post("/things/:id", {
...
});
// Register stateful plugin
await fastify.register(
StatefulPlugin,
{
prefix: ...,
entity: {
dropTables: true,
field: "status",
name: "thing",
table: "things",
users: "users",
},
}
);
}
Options
The Stateful plugin suports the standard Fastify plugin options:
logLevel
logSerializers
prefix
An additional entity: StatefulPluginOptions
option is requred:
| Option | Type | Required | Default | Description |
|------------|---------|----------|----------|-------------|
| dropTables | boolean | no | false | Whether to drop all generated tables before running migrations. |
| field | string | yes | status
| The name of the attribute representing the state in the entity concerned. |
| name | string | yes | | The name of the entity for which to create a state machine. Will be used as the prefix of all generated tables. |
| table | string | yes | | The name of the table for the entities in question. |
| users | string | no | users
| The name of the table containing user records. |