Skip to content

Multi tenancy example with Qwik.js, Prisma, PlanetScale and Fly.io

License

Notifications You must be signed in to change notification settings

peter-kuhmann/qwik-multi-tenancy

Repository files navigation

⚡️ Multi-tenancy demonstration using Qwik

The goal of this project is to demonstrate how easy it can be to create a multi-tenant web application using Qwik.js, Prisma, PlanetScale and Fly.io. 🏎️

📚 Table of contents

🍭 Blog articles and YouTube videos

3️⃣ Users and sessions (Part 3)

2️⃣ Users and sessions (Part 2)

1️⃣ Tenant resolver (Part 1)

📡 Involved technologies

🏷️ Git tags

4️⃣ final

  • Permissions
  • User management
  • Tenant management
  • "Posts" feature
  • Cache invalidations
  • Session replacement
  • New URL paths
  • New UI layout

3️⃣ users-and-sessions

🌰 In a nutshell: User sign-up, login, logout and cookie based sessions.

Details:

  • Prisma models User and Session
  • CRUD functions for User and Session
  • Cookie based sessions
  • Sign-up screen and flow
  • Login screen and flow
  • Logged in tenant home screen
  • Logout
  • Route loaders/hooks useSession and useRequiredSession
  • Sending emails
  • New .env.example
  • fly.toml replaced by fly.toml.example
  • Minor changes:
    • Tenant cache now also caches "not found" state.
    • Tenant.id renamed to Tenant.tenantId

2️⃣ tenant-resolver

🌰 In a nutshell: Providing tenant context based on subdomain.

Details:

  • Prisma client
  • DB seed script
  • Environment variables validation
  • BASE_HOSTNAME env var
  • useUrlInfo() route loader
  • useTenant() route loader
  • Three screens: Base screen + subdomain screens: Tenant not found and tenant found
  • Dockerfile that generates prisma client
  • fly.toml with env var section

1️⃣ bootstrapped

Bootstrapped project that includes:

  • Bootstrapped Qwik app
  • Prisma installed
  • Fastify adapter configured
  • Tailwind + DaisyUI installed
  • fly.toml created
  • Dockerfile created
  • Initial README.md

🌩️ Hosting

The app is meant to be hosted on Fly.io 🔗.

Therefor, I created the Dockerfile 🔗, so that Fly can build a working docker image to be used for the Fly app.

I use the Fastify adapter 🔗 to host the Qwik app as a server instance.

✅ Requirements

  • fly CLI installed
  • pnpm installed
  • Node 18+ installed
  • Docker installed

🛠️ Development

🥪 Preparation

1️⃣ Copy .env.example as .env and add all the variable values.

2️⃣ First spin up the local test database (or use PlanetScale):

./.infra/start.sh

3️⃣ Push the database schema:

pnpm prisma db push

⚙️ Start locally

Start the dev server:

pnpm start

🏗️ Build + serve via Fastify

pnpm serve:build

🛫 Fly.io deployment

🥪 Preparation

1️⃣ Copy fly.toml.example as fly.toml and add all the variable values.

Use your PlanetScale connection string for DATABASE_URL!

2️⃣ First you need to create a new Fly app via:

fly launch

3️⃣ Now, set the following two secrets via fly:

fly secrets set AUTH_FLOW_JWT_PRIVATE_KEY="...add_value..." AUTH_FLOW_JWT_PUBLIC_KEY="...add_value..."

4️⃣ Update PlanetScale schema:

pnpm prisma db push

⬆️ Deploy app

1️⃣ Deploy the application:

fly deploy

2️⃣ Configure your DNS records to point to fly: After deploying your app, you get a fly.dev URL. Create two CNAME records:

  • yourdomain.com > your-fly-url.fly.dev
  • *.yourdomain.com > your-fly-url.fly.dev

3️⃣ Allocate a dedicated IP v4 address: This is necessary, to issue an SSL certificate for the wildcard subdomains.

fly ips allocate-v4

⚠️ A dedicated v4 IP address costs 2$/month (June/2023): https://fly.io/docs/about/pricing/#anycast-ip-addresses 🔗

4️⃣ Issue certificates:

fly certs add "yourdomain.com"
fly certs add "*.yourdomain.com"

⚠️ A wildcard certificate costs 1$/month (June/2023): https://fly.io/docs/about/pricing/#managed-ssl-certificates 🔗

About

Multi tenancy example with Qwik.js, Prisma, PlanetScale and Fly.io

Resources

License

Stars

Watchers

Forks

Packages

No packages published