Skip to content

SQLite Adapter (@xyzintel/authlite-sqlite)

The SQLite adapter implements the AuthAdapter interface using better-sqlite3 — a synchronous, native SQLite binding for Node.js.

Requires Node.js 18+ and better-sqlite3 version 11 or 12 (^11.0.0 || ^12.0.0).

SQLiteAdapter Class

Constructor

typescript
new SQLiteAdapter(dbPath: string | Database.Database)

Accepts either a file path or an existing better-sqlite3 Database instance.

typescript
// From a file path (creates the database if it doesn't exist)
import { SQLiteAdapter } from "@xyzintel/authlite-sqlite";
const adapter = new SQLiteAdapter("./auth.db");

// From an existing Database instance
import Database from "better-sqlite3";
const db = new Database("./auth.db");
const adapter = new SQLiteAdapter(db);

Automatic Schema

When auth.init() is called (which delegates to adapter.initialize()), the adapter creates the following tables:

users table:

ColumnTypeConstraints
idTEXTPRIMARY KEY
emailTEXTUNIQUE NOT NULL
password_hashTEXTNOT NULL
email_verifiedINTEGERDEFAULT 0
created_atDATETIMEDEFAULT CURRENT_TIMESTAMP
updated_atDATETIMEDEFAULT CURRENT_TIMESTAMP

sessions table:

ColumnTypeConstraints
idTEXTPRIMARY KEY
user_idTEXTNOT NULL (FK → users.id, ON DELETE CASCADE)
token_hashTEXTNOT NULL (indexed for fast lookups)
expires_atDATETIMENOT NULL
created_atDATETIMEDEFAULT CURRENT_TIMESTAMP

All tables use IF NOT EXISTS, so repeated init() calls are safe.

Complete Example

typescript
import { AuthLite } from "@xyzintel/authlite-core";
import { SQLiteAdapter } from "@xyzintel/authlite-sqlite";

const adapter = new SQLiteAdapter("./auth.db");
const auth = new AuthLite(adapter, {
  secret: process.env.AUTH_SECRET!,
});

await auth.init();

// Ready to use
const result = await auth.signUp("user@example.com", "secure-password");

IDs

User and session IDs are generated using nanoid — URL-safe, collision-resistant unique identifiers.

Column Mapping

The adapter maps between snake_case database columns and camelCase JavaScript properties:

  • password_hashpasswordHash
  • email_verifiedemailVerified
  • created_atcreatedAt
  • updated_atupdatedAt
  • user_iduserId
  • token_hashtokenHash
  • expires_atexpiresAt

Performance

  • The sessions.token_hash column is indexed for constant-time session lookups.
  • better-sqlite3 operates synchronously (no event loop overhead), which is ideal for SQLite's single-writer model.

Released under the MIT License.