Drizzle ORM 适配器
Drizzle ORM 是一个功能强大且灵活的 Node.js 和 TypeScript ORM。它提供了一个简单直观的 API 用于操作数据库,并支持包括 MySQL、PostgreSQL、SQLite 等在内的多种数据库。
【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.】
在开始之前,请确保已安装并配置好 Drizzle。更多信息,请参阅 Drizzle 文档
【Before getting started, make sure you have Drizzle installed and configured. For more information, see Drizzle Documentation】
示例用法
【Example Usage】
你可以使用 Drizzle 适配器按如下方式连接到你的数据库。
【You can use the Drizzle adapter to connect to your database as follows.】
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】
Better Auth CLI 允许你根据你的 Better Auth 配置和插件生成或迁移数据库模式。
【The Better Auth CLI allows you to generate or migrate your database schema based on your Better Auth configuration and plugins.】
要生成 Better Auth 所需的架构,请运行以下命令:
【To generate the schema required by Better Auth, run the following command:】
npx @better-auth/cli@latest generate要生成并应用迁移,请运行以下命令:
【To generate and apply the migration, run the following commands:】
npx drizzle-kit generate # generate the migration filenpx drizzle-kit migrate # apply the migration联接(实验性)
【Joins (Experimental)】
当 Better-Auth 需要在单个查询中从多张表获取相关数据时,数据库连接(joins)非常有用。像 /get-session、/get-full-organization 等端点以及许多其他端点都极大地受益于这一功能,根据数据库延迟的不同,性能提升可达 2 到 3 倍。
【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.】
自 1.4.0 版本起,Drizzle 适配器开箱即用地支持连接(joins)。要启用此功能,你需要在你的认证配置中将 experimental.joins 选项设置为 true。
【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 @better-auth/cli@latest generate to generate a new Drizzle schema with the relations.
此外,你需要将每个 relation 传递给 drizzle 适配器模式对象。
修改表名
【Modifying Table Names】
Drizzle 适配器期望你定义的 schema 与表名匹配。例如,如果你的 Drizzle schema 将 user 表映射为 users,你需要手动传入 schema 并将其映射到 user 表。
【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,
},
}),
});你可以像上面的示例一样修改提供的模式值,或者直接修改 auth 配置的 modelName 属性。例如:
【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】
我们根据你传递给 Drizzle 模式的属性映射字段名称。例如,如果你想将 email 字段修改为 email_address,只需将 Drizzle 模式更改为:
【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
});你可以像上面的例子那样修改 Drizzle 模式,或者直接修改 auth 配置的 fields 属性。例如:
【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】
如果你所有的表都使用复数形式,你可以直接传递 usePlural 选项:
【If all your tables are using plural form, you can just pass the usePlural option:】
export const auth = betterAuth({
database: drizzleAdapter(db, {
...
usePlural: true,
}),
});附加信息
【Additional Information】
- 如果你正在寻找性能改进或技巧,请查看我们的 性能优化指南。