Drizzle ORM Adapter
Drizzle ORM is a powerful and flexible ORM for Node.js and TypeScript. It provides a simple and intuitive API for working with databases, and supports a wide range of databases including MySQL, PostgreSQL, SQLite, and more. Read more here: Drizzle ORM.
Example Usage
Make sure you have Drizzle installed and configured. Then, you can use the Drizzle adapter to connect to your database.
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "./database.ts";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
}),
//... the rest of your config
});Schema generation & migration
The Better Auth CLI allows you to generate or migrate your database schema based on your Better Auth configuration and plugins.
To generate the schema required by Better Auth, run the following command:
npx @better-auth/cli@latest generateTo generate and apply the migration, run the following commands:
npx drizzle-kit generate # generate the migration file
npx drizzle-kit migrate # apply the migrationJoins (Experimental)
Database joins is useful when Better-Auth needs to fetch related data from multiple tables in a single query.
Endpoints like /get-session, /get-full-organization and many others benefit greatly from this feature,
seeing upwards of 2x to 3x performance improvements depending on database latency.
The Drizzle adapter supports joins out of the box since version 1.4.0.
To enable this feature, you need to set the experimental.joins option to true in your auth configuration.
export const auth = betterAuth({
experimental: { joins: true }
});Please make sure that your Drizzle schema has the necessary relations defined.
If you do not see any relations in your Drizzle schema, you can manually add them using the relation drizzle-orm function
or run our latest CLI version npx auth@latest generate to generate a new Drizzle schema with the relations.
Additionally, you're required to pass each relation through the drizzle adapter schema object.
Modifying Table Names
The Drizzle adapter expects the schema you define to match the table names. For example, if your Drizzle schema maps the user table to users, you need to manually pass the schema and map it to the user table.
import { betterAuth } from "better-auth";
import { db } from "./drizzle";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { schema } from "./schema";
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema: {
...schema,
user: schema.users,
},
}),
});You can either modify the provided schema values like the example above,
or you can mutate the auth config's modelName property directly.
For example:
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
user: {
modelName: "users",
}
});Modifying Field Names
We map field names based on property you passed to your Drizzle schema.
For example, if you want to modify the email field to email_address,
you simply need to change the Drizzle schema to:
export const user = mysqlTable("user", {
// Changed field name without changing the schema property name
// This allows drizzle & better-auth to still use the original field name,
// while your DB uses the modified field name
email: varchar("email_address", { length: 255 }).notNull().unique(),
// ... others
});You can either modify the Drizzle schema like the example above,
or you can mutate the auth config's fields property directly.
For example:
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite", // or "pg" or "mysql"
schema,
}),
user: {
fields: {
email: "email_address",
}
}
});Using Plural Table Names
If all your tables are using plural form, you can just pass the usePlural option:
export const auth = betterAuth({
database: drizzleAdapter(db, {
...
usePlural: true,
}),
});Performance Tips
If you're looking for performance improvements or tips, take a look at our guide to performance optimizations.