better-sqlite3 adapter

Adapter for better-sqlite3 provided by the SQLite adapter package.

import { betterSqlite3 } from "@lucia-auth/adapter-sqlite";
const betterSqlite3: (
	db: Database,
	tableNames: {
		user: string;
		key: string;
		session: string | null;
	}
) => InitializeAdapter<Adapter>;
Parameters#

Table names are automatically escaped.

nametypedescription
dbDatabasebetter-sqlite3 database instance
tableNames.userstringUser table name
tableNames.keystringKey table name
tableNames.sessionstring | nullSession table name - can be null when using alongside a session adapter

Installation#

npm i @lucia-auth/adapter-sqlite
pnpm add @lucia-auth/adapter-sqlite
yarn add @lucia-auth/adapter-sqlite

Usage#

import { lucia } from "lucia";
import { betterSqlite3 } from "@lucia-auth/adapter-sqlite";
import sqlite from "better-sqlite3";

const db = sqlite("main.db");

const auth = lucia({
	adapter: betterSqlite3(db, {
		user: "user",
		key: "user_key",
		session: "user_session"
	})
	// ...
});

SQLite3 schema#

You can choose any table names, just make sure to define them in the adapter argument. The id columns are not UUID types with the default configuration.

User table#

You can add additional columns to store user attributes.

CREATE TABLE user (
    id TEXT NOT NULL PRIMARY KEY
);

Key table#

Make sure to update the REFERENCES if you change the user table name.

CREATE TABLE user_key (
    id TEXT NOT NULL PRIMARY KEY,
    user_id TEXT NOT NULL,
    hashed_password TEXT,
    FOREIGN KEY (user_id) REFERENCES user(id)
);

Session table#

You can add additional columns to store session attributes. Make sure to update REFERENCES if you change the user table name.

CREATE TABLE user_session (
    id TEXT NOT NULL PRIMARY KEY,
    user_id TEXT NOT NULL,
    active_expires INTEGER NOT NULL,
    idle_expires INTEGER NOT NULL,
    FOREIGN KEY (user_id) REFERENCES user(id)
);