Failed to initialize database adapter

I'm currently running the Drizzle adapter, and currently cannot get it to generate a schema. The following auth.ts causes the cli to throw: Failed to initalize database adapter
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: { // Throws with or without this
...schema,
},
}),
});
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "pg",
schema: { // Throws with or without this
...schema,
},
}),
});
The database is currently the following:
import { drizzle } from "drizzle-orm/node-postgres";
import { loadEnvConfig } from "@next/env";

// Load environment variables
loadEnvConfig(process.cwd());

export const db = drizzle(process.env.DATABASE_URL!);
import { drizzle } from "drizzle-orm/node-postgres";
import { loadEnvConfig } from "@next/env";

// Load environment variables
loadEnvConfig(process.cwd());

export const db = drizzle(process.env.DATABASE_URL!);
When using the following base for betterAuth it successfully tells me: ✔ Your schema is already up to date.
export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
});
export const auth = betterAuth({
database: new Pool({
connectionString: process.env.DATABASE_URL,
}),
});
28 Replies
bekacru
bekacru10mo ago
make sure both the cli and the better auth versions are upto date. There was a breaking change.
Kyle
KyleOP10mo ago
I see, will check in a bit, it works with just making a pool with it, but not with Drizzle from above at least.
Sanjay Kholiya
Sanjay Kholiya10mo ago
any resolution yet?
bekacru
bekacru10mo ago
could you please check for me if it works for you with sqlite db? use better-sqlite3
Sanjay Kholiya
Sanjay Kholiya10mo ago
just checked, same issue with sqlite
bekacru
bekacru10mo ago
It's passing all possible tests with Drizzle, that what makes it difficult to track down the problem. Anyway, in case you're using an older version or if there was a version causing the issue, could you try 0.8.6-beta.5?
Kyle
KyleOP10mo ago
I’ll check that version in a bit @bekacru If you saw my other bug report it’s with your adapter Debugging now, seems to be a issue with the /src/db/utils.ts and specifically:
const { kysely, databaseType } = await createKyselyAdapter(options);
if (!kysely) {
throw new BetterAuthError("Failed to initialize database adapter");
}
const { kysely, databaseType } = await createKyselyAdapter(options);
if (!kysely) {
throw new BetterAuthError("Failed to initialize database adapter");
}
If you allowed us to pass a adapter so basically you made a abstract version of this I could test it
bekacru
bekacru10mo ago
this should only happen if the adapter format isn't updated. Please make sure both the cli and the package is on latest.
Kyle
KyleOP10mo ago
I haven't updated since the first time I reported this bug, I was just forwarding what I put in from #Failed to initialize database adapter. I'll take a look later to see if it's fixed at all.
Sanjay Kholiya
Sanjay Kholiya10mo ago
Just tried with 0.8.7 beta 1, still not working
bekacru
bekacru10mo ago
Is it only signIn.email that doesn't work, or do none of the other APIs work either?
Sanjay Kholiya
Sanjay Kholiya10mo ago
None of them work, not even signin with google and signup with email
Kyle
KyleOP10mo ago
Nothing works for me either @bekacru and it won't due to the init.ts failing in that utils area.
bekacru
bekacru10mo ago
I think you have differnt issue yours is you can't make it generate the schema, right?
Kyle
KyleOP10mo ago
No the entire auth system doesn't work generate schema is apart of this issue as it gives the same errors
Kyle
KyleOP10mo ago
I mentioned this bc based on https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/init.ts the first line you do is const adapter = await getAdapter(options); which hooks into our options.
GitHub
better-auth/packages/better-auth/src/init.ts at main · better-auth/...
The most comprehensive authentication library for TypeScript - better-auth/better-auth
bekacru
bekacru10mo ago
okay so went to the full rabit hole. Drizzle itself isn't working for me without better auth. I cahnged the db connection to use pool
export const db = drizzle(new Pool({
connectionString: process.env.DATABASE_URL!,
}), { schema });
export const db = drizzle(new Pool({
connectionString: process.env.DATABASE_URL!,
}), { schema });
and it start working again
Sanjay Kholiya
Sanjay Kholiya10mo ago
tried this but not working for me, getting this error: ⨯ TypeError: dialect.createDriver is not a function at new Kysely (node_modules/.pnpm/[email protected]/node_modules/kysely/dist/esm/kysely.js:74:35) at Ee (node_modules/.pnpm/[email protected]/node_modules/better-auth/dist/index.js:83:20441) at jt (node_modules/.pnpm/[email protected]/node_modules/better-auth/dist/index.js:83:24510) at Mt (node_modules/.pnpm/[email protected]/node_modules/better-auth/dist/index.js:83:25114) at tc (node_modules/.pnpm/[email protected]/node_modules/better-auth/dist/index.js:83:27422) at <unknown> (src/lib/auth.ts:21:31) 72 | else { 73 | const dialect = args.dialect; > 74 | const driver = dialect.createDriver(); | ^ 75 | const compiler = dialect.createQueryCompiler(); 76 | const adapter = dialect.createAdapter(); 77 | const log = new Log(args.log ?? []); POST /api/auth/sign-in/social?currentURL=https%3A%2F%2Flocalhost%3A3000%2F 500 in 536ms
bekacru
bekacru10mo ago
hmm why is this a kysley error?
Kyle
KyleOP10mo ago
I'm stuck on 0.6.2 until I can figure out how to move to the newer version as I run a custom adapter that runs off Drizzzle
bekacru
bekacru10mo ago
are you using drizzle with pg?
Sanjay Kholiya
Sanjay Kholiya10mo ago
Not sure, I am using just drizzle Exactly this code
bekacru
bekacru10mo ago
no this would never through the above error check if you're using drizzle adapter in your auth config also make sure to import Pool from pg
Sanjay Kholiya
Sanjay Kholiya10mo ago
Oh, I actually directly called this from auth.ts replacing DrizzleAdapter completely, my bad. This is something I already had in my db.ts file which I am passing to DrizzleAdapter in auth.ts
Kyle
KyleOP10mo ago
i have two projects runing 0.6.2, both with drizzle, one on pg, and one is a custom adapter as you don't support the database type they both run into that error on the newer versions.
bekacru
bekacru10mo ago
yeah change that "Failed to initialize database adapter" only means one thing. the adapter wasn't a valid adapter meaning it didn't return a function
//this code was bypassed
if (typeof options.database === "function") {
return options.database(options);
}
//this code was bypassed
if (typeof options.database === "function") {
return options.database(options);
}
which should never happen unless there is a version mismatch or your custom adapter isn't quite correct for your custom adapter the new adapter interface is
(options: BetterAuthOptions)=>Adapter
(options: BetterAuthOptions)=>Adapter
it used to be just Adapter in older versions
Sanjay Kholiya
Sanjay Kholiya10mo ago
yeah, just tried this and it works!
Kyle
KyleOP10mo ago
I was using your base adapter code from the latest version, it was occuring with the default drizzle stuff too

Did you find this page helpful?