Schema

Using the database schema in your project

The database schema is defined in server/database/schema.ts.

By default Supersaas comes with tables for users, credentials, OAuth accounts, images, subscriptions, email verification, password reset, one-time passwords, posts, and waitlist.

Here's an example of how tables are defined:

server/database/schema.ts
import {
  sqliteTable,
  text,
  integer,
  uniqueIndex,
  blob,
} from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";

export const users = sqliteTable("users", {
  id: text("id")
    .primaryKey()
    .$default(() => createId()),
  email: text("email").notNull().unique(),
  emailVerified: integer("emailVerified", { mode: "boolean" })
    .notNull()
    .default(false),
  role: text("role").notNull().default(UserRole.USER),
  name: text("name").notNull(),
  avatarUrl: text("avatarUrl"),
  hashedPassword: text("hashedPassword"),
  // ... other fields
});

// ... other table definitions

Example

index.ts
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";

export const posts = sqliteTable("posts", {
  id: text("id")
    .primaryKey()
    .$default(() => createId()),
  userId: text("userId")
    .notNull()
    .references(() => users.id, { onDelete: "cascade" }),
  title: text("title").notNull(),
  content: text("content").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).$default(
    () => new Date(),
  ),
  updatedAt: integer("updated_at", { mode: "timestamp" }).$onUpdate(
    () => new Date(),
  ),
});

Database Migrations

Migrations are used to update the database schema. You can find the migrations in server/database/migrations. You don't need to touch this, unless you want to change the database schema. Drizzle ORM will automatically create the migrations for you.

Creating Migrations

To run the migrations, you can use the pnpm db:migrate command.

Deploying Migrations

To deploy the database, you can use the pnpm db:deploy command.

Auto run migrations

You can utilize Nuxt's server plugins to run the migrations on the development server start.

index.ts
import { consola } from "consola";
import { migrate } from "drizzle-orm/d1/migrator";

export default defineNitroPlugin(async () => {
  if (!import.meta.dev) return;

  nuxtApp.hook("ready", async () => {
    await migrate(useDB(), { migrationsFolder: "server/database/migrations" })
      .then(() => {
        consola.success("Database migrations done");
      })
      .catch((err) => {
        consola.error("Database migrations failed", err);
      });
  });
});