Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Latest commit

 

History

History
81 lines (54 loc) · 1.82 KB

Redwood.md

File metadata and controls

81 lines (54 loc) · 1.82 KB

Adding multi-tenancy to your Redwood app

This document is a tutorial giving step by step instructions on how to add multi-tenancy to your Redwood application, using Prisma-multi-tenant.

Step 1: Initialize Prisma-multi-tenant

Download, then run the Prisma-multi-tenant CLI, from the api/ directory.

npm i -g prisma-multi-tenant

cd api/

prisma-multi-tenant init

After following the instructions, you will have a management database set up.

Step 2: Add the Redwood multi-tenant package

npm i @prisma-multi-tenant/redwood

Step 3: Update the db lib

Replace the code in api/src/lib/db.js with the following:

import { MultiTenant, fromContext } from '@prisma-multi-tenant/redwood'

export const multiTenant = new MultiTenant()
export const db = fromContext()

Step 4: Add the database to the context

Update the code in api/src/functions/graphql.js:

import { multiTenant } from 'src/lib/db'

export const handler = createGraphQLHandler({
  schema: makeMergedSchema({
    schemas,
    services: makeServices({ services }),
  }),
  context: async ({ event }) => ({
    // The name can come from anywhere (headers, token, ...)
    db: await multiTenant
      .get('dev') // or 'my_tenant_A' or anything
      .catch(console.error),
  }),
})

Finally, restart the server:

yarn redwood dev

And that's done! 🎉

Try adding a new tenant and play between them!

pmt new # Creates a new tenant

pmt list # List your existing tenants

# Access studio to the given tenant
pmt studio my_tenantA
pmt studio my_tenantB

If you have any issues with Prisma-multi-tenant and its Redwood plugin, don't hesitate to create an issue on Github