Back to Better Auth

SQLite

docs/content/docs/adapters/sqlite.mdx

1.6.93.7 KB
Original Source

SQLite is a lightweight, serverless, self-contained SQL database engine that is widely used for local data storage in applications. Read more here.

Example Usage

Better Auth supports multiple SQLite drivers. Choose the one that best fits your environment:

The most popular and stable SQLite driver for Node.js:

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 (Experimental)

<Callout type="warning"> The `node:sqlite` module is still experimental and may change at any time. It requires Node.js 22.5.0 or later. </Callout>

Starting from Node.js 22.5.0, you can use the built-in SQLite module:

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 module in Bun, which is similar to the Node.js version:

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 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 (Experimental)

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.

The Kysely SQLite 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.

ts
export const auth = betterAuth({
  experimental: { joins: true }
});
<Callout type="warn"> It's possible that you may need to run migrations after enabling this feature. </Callout>

Additional Information

SQLite is supported under the hood via the Kysely 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>.