微软 SQL

Microsoft SQL Server 是由微软开发的关系型数据库管理系统,旨在为企业级数据存储、管理和分析提供支持,具有强大的安全性和可扩展性功能。 更多信息请点击 这里

【Microsoft SQL Server is a relational database management system developed by Microsoft, designed for enterprise-level data storage, management, and analytics with robust security and scalability features. Read more here.】

示例用法

【Example Usage】

确保你已经安装并配置了 MS SQL。然后,你可以将它直接连接到 Better Auth。

【Make sure you have MS SQL installed and configured. Then, you can connect it straight into Better Auth.】

auth.ts
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"
  }
});

For more information, read Kysely's documentation to the MssqlDialect.

架构生成与迁移

【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.】

MS SQL 架构生成

MS SQL 架构迁移

✅ 支持✅ 支持
Schema Generation
npx @better-auth/cli@latest generate
Schema Migration
npx @better-auth/cli@latest migrate

联接(实验性)

【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 版本起,Kysely MS SQL 方言原生支持连接(joins)。 要启用此功能,你需要在身份验证配置中将 experimental.joins 选项设置为 true

【The Kysely MS SQL dialect 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.】

auth.ts
export const auth = betterAuth({
  experimental: { joins: true }
});

It's possible that you may need to run migrations after enabling this feature.

附加信息

【Additional Information】

MS SQL 在底层通过 Kysely 适配器得到支持,任何 Kysely 支持的数据库也将被支持。(在此处阅读更多)

如果你正在寻找性能改进或技巧,请查看我们的 性能优化指南

On this page