mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-19 23:53:17 -05:00
137 lines
3.8 KiB
Plaintext
137 lines
3.8 KiB
Plaintext
---
|
|
title: SQLite
|
|
description: Integrate Better Auth with SQLite.
|
|
---
|
|
|
|
SQLite is a lightweight, serverless, self-contained SQL database engine that is widely used for local data storage in applications.
|
|
Read more [here.](https://www.sqlite.org/)
|
|
|
|
## Example Usage
|
|
|
|
Better Auth supports multiple SQLite drivers. Choose the one that best fits your environment:
|
|
|
|
### Better-SQLite3 (Recommended)
|
|
|
|
The most popular and stable SQLite driver for Node.js:
|
|
|
|
```ts title="auth.ts"
|
|
import { betterAuth } from "better-auth";
|
|
import Database from "better-sqlite3";
|
|
|
|
export const auth = betterAuth({
|
|
database: new Database("database.sqlite"),
|
|
});
|
|
```
|
|
|
|
<Callout>
|
|
For more information, read Kysely's documentation to the
|
|
[SqliteDialect](https://kysely-org.github.io/kysely-apidoc/classes/SqliteDialect.html).
|
|
</Callout>
|
|
|
|
### Node.js Built-in SQLite (Release Candidate)
|
|
|
|
<Callout type="info">
|
|
The `node:sqlite` module is a **Release Candidate** as of Node.js 24.15.0. It requires Node.js 22.5.0 or later and no longer requires the `--experimental-sqlite` flag since Node.js 22.13.0 / 23.4.0.
|
|
</Callout>
|
|
|
|
Starting from Node.js 22.5.0, you can use the built-in [SQLite](https://nodejs.org/api/sqlite.html) module:
|
|
|
|
```ts title="auth.ts"
|
|
import { betterAuth } from "better-auth";
|
|
import { DatabaseSync } from "node:sqlite";
|
|
|
|
export const auth = betterAuth({
|
|
database: new DatabaseSync("database.sqlite"),
|
|
});
|
|
```
|
|
|
|
To run your application with Node.js SQLite:
|
|
|
|
```bash
|
|
node your-app.js
|
|
```
|
|
|
|
### Bun Built-in SQLite
|
|
|
|
<Callout type="warning">
|
|
Use `bunx` with the `--bun` flag to run cli commands to prevent seeing type errors related to recognizing the `bun:sqlite` module, eg: `bunx --bun auth@latest generate`
|
|
</Callout>
|
|
|
|
You can also use the built-in [SQLite](https://bun.com/docs/api/sqlite) module in Bun, which is similar to the Node.js version:
|
|
|
|
```ts title="auth.ts"
|
|
import { betterAuth } from "better-auth";
|
|
import { Database } from "bun:sqlite";
|
|
|
|
export const auth = betterAuth({
|
|
database: new Database("database.sqlite"),
|
|
});
|
|
```
|
|
|
|
## Schema generation & migration
|
|
|
|
The [Better Auth CLI](/docs/concepts/cli) allows you to generate or migrate
|
|
your database schema based on your Better Auth configuration and plugins.
|
|
|
|
<table>
|
|
<thead>
|
|
<tr className="border-b">
|
|
<th>
|
|
<p className="font-bold text-[16px] mb-1">SQLite Schema Generation</p>
|
|
</th>
|
|
|
|
<th>
|
|
<p className="font-bold text-[16px] mb-1">SQLite Schema Migration</p>
|
|
</th>
|
|
</tr>
|
|
</thead>
|
|
|
|
<tbody>
|
|
<tr className="h-10">
|
|
<td>✅ Supported</td>
|
|
<td>✅ Supported</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<Tabs items={["migrate", "generate"]}>
|
|
<Tab value="migrate">
|
|
```package-install
|
|
npx auth@latest migrate
|
|
```
|
|
</Tab>
|
|
|
|
<Tab value="generate">
|
|
```package-install
|
|
npx auth@latest generate
|
|
```
|
|
</Tab>
|
|
</Tabs>
|
|
|
|
## Joins
|
|
|
|
Database joins are 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 Kysely SQLite dialect supports joins out of the box since version `1.4.0`.
|
|
To enable this feature, set `advanced.database.joins` to `true` in your auth configuration.
|
|
|
|
```ts title="auth.ts"
|
|
import { betterAuth } from "better-auth";
|
|
|
|
export const auth = betterAuth({
|
|
advanced: {
|
|
database: {
|
|
joins: true,
|
|
},
|
|
},
|
|
});
|
|
```
|
|
|
|
## Additional Information
|
|
|
|
SQLite is supported under the hood via the [Kysely](https://kysely.dev/) adapter, any database supported by Kysely would also be supported. (<Link href="/docs/adapters/other-relational-databases">Read more here</Link>)
|
|
|
|
If you're looking for performance improvements or tips, take a look at our guide to <Link href="/docs/guides/optimizing-for-performance">performance optimizations</Link>.
|