@12deg/saas-fastify
v0.21.0
Published
saas plugin for fastify
Readme
@12deg/saas-fastify
A Fastify plugin that provides an easy integration of saas.
Requirements
- @prefabs.tech/fastify-config
- @prefabs.tech/fastify-mailer
- @prefabs.tech/fastify-slonik
- @prefabs.tech/fastify-user
- slonik
- supertokens-node
Installation
Install with npm:
npm install @prefabs.tech/fastify-config @prefabs.tech/fastify-mailer @prefabs.tech/fastify-slonik @prefabs.tech/fastify-user slonik supertokens-node @12deg/saas-fastifyInstall with pnpm:
pnpm add --filter "@scope/project" @prefabs.tech/fastify-config @prefabs.tech/fastify-mailer @prefabs.tech/fastify-slonik @prefabs.tech/fastify-user slonik supertokens-node @12deg/saas-fastifyConfiguration
import { supertokensRecipesConfig } from "@12deg/saas-fastify";
const config: ApiConfig = {
...
saas: {
rootDomain: process.env.APP_ROOT_DOMAIN as string,
mainAppSubdomain: process.env.MAIN_APP_SUBDOMAIN as string || "app",
subdomains: "optional" // "disabled", "optional", "required",
},
user: {
...
supertokens: {
recipes: supertokensRecipesConfig,
}
}
...
};Usage
Register the saas plugin with your Fastify instance:
import saasPlugin, { accountMigrationPlugin } from "@12deg/saas-fastify";
import configPlugin from "@prefabs.tech/fastify-config";
import mailerPlugin from "@prefabs.tech/fastify-mailer";
import slonikPlugin, { migrationPlugin } from "@prefabs.tech/fastify-slonik";
import userPlugin from "@prefabs.tech/fastify-user";
import Fastify from "fastify";
import config from "./config";
import type { ApiConfig } from "@prefabs.tech/fastify-config";
import type { FastifyInstance } from "fastify";
const start = async () => {
// Create fastify instance
const fastify = Fastify({
logger: config.logger,
});
// Register fastify-config plugin
await fastify.register(configPlugin, { config });
// Register database plugin
await fastify.register(slonikPlugin, config.slonik);
// Register mailer plugin
await fastify.register(mailerPlugin, config.mailer);
// Register fastify-user plugin
await fastify.register(userPlugin);
// Register saas-fastify plugin
await api.register(saasPlugin);
// Run app database migrations
await fastify.register(migrationPlugin, config.slonik);
// Run accounts database migrations
await api.register(accountMigrationPlugin);
try {
await fastify.listen({
port: 3000,
host: "0.0.0.0",
});
} catch (error) {
fastify.log.error(error);
}
};
start();