You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a guide on how to initialize a database client:
//lib/db.tsimport{drizzle}from"drizzle-orm/node-postgres";// You can use cache from react to cache the client during the same request// this is not mandatory and only has an effect for server componentsimport{cache}from"react";import*asschemafrom"./schema/pg";import{Pool}from"pg";exportconstgetDb=cache(()=>{constpool=newPool({connectionString: process.env.PG_URL,// You don't want to reuse the same connection for multiple requestsmaxUses: 1,});returndrizzle({client: pool, schema });});
However, the comment notes that cache only works for server components—not for route handlers. In route handlers, we may need to call getDb() multiple times. If each call creates a new DB client, it can lead to significant performance issues.
Describe the solution you'd like
A better way to cache the DB client in route handlers.
Ideally, a universal approach that handles per-request DB client caching consistently across both server components and route handlers.
Describe alternatives you've considered
One workaround is to initialize the DB client at the beginning of the route handler and pass it manually to all functions that need it. However, this is cumbersome and requires a lot of refactoring when migrating from existing Next.js projects.
@opennextjs/cloudflare version
1.0.0-beta.4
Additional context
No response
Before submitting
I have checked that there isn't already a similar feature request
This is a single feature (not multiple features in one request)
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem?
On this page: https://opennext.js.org/cloudflare/howtos/db
There is a guide on how to initialize a database client:
However, the comment notes that cache only works for server components—not for route handlers. In route handlers, we may need to call getDb() multiple times. If each call creates a new DB client, it can lead to significant performance issues.
Describe the solution you'd like
A better way to cache the DB client in route handlers.
Ideally, a universal approach that handles per-request DB client caching consistently across both server components and route handlers.
Describe alternatives you've considered
One workaround is to initialize the DB client at the beginning of the route handler and pass it manually to all functions that need it. However, this is cumbersome and requires a lot of refactoring when migrating from existing Next.js projects.
@opennextjs/cloudflare version
1.0.0-beta.4
Additional context
No response
Before submitting
The text was updated successfully, but these errors were encountered: