How to avoid singleton database when setting up better auth

from what I am seeing in the installation steps, the way to set this up is by making a specific file in an specific folder and exporting specifically an auth variable:
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
}),
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
}),
});
I don't have a singleton database, so this is impossible for me. my database is setup a main function on startup, not exported. someone else offered this solution in a different post, but it doesn't work:
// lib/auth.ts
export function initAuth(db: DB) {
return betterAuth({
database: drizzleAdapter(db, { provider: 'pg' })
});
}

// index.ts
async function main() {
...

const db = await openDB({ config: config.value });
if (db.isErr()) {
...
}

const { runMigrations } = await getMigrations(initAuth(db.value) as BetterAuthOptions);
await runMigrations();

...
}

await main();
// lib/auth.ts
export function initAuth(db: DB) {
return betterAuth({
database: drizzleAdapter(db, { provider: 'pg' })
});
}

// index.ts
async function main() {
...

const db = await openDB({ config: config.value });
if (db.isErr()) {
...
}

const { runMigrations } = await getMigrations(initAuth(db.value) as BetterAuthOptions);
await runMigrations();

...
}

await main();
I get this error:
2025-09-24T02:44:44.487Z WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this.
2025-09-24T02:44:44.488Z ERROR [Better Auth]: Only kysely adapter is supported for migrations. You can use `generate` command to generate the schema, if you're using a different adapter.
2025-09-24T02:44:44.487Z WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this.
2025-09-24T02:44:44.488Z ERROR [Better Auth]: Only kysely adapter is supported for migrations. You can use `generate` command to generate the schema, if you're using a different adapter.
nowhere in the docs seem to be explained why better auth needs to be initialised in this very specific way or what the consequences are for not doing so, either way I still like to inject my dependencies, is there any way to achieve this pattern?\
6 Replies
caches
cachesOP4w ago
@Better Auth can you help me with the above
Better Auth
Better Auth4w ago
Hi there! I'd be happy to help with Better Auth. To give you a quick overview, Better Auth is a framework-agnostic authentication solution for TypeScript offering built‑in support for email/password logins, social sign-on providers, two-factor authentication, and a robust plugin ecosystem for advanced features [1], [2]. Some common questions from the community include: • Configuration & Integration:
 – How to set up email/password or social sign-on authentication?
 – How to integrate Better Auth into various frameworks (Next.js, Hono, Convex, etc.)?
 These topics are covered in our installation guides and framework-specific documentation [1], [18].
• Type Safety & Customization:
 – How to leverage TypeScript with strict mode and infer custom additional fields for user and session objects?
 – How to extend or customize the default authentication flows using plugins?
 We provide detailed instructions on setting up type-safe configurations and extending types with additional fields [4].
• Advanced Features:
 – Enabling and managing two-factor authentication (2FA), including handling TOTP code verifications.
 – Configuring passkey registration with custom options using the Passkey plugin.
 Our 2FA documentation explains enabling 2FA for credential accounts, while our passkey plugin updates cover client-side options and global authenticatorSelection settings [5], [15].
Are you looking for guidance on one of these specific areas, or is there another aspect of Better Auth you'd like to explore further? Let me know, and I'll
Better Auth
Better Auth4w ago
Introduction | Better Auth
Introduction to Better Auth.
Better Auth
Better Auth
The most comprehensive authentication library for TypeScript.
TypeScript | Better Auth
Better Auth TypeScript integration.
Two-Factor Authentication (2FA) | Better Auth
Enhance your app's security with two-factor authentication.
GitHub
feat: passkey plugin with client-side authenticatorAttachment an...
Description This PR addresses Issue #1311 by enhancing the Passkey plugin in better-auth to allow clients to specify the authenticatorAttachment (platform or cross-platform) during passkey registra...
caches
cachesOP4w ago
@Better Auth from what I am seeing in the installation steps, the way to set this up is by making a specific file in an specific folder and exporting specifically an auth variable:
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
}),
});
import { betterAuth } from "better-auth";
import { drizzleAdapter } from "better-auth/adapters/drizzle";
import { db } from "@/db"; // your drizzle instance

export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg", // or "mysql", "sqlite"
}),
});
I don't have a singleton database, so this is impossible for me. my database is setup a main function on startup, not exported. someone else offered this solution in a different post, but it doesn't work:
// lib/auth.ts
export function initAuth(db: DB) {
return betterAuth({
database: drizzleAdapter(db, { provider: 'pg' })
});
}

// index.ts
async function main() {
...

const db = await openDB({ config: config.value });
if (db.isErr()) {
...
}

const { runMigrations } = await getMigrations(initAuth(db.value) as BetterAuthOptions);
await runMigrations();

...
}

await main();
// lib/auth.ts
export function initAuth(db: DB) {
return betterAuth({
database: drizzleAdapter(db, { provider: 'pg' })
});
}

// index.ts
async function main() {
...

const db = await openDB({ config: config.value });
if (db.isErr()) {
...
}

const { runMigrations } = await getMigrations(initAuth(db.value) as BetterAuthOptions);
await runMigrations();

...
}

await main();
I get this error:
2025-09-24T02:44:44.487Z WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this.
2025-09-24T02:44:44.488Z ERROR [Better Auth]: Only kysely adapter is supported for migrations. You can use `generate` command to generate the schema, if you're using a different adapter.
2025-09-24T02:44:44.487Z WARN [Better Auth]: Could not determine database type, defaulting to sqlite. Please provide a type in the database options to avoid this.
2025-09-24T02:44:44.488Z ERROR [Better Auth]: Only kysely adapter is supported for migrations. You can use `generate` command to generate the schema, if you're using a different adapter.
nowhere in the docs seem to be explained why better auth needs to be initialised in this very specific way or what the consequences are for not doing so, either way I still like to inject my dependencies, is there any way to achieve this pattern?\
Ping
Ping4w ago
It's most likely coming from getMigrations, since the provided DB is the drizzle adapter it won't work, you must pass an actual database connection for pg I recommed just using the pg package and creating a new Pool("connection-string-here") and then overwriting the database with that pool you created right before you pass it to the getMigrations. Also with initAuth it's returning better-auth itself, you need to do initAuth(...).options to grab the BetterAuthOptions
caches
cachesOP4w ago
Got it, thanks! I'll try that.

Did you find this page helpful?