mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-24 16:11:53 -05:00
docs: fix adapter example
This commit is contained in:
@@ -101,27 +101,50 @@ If your database is not supported by Kysely, you can use an adapter to connect t
|
||||
|
||||
To use an adapter, import the adapter and pass it to the database option.
|
||||
|
||||
Currently, 3 adapters are supported:
|
||||
- **Prisma**
|
||||
- **Drizzle**
|
||||
- **MongoDB**
|
||||
<Tabs items={["prisma", "drizzle", "mongodb"]}>
|
||||
<Tab value="prisma">
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
import { prismaAdapter } from "better-auth/adapters";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
```ts title="auth.ts"
|
||||
import { prismaAdapter } from "better-auth/adapters"; // [!code highlight]
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
const prisma = new PrismaClient();
|
||||
export const auth = betterAuth({
|
||||
database: {
|
||||
provider: prismaAdapter(prisma),
|
||||
},
|
||||
});
|
||||
```
|
||||
</Tab>
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
<Tab value="drizzle">
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters";
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: {
|
||||
provider: prismaAdapter(prisma), // [!code highlight]
|
||||
}
|
||||
})
|
||||
```
|
||||
export const auth = betterAuth({
|
||||
database: {
|
||||
provider: drizzleAdapter(db),
|
||||
},
|
||||
});
|
||||
```
|
||||
</Tab>
|
||||
|
||||
<Tab value="mongodb">
|
||||
```ts title="auth.ts"
|
||||
import { betterAuth } from "better-auth";
|
||||
import { monogdbAdapter } from "better-auth/adapters";
|
||||
import { client } from "@/db"; // your mongodb client
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: {
|
||||
provider: monogdbAdapter(client),
|
||||
},
|
||||
});
|
||||
```
|
||||
</Tab>
|
||||
</Tabs>
|
||||
|
||||
<Callout>
|
||||
We highly recommend using the built-in option if your database is supported by Kysely, as we use optimized queries for Kysely.
|
||||
</Callout>
|
||||
</Step>
|
||||
|
||||
<Step>
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { and, eq, or, SQL } from "drizzle-orm";
|
||||
import type { Adapter, Where } from "../../types";
|
||||
|
||||
export interface DrizzleAdapterOptions<
|
||||
T extends Record<string, any> = Record<string, any>,
|
||||
> {
|
||||
db: T;
|
||||
export interface DrizzleAdapterOptions {
|
||||
schema: Record<string, any>;
|
||||
}
|
||||
|
||||
@@ -48,10 +45,10 @@ function whereConvertor(where: Where[], schemaModel: any) {
|
||||
return clause;
|
||||
}
|
||||
|
||||
export const drizzleAdapter = ({
|
||||
db,
|
||||
schema,
|
||||
}: DrizzleAdapterOptions): Adapter => {
|
||||
export const drizzleAdapter = (
|
||||
db: Record<string, any>,
|
||||
{ schema }: DrizzleAdapterOptions,
|
||||
): Adapter => {
|
||||
return {
|
||||
async create(data) {
|
||||
const { model, data: val } = data;
|
||||
|
||||
@@ -4,7 +4,6 @@ import { afterAll, beforeAll, beforeEach, describe, it } from "vitest";
|
||||
import { user } from "./schema";
|
||||
import { runAdapterTest } from "../../test";
|
||||
import { drizzleAdapter } from "..";
|
||||
import { getTestInstance } from "../../../test-utils/test-instance";
|
||||
import { getMigrations } from "../../../cli/utils/get-migration";
|
||||
import path from "path";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
@@ -31,8 +30,7 @@ describe("adapter test", async () => {
|
||||
},
|
||||
});
|
||||
|
||||
const adapter = drizzleAdapter({
|
||||
db: db,
|
||||
const adapter = drizzleAdapter(db, {
|
||||
schema: {
|
||||
user,
|
||||
},
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import { int, text } from "drizzle-orm/sqlite-core";
|
||||
import { sqliteTable } from "drizzle-orm/sqlite-core";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import Database from "better-sqlite3";
|
||||
import path from "path";
|
||||
|
||||
export var user = sqliteTable("user", {
|
||||
id: text("id").primaryKey().default(new Date().toISOString()),
|
||||
|
||||
Reference in New Issue
Block a user