Skip to content

multiple Prisma instances fix#66

Merged
callofcode07 merged 3 commits intomainfrom
db-fix
Feb 14, 2026
Merged

multiple Prisma instances fix#66
callofcode07 merged 3 commits intomainfrom
db-fix

Conversation

@Harish-Naruto
Copy link
Member

@Harish-Naruto Harish-Naruto commented Feb 14, 2026

Summary by CodeRabbit

  • Chores
    • Improved database connection handling for more reliable and efficient pooling and caching, reducing connection issues and resource usage.
    • Better development experience with stable module reloads and enhanced query/error logging in non-production for easier debugging.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 14, 2026

Warning

Rate limit exceeded

@Harish-Naruto has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 21 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📝 Walkthrough

Walkthrough

Replaces simple PrismaClient setup with a pg Pool + PrismaPg adapter, adds a non-production global cache for the Pool and Prisma client to persist across reloads, and exposes prisma as a named export while retaining existing generated exports. (43 words)

Changes

Cohort / File(s) Summary
Database Client Initialization
src/db/client.ts
Replaces direct PrismaClient initialization with a pooled pg Pool, creates PrismaPg(pool) adapter, conditionally configures logging by NODE_ENV, and caches pool and prisma on a global object in non-production. Adds export const prisma named export and preserves default/generated exports.

Sequence Diagram(s)

sequenceDiagram
participant Module as Importing Module
participant ClientFile as src/db/client.ts
participant Pool as pg Pool
participant Prisma as PrismaClient (PrismaPg adapter)

Module->>ClientFile: import { prisma } or default
ClientFile->>Pool: create or reuse global Pool
ClientFile->>Prisma: create or reuse PrismaClient with PrismaPg(pool)
ClientFile-->>Module: return prisma instance
Note right of Prisma: In non-production, both Pool and Prisma cached on global object
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • i-am-that-guy
  • Sherin-2711

Poem

🐰 I hopped through code, found pools to share,
Threads now sip safely from connections rare.
A cached little prisma, snug as can be,
Reused across reloads — hooray, that's me! 🥕✨

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main change—implementing pooled Prisma client initialization with global caching to prevent multiple instances, which is the core objective of the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch db-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@src/db/client.ts`:
- Around line 29-32: The environment check uses the wrong casing ("Production")
so it never matches NODE_ENV and incorrectly sets globalForPrisma.prisma and
globalForPrisma.pgPool in production; change the check to compare against the
lowercase "production" (or normalize with process.env.NODE_ENV?.toLowerCase())
so the caching block only runs when NODE_ENV !== "production" and avoids adding
references to globalThis in production.
- Around line 24-26: The NODE_ENV check in the DB client config uses
"Development" which won't match conventional lowercase env values; update the
check in the DB client setup (the process.env.NODE_ENV comparison used when
building the logger level array) to use the lowercase string "development" or,
better, compare case-insensitively (e.g., process.env.NODE_ENV?.toLowerCase()
=== "development") so verbose query logging actually enables in development
environments.
🧹 Nitpick comments (1)
src/db/client.ts (1)

11-27: Adapter is created even when prisma is served from cache.

When globalForPrisma.prisma already exists, the Pool and PrismaPg adapter instantiated on lines 11-17 are unused. Consider moving the pool, adapter, and client creation inside a single guard:

Suggested restructure
-const pool =
-  globalForPrisma.pgPool ??
-  new Pool({
-    connectionString: process.env.DATABASE_URL,
-  });
-
-const adapter = new PrismaPg(pool);
-
-export const prisma =
-  globalForPrisma.prisma ??
-  new PrismaClient({
-    adapter,
-    log:
-      process.env.NODE_ENV === "Development"
-        ? ["query", "error", "warn"]
-        : ["error"],
-  });
+const pool =
+  globalForPrisma.pgPool ??
+  new Pool({ connectionString: process.env.DATABASE_URL });
+
+export const prisma =
+  globalForPrisma.prisma ??
+  new PrismaClient({
+    adapter: new PrismaPg(pool),
+    log:
+      process.env.NODE_ENV === "development"
+        ? ["query", "error", "warn"]
+        : ["error"],
+  });

@callofcode07 callofcode07 merged commit 56e6288 into main Feb 14, 2026
3 checks passed
@callofcode07 callofcode07 deleted the db-fix branch February 14, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants