Schema Generation
Set up auto schema generation for @nuxtjs/better-auth with NuxtHub.
- Requires NuxtHub integration — the module generates Drizzle ORM schemas at build time
- Supports sqlite (default), postgresql, and mysql dialects via `hub.db.dialect` in `nuxt.config.ts`
- The module reads `server/auth.config.ts` plugins to determine required tables (user, session, account, verification + plugin tables)
- After adding/removing plugins: restart the dev server to regenerate schemas
- For production (Cloudflare D1): run `npx nuxt db migrate`
- No manual Drizzle schema writing needed for auth tables
Use this page when you are using NuxtHub and want the module to generate auth tables from your Better Auth configuration.
Better Auth requires tables for users, sessions, accounts. This module creates them automatically with NuxtHub and Drizzle ORM.
How it works
The module analyzes your server/auth.config.ts at build time to determine which tables are required by your core configuration and enabled plugins (e.g., twoFactor, passkey).
- Analysis: Reads
pluginsfrom your config. - Generation: Generates a Drizzle schema file corresponding to your database dialect (SQLite, PostgreSQL, MySQL).
- Integration: Automatically injects this schema into NuxtHub's database configuration.
Importing generated tables
Import generated tables through #auth/schema in server code. The exports retain their Drizzle types, including configured additional fields and plugin tables.
import { user } from '#auth/schema'
type User = typeof user.$inferSelect
The stable user, session, account, and verification exports also work with auth.schema.usePlural and custom Better Auth modelName values. For example, user: { modelName: 'person' } generates person, or persons with plural names, while user still refers to that table. The schema export contains both the generated names and these stable aliases. Tables omitted by your auth configuration are undefined.
Supported Dialects
The module supports all dialects compatible with NuxtHub:
sqlite(Default)postgresqlmysql
Ensure you have configured the dialect in nuxt.config.ts:
export default defineNuxtConfig({
hub: {
db: {
dialect: 'postgresql' // or 'sqlite', 'mysql'
}
}
})
Workflow
When you add a new plugin that requires database tables (e.g., adding twoFactor):
- Update
server/auth.config.ts. - Restart your development server.
- The module regenerates the schema.
- If using NuxtHub local development, the tables are ready.
- Deploy with migrations:
For local development, migrations run automatically when you start the dev server.
For production (Cloudflare D1):npx nuxt db migrate
When not to use this
Do not rely on this flow if:
- you are not using NuxtHub
- you are in
clientOnlymode - you manage Better Auth tables with a separate adapter and migration pipeline
In those cases, use custom database setup.