Database

Database setup with Cloudflare D1 and DrizzleORM that's compatible with the NuxtHub database module.

Nuxflare Pro uses Cloudflare D1 with DrizzleORM as the primary database.

Database Architecture

1. Schema Definition

The database schema is defined in schema.ts using Drizzle's SQLite dialect. The schema includes the following tables:

  • Users
  • Teams
  • Team Memberships
  • Roles
  • Permissions
  • Jobs
  • Logs
  • Subscriptions
  • Benefits
  • Plans

Nuxflare Database Hub Page Screenshot

2. Infrastructure Setup

The database infrastructure is configured in d1.ts:

export const d1 = new sst.cloudflare.D1("AppDB");

This creates a new Cloudflare D1 database using SST.dev. You can add more databases if needed.

3. Database Connection

Database connections are managed through a utility function in db.ts:

import { drizzle } from "drizzle-orm/d1";
import * as schema from "@nuxflare-pro/functions/schema";

export function useDB() {
  return drizzle(hubDatabase(), { schema });
}

We are using the hubDatabase utility from NuxtHub. Nuxflare Pro integrates with NuxtHub to support using the NuxtHub dev tools during development.

Database Usage

Here's an example of how to query the database using DrizzleORM:

const team = await useDB().query.teams.findFirst({
  where: or(
    eq(teams.id, input.teamIdentifier),
    eq(teams.slug, input.teamIdentifier),
  ),
  with: {
    memberships: {
      where: eq(teamMemberships.userId, user.id),
    },
  },
});

Database Migrations

Local Development

Migrations are configured in the Nuxt config (nuxt.config.ts) using the NuxtHub module for local development:

hooks: {
  "hub:database:migrations:dirs"(dirs) {
    dirs.push("../functions/src/database/migrations");
  }
}

Deployment

During deployment, migrations are handled using the Cloudflare D1 migration system. Migrations are automatically applied during each deployment.