SQLite

SQLite 是一个轻量级、无服务器、自包含的 SQL 数据库引擎,广泛用于应用程序中的本地数据存储。 阅读更多 信息。

示例用法

Better Auth 支持多种 SQLite 驱动程序。选择最适合您环境的驱动程序:

Better-SQLite3 (推荐)

Node.js 最受欢迎且稳定的 SQLite 驱动程序:

auth.ts
import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
  database: new Database("database.sqlite"),
});

更多信息,请阅读 Kysely 的文档,了解 SqliteDialect

Node.js 内置 SQLite (实验性)

node:sqlite 模块仍处于实验阶段,随时可能发生更改。它需要 Node.js 22.5.0 或更高版本。

从 Node.js 22.5.0 开始,您可以使用内置的 SQLite 模块:

auth.ts
import { betterAuth } from "better-auth";
import { DatabaseSync } from "node:sqlite";

export const auth = betterAuth({
  database: new DatabaseSync("database.sqlite"),
});

使用 Node.js SQLite 运行您的应用程序:

node your-app.js

Bun 内置 SQLite

您也可以使用 Bun 中的内置 SQLite 模块,它类似于 Node.js 版本:

auth.ts
import { betterAuth } from "better-auth";
import { Database } from "bun:sqlite";
export const auth = betterAuth({
  database: new Database("database.sqlite"),
});

架构生成与迁移

Better Auth CLI 允许您根据 Better Auth 配置和插件生成或迁移 您的数据库架构。

SQLite 架构生成

SQLite 架构迁移

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

附加信息

SQLite 通过 Kysely 适配器在底层得到支持,Kysely 支持的任何数据库也将得到支持。( 阅读更多信息 )

如果您正在寻找性能改进或提示,请查看我们的 性能优化 指南。

On this page