Next.js Discord

Discord Forum

Database Connections

Unanswered
Africanized honey bee posted this in #help-forum
Open in Discord
Africanized honey beeOP
I'm running into two issues. In a lib/db.ts file I declare a single database connection using node-postgres and drizzle. In my development server, it constantly creates new clients and no matter what singleton pattern I've attempted, none seem to prevent the destruction and recreation of a new client which will eventually max out my postgres database. Secondly, I actually need to be creating multiple additional db connections based off of the results from the first one. Ideally, they would be exported in a way like export const dbs: Record<number, PostgresJsDatabase> so that I can access the one that is needed in my react server components. Is this possible? What are the recommendations / solutions for this situation. Thanks!

6 Replies

Boreal Chickadee
did you find any solution to that? I'm having the same problem 😫
Boreal Chickadee
other people are experiencing it too, I believe this is the main issue, at least for me:
https://github.com/vercel/next.js/issues/45483
still seems unresolved
Hello, @Africanized honey bee can you share singleton patterns that you were trying to use please?
Cape lion
I actually have a smiliar issue but with higher order functions that keep local state/do caching... Instead of reusing the same export, the file which defines the export is evaluated over and over again...
Cape lion
Should I think of a nextjs app similar to a php app where everything is reevaluated on every request?
@Cape lion Should I think of a nextjs app similar to a php app where everything is reevaluated on every request?
no, it's not - there is already working solution with prisma for example, I would try to adapt it to drizzle in freetime as I don't have any spare postgres currently on local 😄