Database

Learn how to configure and manage databases in your SaaS application.

Supported Databases

Multi-Database Support

The boilerplate supports multiple database options with automatic detection

SQLite (Default)

Zero-configuration database perfect for development and small applications.

Default Option
file:./auth.db - Local SQLite file

No environment variables required - works out of the box

Database Setup

Configuration Guide

Step-by-step guide to set up your preferred database

1. Choose Your Database

Development

Use SQLite for quick setup

Production

Use Turso for edge performance or MySQL for enterprise

2. Set Environment Variables

Add the required variables to your .env.local file:

# For Turso
TURSO_DATABASE_URL=libsql://your-db.turso.io
TURSO_AUTH_TOKEN=your-auth-token

# For MySQL
USE_MYSQL=true
MYSQL_DATABASE_URL=mysql://user:pass@host:3306/db

3. Restart Application

The database dialect is selected at server startup based on environment variables.

$ pnpm dev

Migrations

Database Schema Management

Better Auth handles database migrations automatically

Automatic Migrations

Better Auth automatically creates and updates database tables when your application starts. No manual migration process is required.

Tables Created

users - User accounts and profiles
sessions - Active user sessions
accounts - OAuth provider accounts
organizations - Team/organization data
subscriptions - Stripe subscription data

Database File

For SQLite, the database file is created at ./auth.db in your project root. You can inspect this file with any SQLite browser tool.

Query Builder

Kysely Integration

Type-safe SQL query builder with excellent TypeScript support

Why Kysely?

Type-Safe
Full TypeScript type safety
Multi-DB
Supports all configured databases
IntelliSense
Excellent IDE autocomplete

Usage Examples

Basic Query:

const user = await db
  .selectFrom('users')
  .where('id', '=', userId)
  .selectAll()
  .executeTakeFirst()

With Relations:

const userWithOrg = await db
  .selectFrom('users')
  .innerJoin('organizations', 'users.organizationId', 'organizations.id')
  .selectAll()
  .execute()

Database Access

The database instance is available through the Better Auth configuration. Access it in your API routes and server components.