NextSaas
NextSaas
IntroductionGetting StartedConfigurationDatabaseAuthenticationEmailsInternationalizationPaymentsStorageDeploymentChangelog

Database

Configure and use PostgreSQL or SQLite with Drizzle ORM.

NextSaas supports PostgreSQL and SQLite out of the box. Simply set two environment variables and you're ready to go — no code changes needed to switch between databases.

Setup

PostgreSQL (default)

  1. Provision a PostgreSQL database from any provider (Neon, Supabase, or local Docker).

  2. Configure your .env:

    DATABASE_ADAPTER="pg"
    DATABASE_URL="postgresql://user:password@host:5432/dbname?sslmode=require"

SQLite

  1. Configure your .env:

    DATABASE_ADAPTER="sqlite"
    DATABASE_URL="file:data/local.db"

    You can also use an in-memory database for development:

    DATABASE_URL=":memory:"

Usage

All database access goes through getDb():

import { getDb } from "@/db";

export default async function Page() {
  const db = await getDb();

  const user = await db.users.findById(userId);
  const orders = await db.orders.findByUserId(userId);
  await db.orders.create({ id, userId, amount, ... });
}

You can also import types for use in your components:

import { getDb, type Order, type User } from "@/db";

Modifying Schema

To add a new field (e.g., phone on users):

  1. Edit the schema file for each database you support:

    // PostgreSQL schema
    export const usersTable = pgTable("users", {
      // ...existing fields
      phone: text("phone"),
    });
    // SQLite schema
    export const usersTable = sqliteTable("users", {
      // ...existing fields
      phone: text("phone"),
    });
  2. Generate and apply the migration:

    bun run db:generate
    bun run db:migrate

Migrations

After any schema change, run these two commands:

  1. Generate migration — creates a SQL migration file:

    bun run db:generate
  2. Apply migration — executes against your database:

    bun run db:migrate

Drizzle Studio

Browse and manage your database data with Drizzle Studio:

bunx drizzle-kit studio

Adding a New Database Adapter

NextSaas is designed to make it easy to add support for other databases. Here's how to add a new adapter, using MySQL as an example:

  1. Create a schema file at <src>/db/drivers/mysql/schema.ts, defining the same tables using Drizzle's MySQL column types. You can reference the existing PostgreSQL or SQLite schema as a guide.

  2. Create an adapter file at <src>/db/drivers/mysql/adapter.ts that implements all the database query methods. Reference an existing adapter for the expected function signatures.

  3. Register the new driver in the following files:

    • <src>/db/types.ts — add "mysql" to the DriverType union
    • <src>/db/factory.ts — add a case "mysql" branch in both createAdapter() and createAuthDrizzle()
    • <src>/env.ts — add "mysql" to the DATABASE_ADAPTER validation
  4. Configure your .env:

    DATABASE_ADAPTER="mysql"
    DATABASE_URL="mysql://user:password@host:3306/dbname"
  5. Generate and apply migrations:

    bun run db:generate
    bun run db:migrate

No changes are needed in any business code — everything works through getDb() automatically.

Configuration

Customize your SaaS branding and settings.

Authentication

Secure user login with Better Auth and social providers.

On this page

SetupPostgreSQL (default)SQLiteUsageModifying SchemaMigrationsDrizzle StudioAdding a New Database Adapter