From 238564f311a6adcb45acd7178a77a49070613d1c Mon Sep 17 00:00:00 2001 From: Taesu <166604494+bytaesu@users.noreply.github.com> Date: Sun, 23 Aug 2026 15:56:32 +0000 Subject: [PATCH] docs: improve Prisma 7 adapter setup guide (#10943) --- docs/content/docs/adapters/prisma.mdx | 121 +++++++++++++++++++++----- docs/content/docs/installation.mdx | 9 +- 2 files changed, 103 insertions(+), 27 deletions(-) diff --git a/docs/content/docs/adapters/prisma.mdx b/docs/content/docs/adapters/prisma.mdx index 2c62fac6fc..f45fa00da3 100644 --- a/docs/content/docs/adapters/prisma.mdx +++ b/docs/content/docs/adapters/prisma.mdx @@ -5,31 +5,85 @@ description: Integrate Better Auth with Prisma. Prisma ORM is an open-source database toolkit that simplifies database access and management in applications by providing a type-safe query builder and an intuitive data modeling interface. -Before getting started, make sure you have Prisma installed and configured. For more information, see [Prisma Documentation](https://www.prisma.io/docs/) +For an introduction to Prisma ORM, see +[Prisma getting started](https://www.prisma.io/docs/getting-started). + + + This guide uses Prisma 7 and PostgreSQL. If you use Prisma 6 or earlier, a + driver adapter is optional and your existing Prisma Client setup can remain + unchanged. + ## Installation -To use the Prisma adapter, you need to install the `@better-auth/prisma-adapter` package: +To use the Prisma adapter, install `@better-auth/prisma-adapter`: ```package-install @better-auth/prisma-adapter ``` -Prisma 7 with SQLite requires the SQLite driver adapter: +The example below uses Prisma 7 and PostgreSQL. If Prisma is not already +configured, follow +[Prisma's PostgreSQL quickstart](https://www.prisma.io/docs/prisma-orm/quickstart/postgresql) +before continuing. -```package-install -@prisma/adapter-better-sqlite3 +## Setup + +The examples below use the following project structure: + + + + + + + + + + + + + + + + + + + + + + + + + +### Configure Prisma + +If you are starting a new Prisma project, initialize it with PostgreSQL and an +explicit Prisma Client output path: + +```package-install title="Initialize Prisma" +npx prisma init --datasource-provider postgresql --output ../src/generated/prisma ``` -## Example Usage +This creates `prisma/schema.prisma`, `prisma.config.ts`, and `.env`. Set +`DATABASE_URL` in `.env` to your PostgreSQL connection string. -You can use the Prisma adapter to connect to your database as follows. +If Prisma is already configured in your project, keep your existing datasource +and output path and skip this initialization command. -```ts title="auth.ts" -import { betterAuth } from "better-auth"; -import { prismaAdapter } from "better-auth/adapters/prisma"; -import { PrismaClient } from "@prisma/client"; -import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3"; +Generate Prisma Client after configuring its output path: + +```package-install title="Generate Prisma Client" +npx prisma generate +``` + +### Create the Prisma client + +Import `PrismaClient` from the output path configured in your Prisma schema and +pass the PostgreSQL driver adapter to it: + +```ts title="src/lib/prisma.ts" +import { PrismaPg } from "@prisma/adapter-pg"; +import { PrismaClient } from "../generated/prisma/client"; const databaseUrl = process.env.DATABASE_URL; @@ -37,26 +91,38 @@ if (!databaseUrl) { throw new Error("DATABASE_URL is not set"); } -const adapter = new PrismaBetterSqlite3({ - url: databaseUrl, +const adapter = new PrismaPg({ + connectionString: databaseUrl, }); -const prisma = new PrismaClient({ adapter }); + +export const prisma = new PrismaClient({ adapter }); +``` + +Create one `PrismaClient` instance and reuse it across your application. +Frameworks with hot reloading or serverless runtimes may require a +framework-specific lifecycle pattern. + +### Configure Better Auth + +Pass the Prisma Client instance to the Better Auth Prisma adapter: + +```ts title="src/lib/auth.ts" +import { betterAuth } from "better-auth"; +import { prismaAdapter } from "better-auth/adapters/prisma"; +import { prisma } from "./prisma"; export const auth = betterAuth({ database: prismaAdapter(prisma, { - provider: "sqlite", + provider: "postgresql", }), }); ``` - - Starting from Prisma 7, the `output` path field is required. If you have configured a custom output path in your `schema.prisma` file (e.g., `output = "../src/generated/prisma"`), make sure to [import the Prisma client from that location](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#the-location-of-prisma-client) instead of `@prisma/client`. - - ## Schema generation & migration -The [Better Auth CLI](/docs/concepts/cli) allows you to generate or migrate -your database schema based on your Better Auth configuration and plugins. +The [Better Auth CLI](/docs/concepts/cli) generates the Prisma schema required +by your Better Auth configuration and plugins. Use the Prisma CLI to create and +apply a migration from the generated schema. @@ -83,6 +149,14 @@ your database schema based on your Better Auth configuration and plugins. npx auth@latest generate ``` +The Better Auth CLI updates your Prisma schema but does not apply the migration. +Use Prisma to create the database migration, then regenerate Prisma Client: + +```bash title="Terminal" +npx prisma migrate dev --name add-better-auth +npx prisma generate +``` + ## Joins Database joins are useful when Better-Auth needs to fetch related data from multiple tables in a single query. @@ -113,4 +187,5 @@ export const auth = betterAuth({ ## Additional Information * If you're looking for performance improvements or tips, take a look at our guide to performance optimizations. -* [How to use Prisma ORM with Better Auth and Next.js](https://www.prisma.io/docs/guides/betterauth-nextjs) +* [How to use Prisma ORM with Better Auth and Next.js](https://www.prisma.io/docs/guides/authentication/better-auth/nextjs) +* [How to use Prisma ORM with Better Auth and Astro](https://www.prisma.io/docs/guides/authentication/better-auth/astro) diff --git a/docs/content/docs/installation.mdx b/docs/content/docs/installation.mdx index 7559356635..65c8f181c3 100644 --- a/docs/content/docs/installation.mdx +++ b/docs/content/docs/installation.mdx @@ -136,16 +136,17 @@ description: Learn how to configure Better Auth in your project. ```ts title="auth.ts" import { betterAuth } from "better-auth"; import { prismaAdapter } from "better-auth/adapters/prisma"; - // If your Prisma file is located elsewhere, you can change the path - import { PrismaClient } from "@/generated/prisma/client"; + import { prisma } from "@/lib/prisma"; // your prisma client instance - const prisma = new PrismaClient(); export const auth = betterAuth({ database: prismaAdapter(prisma, { - provider: "sqlite", // or "mysql", "postgresql", ...etc + provider: "postgresql", // or "mysql", "sqlite", ...etc }), }); ``` + + See the [Prisma adapter guide](/docs/adapters/prisma) for the Prisma 7 + driver adapter and generated client setup.