0.75.0 • Published 2 months ago

@dzangolab/fastify-multi-tenant v0.75.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@dzangolab/fastify-multi-tenant

A Fastify plugin that adds support for multi-tenant architecture in your API.

Requirements

Tenants table

You need to create a table containing the tenants. You can name the table as you wish. The default name is tenants.

The table should contain the following columns:

PurposeTypeConstraintsDefault column name
Identifierinteger \| varchar(255) \| uuidPKid
Display namevarchar(255)NOT NULLname
Owner IDvarchar(36)owner_id
Slugvarchar(63)NOT NULL UNIQUEslug
Domainvarchar(255)UNIQUEdomain
created_atTIMESTAMPDEFAULT NOW() NOT NULLcreated_at
updated_atTIMESTAMPDEFAULT NOW() NOT NULLupdated_at

The owner_id column serves as a foreign key referencing the id column in the users table.

Installation

Install with npm:

npm install @dzangolab/fastify-config @dzangolab/fastify-mailer @dzangolab/fastify-slonik @dzangolab/fastify-multi-tenant @dzangolab/fastify-user

Install with pnpm:

pnpm add --filter "@scope/project" @dzangolab/fastify-config @dzangolab/fastify-mailer @dzangolab/fastify-slonik @dzangolab/fastify-multi-tenant @dzangolab/fastify-user

Usage

Register the fastify plugin

Register the plugin with your Fastify instance:

import configPlugin from "@dzangolab/fastify-config";
import mailerPlugin from "@dzangolab/fastify-mailer";
import multiTenantPlugin, { tenantMigrationPlugin } from "@dzangolab/fastify-multi-tenant"
import slonikPlugin, { migrationPlugin } from "@dzangolab/fastify-slonik"
import userPlugin from "@dzangolab/fastify-user";
import Fastify from "fastify";

import config from "./config";

import type { ApiConfig } from "@dzangolab/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 mailer plugin
  await fastify.register(mailerPlugin, config.mailer);
  
  // Register database plugin
  await fastify.register(slonikPlugin, config.slonik);
  
  // Register multi tenant plugin
  await fastify.register(multiTenantPlugin);
  
  // Register user plugin
  await fastify.register(userPlugin);
  
  // Run app database migrations
  await fastify.register(migrationPlugin, config.slonik);
  
  // Run tenant database migrations
  await fastify.register(tenantMigrationPlugin);

  await fastify.listen({
    port: config.port,
    host: "0.0.0.0",
  });
};

start();

Configuration

If you are not using the default table name and columns, add the following configuration to your config:

const config: ApiConfig = {
  // ...
  multiTenant: {
    reserved: {
      slugs: ["..."],
      domains: ["..."],
    },
    rootDomain: "";
    table: {
      columns: {
        id: "...",
        domain: "...",
        name: "...",
        ownerId: "...",
        slug: "...",
      },
      name: "...",
    },
  }
};

Using GraphQL

This package supports integration with @dzangolab/fastify-graphql.

Configuration

Add the required context for the fastify-user package by including multiTenantPlugin in your GraphQL configuration as shown below:

import multiTenantPlugin from "@dzangolab/fastify-multi-tenant";
import userPlugin from "@dzangolab/fastify-user";
import type { ApiConfig } from "@dzangolab/fastify-config";

const config: ApiConfig = {
  // ...other configurations...
  graphql: {
    // ...other graphql configurations...
    plugins: [userPlugin, multiTenantPlugin],
  },
  // ...other configurations...
};

Schema Integration

This package does not provide a predefined schema. To integrate it into your GraphQL setup, add the following to your GraphQL schema:

type Tenant {
  id: Int!
  name: String
  slug: String!
  domain: String
  ownerId: String
  createdAt: Float!
  updatedAt: Float!
}

type Tenants {
  totalCount: Int
  filteredCount: Int
  data: [Tenant]!
}

input TenantCreateInput {
  name: String
  slug: String!
  domain: String
}

type Mutation {
  createTenant(data: TenantCreateInput): Tenant @auth
}

type Query {
  allTenants(fields: [String]): [Tenant]! @auth
  tenant(id: Int): Tenant @auth
  tenants(limit: Int, offset: Int, filters: Filters, sort: [SortInput]): Tenants! @auth
}

Resolver Integration

To integrate the resolvers provided by this package, import them and merge with your application's resolvers:

import { tenantResolver } from "@dzangolab/fastify-multi-tenant";

import type { IResolvers } from "mercurius";

const resolvers: IResolvers = {
  Mutation: {
    // ...other mutations ...
    ...tenantResolver.Mutation,
  },
  Query: {
    // ...other queries ...
    ...tenantResolver.Query,
  },
};

export default resolvers;
0.75.0

2 months ago

0.74.1

2 months ago

0.74.0

3 months ago

0.70.0

5 months ago

0.67.0

7 months ago

0.67.2

7 months ago

0.67.1

7 months ago

0.71.3

4 months ago

0.71.2

4 months ago

0.71.1

4 months ago

0.71.0

5 months ago

0.68.3

6 months ago

0.68.2

7 months ago

0.68.1

7 months ago

0.68.0

7 months ago

0.72.1

3 months ago

0.72.0

3 months ago

0.69.0

6 months ago

0.73.1

3 months ago

0.73.0

3 months ago

0.66.0

7 months ago

0.65.5

7 months ago

0.65.4

7 months ago

0.65.3

8 months ago

0.65.2

8 months ago

0.65.1

8 months ago

0.65.0

8 months ago

0.64.2

9 months ago

0.64.1

9 months ago

0.64.0

9 months ago

0.63.0

9 months ago

0.62.4

9 months ago

0.62.3

9 months ago

0.62.2

10 months ago

0.62.1

10 months ago

0.62.0

10 months ago

0.61.1

10 months ago

0.61.0

11 months ago

0.60.0

11 months ago

0.59.0

11 months ago

0.58.0

11 months ago

0.57.1

12 months ago

0.57.0

12 months ago

0.56.0

12 months ago

0.55.2

1 year ago

0.55.1

1 year ago

0.55.0

1 year ago

0.54.0

1 year ago

0.53.4

1 year ago

0.53.3

1 year ago

0.43.0

1 year ago

0.41.0

1 year ago

0.38.0

1 year ago

0.36.2

1 year ago

0.36.1

1 year ago

0.36.0

1 year ago

0.53.2

1 year ago

0.34.0

1 year ago

0.53.0

1 year ago

0.53.1

1 year ago

0.51.0

1 year ago

0.51.1

1 year ago

0.48.0

1 year ago

0.48.1

1 year ago

0.46.0

1 year ago

0.44.0

1 year ago

0.42.0

1 year ago

0.40.2

1 year ago

0.40.0

1 year ago

0.40.1

1 year ago

0.39.1

1 year ago

0.39.0

1 year ago

0.37.1

1 year ago

0.37.0

1 year ago

0.35.0

1 year ago

0.52.1

1 year ago

0.50.1

1 year ago

0.52.0

1 year ago

0.50.0

1 year ago

0.49.0

1 year ago

0.47.0

1 year ago

0.45.0

1 year ago

0.33.0

1 year ago

0.20.0

2 years ago

0.32.8

2 years ago

0.19.0

2 years ago

0.32.7

2 years ago

0.32.6

2 years ago

0.32.5

2 years ago

0.32.4

2 years ago

0.32.3

2 years ago

0.32.2

2 years ago

0.32.1

2 years ago

0.32.9

2 years ago

0.32.0

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.27.1

2 years ago

0.25.3

2 years ago

0.27.0

2 years ago

0.25.2

2 years ago

0.25.1

2 years ago

0.25.0

2 years ago

0.23.0

2 years ago

0.32.10

2 years ago

0.21.0

2 years ago

0.18.1

2 years ago

0.18.2

2 years ago

0.18.3

2 years ago

0.31.3

2 years ago

0.31.2

2 years ago

0.18.0

2 years ago

0.31.1

2 years ago

0.31.0

2 years ago

0.26.3

2 years ago

0.28.0

2 years ago

0.26.2

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.24.0

2 years ago

0.22.1

2 years ago

0.22.0

2 years ago

0.17.1

2 years ago

0.17.0

2 years ago

0.16.0

2 years ago

0.15.2

2 years ago

0.15.1

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.14.0

2 years ago

0.13.0

2 years ago