MS SQL
将 Better Auth 与 MS SQL 集成。
Microsoft SQL Server 是微软开发的一款关系型数据库管理系统,专为企业级的数据存储、管理和分析设计,具备强大的安全性和可扩展性特性。 更多内容请阅读 这里。
示例用法
确保您已经安装并配置好 MS SQL。 然后,您可以将它直接连接到 Better Auth。
import { betterAuth } from "better-auth";
import { MssqlDialect } from "kysely";
import * as Tedious from 'tedious'
import * as Tarn from 'tarn'
const dialect = new MssqlDialect({
tarn: {
...Tarn,
options: {
min: 0,
max: 10,
},
},
tedious: {
...Tedious,
connectionFactory: () => new Tedious.Connection({
authentication: {
options: {
password: 'password',
userName: 'username',
},
type: 'default',
},
options: {
database: 'some_db',
port: 1433,
trustServerCertificate: true,
},
server: 'localhost',
}),
},
TYPES: {
...Tedious.TYPES,
DateTime: Tedious.TYPES.DateTime2,
},
})
export const auth = betterAuth({
database: {
dialect,
type: "mssql"
}
});
更多信息,请阅读 Kysely 的文档关于 MssqlDialect。
架构生成与迁移
Better Auth CLI 允许你基于 Better Auth 的配置和插件生成或迁移你的数据库架构。
MS SQL 架构生成 | MS SQL 架构迁移 |
|---|---|
| ✅ 支持 | ✅ 支持 |
npx auth@latest migratenpx auth@latest generate联结(实验性)
数据库联结在 Better Auth 需要从多个表中单查询获取相关数据时非常有用。
像 /get-session、/get-full-organization 等端点借助此功能,
根据数据库延迟不同,性能提升可达 2 倍到 3 倍。
Kysely MS SQL 方言自 1.4.0 版本起支持联结。
要启用此功能,需要在你的 auth 配置中将 experimental.joins 选项设置为 true。
import { betterAuth } from "better-auth";
export const auth = betterAuth({
experimental: { joins: true }
});启用此功能后,您可能需要运行迁移。
其他信息
MS SQL 是通过 Kysely 适配器在底层支持的,Kysely 支持的任何数据库也都将得到支持。 (阅读更多)
如果您想了解性能提升或优化技巧,请查看我们的 性能优化指南。