BetterAuth+Prisma given Too many Connections error

Hey, i've been having an issue with better auth. i tend to get too many connections error
Invalid `db[getModelName(model)].findFirst()` invocation in
/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:168:52

165 `Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`
166 );
167 }
→ 168 const result = await db[getModelName(model)].findFirst(
Too many database connections opened: FATAL: sorry, too many clients already
2025-04-13T15:29:21.905Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR PrismaClientInitializationError:
Invalid `db[getModelName(model)].findFirst()` invocation in
/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:168:52

165 `Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`
166 );
167 }
→ 168 const result = await db[getModelName(model)].findFirst(
Too many database connections opened: FATAL: sorry, too many clients already

Networking has been disabled
Invalid `db[getModelName(model)].findFirst()` invocation in
/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:168:52

165 `Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`
166 );
167 }
→ 168 const result = await db[getModelName(model)].findFirst(
Too many database connections opened: FATAL: sorry, too many clients already
2025-04-13T15:29:21.905Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR PrismaClientInitializationError:
Invalid `db[getModelName(model)].findFirst()` invocation in
/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:168:52

165 `Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`
166 );
167 }
→ 168 const result = await db[getModelName(model)].findFirst(
Too many database connections opened: FATAL: sorry, too many clients already

Networking has been disabled
and i normally don't get this before Versions are "@better-auth/expo": "^1.2.6-beta.8", and "better-auth": "1.2.6-beta.6", Auth.ts
import { db } from '@/prisma/db';

export const auth = betterAuth({
database: prismaAdapter(db, {
provider: 'postgresql',
}),
import { db } from '@/prisma/db';

export const auth = betterAuth({
database: prismaAdapter(db, {
provider: 'postgresql',
}),
Prisma/db.ts
import { Prisma, PrismaClient } from '@/prisma/generated/prisma';


const db = new PrismaClient({
log:
process.env.EXPO_PUBLIC_ENV === 'development'
? ['query', 'error', 'warn']
: ['error'],
});

export const db;
import { Prisma, PrismaClient } from '@/prisma/generated/prisma';


const db = new PrismaClient({
log:
process.env.EXPO_PUBLIC_ENV === 'development'
? ['query', 'error', 'warn']
: ['error'],
});

export const db;
18 Replies
KiNFiSH
KiNFiSH2w ago
you should cache the conn like this
import { PrismaClient } from "@prisma/client";

declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== "production") {
global.db = db;
}

export default db;
import { PrismaClient } from "@prisma/client";

declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== "production") {
global.db = db;
}

export default db;
this creates a single shared PrismaClient instance to prevent multiple database connections. It stores the instance globally in development to survive hot-reloads while keeping just one connection in production.
ak11
ak11OP2w ago
ok would try and let you know
ak11
ak11OP2w ago
hey @KiNFiSH . i've the change. when i try signing in to my app an error occurs ``````
KiNFiSH
KiNFiSH2w ago
can you do the generation and push once again can u send me schema.prisma file ? i want to see where the generated config are actually stored ?
ak11
ak11OP2w ago
ok on it they are stored at "./generated/prisma"
KiNFiSH
KiNFiSH2w ago
so you should update your prisma client import like
import { Prisma , PrismaClient } from 'generated/prisma';
// create a client here
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
import { Prisma , PrismaClient } from 'generated/prisma';
// create a client here
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
i mean prisma should be right from where you generate the client
ak11
ak11OP2w ago
oh but there's no "prisma" coming from the grenerated file just Prisma and PrismaClient
KiNFiSH
KiNFiSH2w ago
so you that to create a client
ak11
ak11OP2w ago
i did that a while back but was worried it would create multiple connections
KiNFiSH
KiNFiSH2w ago
wait lemme batch thme up
import { PrismaClient } from "generated/prisma;

declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== "production") {
global.db = db;
}

export default db;
import { PrismaClient } from "generated/prisma;

declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== "production") {
global.db = db;
}

export default db;
no prisma cleint is updted with in the db instead of creating a client on the auth config now pass the db to the adapter as you were doing
ak11
ak11OP2w ago
that's what i did. in my db.ts This is my imports
import { Prisma, PrismaClient } from '@/prisma/generated/prisma';
import { Prisma, PrismaClient } from '@/prisma/generated/prisma';
Then
declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== ‘production’) {
global.db = db;
}

export default db;
declare global {
var db: PrismaClient | undefined;
}

const db = global.db || new PrismaClient();

if (process.env.NODE_ENV !== ‘production’) {
global.db = db;
}

export default db;
KiNFiSH
KiNFiSH2w ago
but now you cache the connection thats why the global is introduced make sure to use the generrated client to create prisma client.
ak11
ak11OP2w ago
ok let me send my full db.ts. as well as my auth.ts
ak11
ak11OP2w ago
ak11
ak11OP2w ago
error
2025-04-14T05:29:47.329Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR TypeError: Cannot read properties of undefined (reading 'session')
at Object.findOne (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:163:14)
at Object.findSession (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/shared/better-auth.BGUZBbM4.cjs:345:37)
at /Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/shared/better-auth.BDYXUcLv.cjs:389:57
at internalHandler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:606:22)
at api.<computed> (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/api/index.cjs:484:22)
at processRequest (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:4894:24)
at handler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:4914:19)
at handler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/@expo/server/src/index.ts:197:17)
at /Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/@expo/server/src/vendor/http.ts:30:24
2025-04-14T05:29:47.360Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR TypeError: Cannot read properties of undefined (reading 'session')
at Object.findOne (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:163:14)

2025-04-14T05:29:47.329Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR TypeError: Cannot read properties of undefined (reading 'session')
at Object.findOne (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:163:14)
at Object.findSession (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/shared/better-auth.BGUZBbM4.cjs:345:37)
at /Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/shared/better-auth.BDYXUcLv.cjs:389:57
at internalHandler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:606:22)
at api.<computed> (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/api/index.cjs:484:22)
at processRequest (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:4894:24)
at handler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-call/dist/index.cjs:4914:19)
at handler (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/@expo/server/src/index.ts:197:17)
at /Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/@expo/server/src/vendor/http.ts:30:24
2025-04-14T05:29:47.360Z ERROR [Better Auth]: INTERNAL_SERVER_ERROR TypeError: Cannot read properties of undefined (reading 'session')
at Object.findOne (/Users/mac/Documents/workspace/business-copilot/business-copilot-webapp/node_modules/better-auth/dist/adapters/prisma-adapter/index.cjs:163:14)

KiNFiSH
KiNFiSH2w ago
where is db import ? in auth config
ak11
ak11OP2w ago
sorry. my bad. i was putting
import {db} from '@/prisma/db';
import {db} from '@/prisma/db';
and not
import db from '@/prisma/db';
import db from '@/prisma/db';
that error has gone silly me i would update you further if i still get "too many connections error" Thank you for your assistance

Did you find this page helpful?