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:
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
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(),
),
});
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.
To run the migrations, you can use the pnpm db:migrate
command.
To deploy the database, you can use the pnpm db:deploy
command.
You can utilize Nuxt's server plugins to run the migrations on the development server start.
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);
});
});
});