Kysely Adapter
@authhero/kysely-adapter implements AuthHero's adapter interfaces on top of the Kysely query builder.
New deployments should use Drizzle
The Kysely adapter is maintained for existing deployments — notably MySQL on PlanetScale. New deployments, and every create-authhero template, use the Drizzle adapter on SQLite or Cloudflare D1.
Supported databases
| Database | Status |
|---|---|
SQLite (better-sqlite3) | Tested — used by the adapter's own test suite |
MySQL on PlanetScale (kysely-planetscale) | Tested — runs in production |
| PostgreSQL and other Kysely dialects | Not supported. The adapter accepts any Kysely<Database> instance, but its migrations and queries are only tested on SQLite and MySQL. |
Installation
npm install @authhero/kysely-adapter kysely
# plus the driver for your database, for example:
npm install better-sqlite3 # SQLite
npm install kysely-planetscale # PlanetScale (MySQL)Configuration
Create a Kysely instance with the dialect for your database, run the migrations, and pass the adapters to init.
SQLite
import SQLite from "better-sqlite3";
import { Kysely, SqliteDialect } from "kysely";
import createAdapters, {
migrateToLatest,
type Database,
} from "@authhero/kysely-adapter";
import { init } from "authhero";
const db = new Kysely<Database>({
dialect: new SqliteDialect({ database: new SQLite("authhero.db") }),
});
await migrateToLatest(db);
const { app } = init({ dataAdapter: createAdapters(db) });MySQL on PlanetScale
import { Kysely } from "kysely";
import { PlanetScaleDialect } from "kysely-planetscale";
import createAdapters, { type Database } from "@authhero/kysely-adapter";
const db = new Kysely<Database>({
dialect: new PlanetScaleDialect({ url: process.env.DATABASE_URL }),
});
const dataAdapter = createAdapters(db);Using the adapters
createAdapters(db) returns a DataAdapters object with one adapter per entity. Methods on tenant-scoped entities take the tenant id first. The exceptions are the tenants adapter itself (create and list take only their params, get/update/remove the tenant id), signing keys, which are keyed by kid, and clients.getByClientId, which resolves a client id to its tenant.
const dataAdapter = createAdapters(db);
const user = await dataAdapter.users.create("tenant_456", {
user_id: "auth2|user_123",
email: "user@example.com",
name: "John Doe",
provider: "auth2",
connection: "Username-Password-Authentication",
email_verified: true,
is_social: false,
});
const { users } = await dataAdapter.users.list("tenant_456", {
page: 0,
per_page: 10,
q: "john",
});
await dataAdapter.users.update("tenant_456", "auth2|user_123", {
name: "John Smith",
});You normally don't call the adapters directly — init uses them to serve the Auth and Management APIs. Calling them is useful in seed and migration scripts.
Migrations
The schema migrations ship with the package and run from code. There is no CLI:
import {
formatMigrationPlan,
migrateDown,
migrateToLatest,
planMigrations,
} from "@authhero/kysely-adapter";
// Preview the pending migrations without applying them
console.log(formatMigrationPlan(await planMigrations(db)));
// Apply every pending migration
await migrateToLatest(db);
// Roll back the most recent migration
await migrateDown(db);Pass true as the second argument of migrateToLatest or migrateDown to log each step.
Database Schema
The Kysely adapter implements a comprehensive multi-tenant database schema designed for authentication and authorization. The schema supports:
- Multi-tenancy: Complete data isolation between tenants
- User Management: Comprehensive user profiles and authentication
- OAuth/OIDC: Full OAuth 2.0 and OpenID Connect support
- RBAC: Role-based access control with permissions
- Organizations: Hierarchical multi-tenancy
- Audit Logging: Complete audit trail
- Customization: Themes, branding, and custom domains
Entity Relationship Diagram
Troubleshooting
Common Issues
- Migration Errors: Ensure the database user has DDL permissions. Run
planMigrationsfirst to see whatmigrateToLatestwould apply. - Connection Timeouts: Increase the timeout settings of your driver or dialect.
- Type Errors: Create the Kysely instance as
Kysely<Database>, withDatabaseimported from@authhero/kysely-adapter.
Debug Logging
Kysely logs queries and errors when you pass a log option to the instance:
const db = new Kysely<Database>({
dialect,
log: ["query", "error"],
});