mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-18 11:32:15 -05:00
docs: clarify Drizzle Relations v2 setup (#11215)
This commit is contained in:
@@ -245,8 +245,8 @@ The `schemaName` option is also supported by the
|
||||
|
||||
## Drizzle Relations v2
|
||||
|
||||
The current Drizzle adapter uses Drizzle Relations v1.
|
||||
To use Drizzle Relations v2, you need to use the `@better-auth/drizzle-adapter/relations-v2` adapter.
|
||||
The default Drizzle adapter uses Relations v1. For Drizzle Relations v2, use
|
||||
`@better-auth/drizzle-adapter/relations-v2`.
|
||||
|
||||
Install the adapter:
|
||||
|
||||
@@ -254,18 +254,18 @@ Install the adapter:
|
||||
npm install @better-auth/drizzle-adapter
|
||||
```
|
||||
|
||||
Update your imports to use the relations-v2 adapter:
|
||||
Configure Better Auth with the v2 adapter and your auth schema:
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from 'better-auth';
|
||||
import { drizzleAdapter } from '@better-auth/drizzle-adapter/relations-v2'; // [!code highlight]
|
||||
import { db } from './database.ts';
|
||||
import * as schema from './schema.ts';
|
||||
import { db } from './db.ts';
|
||||
import * as schema from './auth-schema.ts';
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
provider: 'sqlite', // or "pg" or "mysql"
|
||||
schema,
|
||||
schema, // [!code highlight]
|
||||
}),
|
||||
//... the rest of your config
|
||||
});
|
||||
@@ -283,21 +283,15 @@ npx auth@latest generate
|
||||
The schema generator will output the new v2 format automatically.
|
||||
</Callout>
|
||||
|
||||
The generated auth schema exports relations using `defineRelationsPart`, which is
|
||||
designed to be merged alongside your app's own `defineRelations`. Pass both to
|
||||
the drizzle instance — `schema` is no longer required since Drizzle v1 RC:
|
||||
Merge the generated auth relations after your app's relations in your existing
|
||||
Drizzle configuration:
|
||||
|
||||
```ts title="db.ts"
|
||||
import { drizzle } from 'drizzle-orm/...';
|
||||
// generated relations from auth CLI (uses defineRelationsPart)
|
||||
import { authRelations } from './auth-schema.ts';
|
||||
// your app's own relations (uses defineRelations)
|
||||
import { relations } from './app-schema.ts';
|
||||
|
||||
export const db = drizzle({
|
||||
client,
|
||||
// authRelations uses defineRelationsPart, // [!code highlight]
|
||||
// so it must come after the main relations // [!code highlight]
|
||||
relations: { ...relations, ...authRelations }, // [!code highlight]
|
||||
});
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user