Skip to content

⚡ Bolt: Implement Prisma Client singleton#10

Merged
davidraehles merged 3 commits intomainfrom
bolt-prisma-singleton-1542024686322854175
Feb 18, 2026
Merged

⚡ Bolt: Implement Prisma Client singleton#10
davidraehles merged 3 commits intomainfrom
bolt-prisma-singleton-1542024686322854175

Conversation

@davidraehles
Copy link
Copy Markdown
Collaborator

💡 What: This optimization centralizes the Prisma Client into a singleton instance located in backend/src/lib/prisma.ts. Previously, multiple new PrismaClient() calls were present in different route files, each creating its own connection pool. Additionally, pre-existing TypeScript errors in notes.ts were fixed to ensure the project builds correctly with Express 5.

🎯 Why: Creating multiple Prisma Client instances leads to multiple database connection pools. This is inefficient as it consumes unnecessary memory on the server and can quickly lead to database connection exhaustion, especially under load or in environments with connection limits.

📊 Measured Improvement: While a numerical baseline could not be established due to the lack of a test database and load-testing tools in the environment, the architectural benefit is well-documented. By moving to a singleton, we ensure that only one connection pool is created for the entire application, which is the recommended best practice for Prisma in Node.js applications. This change reduces memory overhead and prevents potential database connection leaks. Verified that new PrismaClient() now only appears once in the codebase and that the backend builds successfully.


PR created automatically by Jules for task 1542024686322854175 started by @davidraehles

- Centralized Prisma Client instantiation in `backend/src/lib/prisma.ts`.
- Updated `auth.ts` and `notes.ts` routes to use the shared singleton instance.
- Fixed pre-existing TypeScript errors in `notes.ts` caused by Express 5's broad typing of `req.params`.
- Documented performance learnings in `.jules/bolt.md`.

Co-authored-by: davidraehles <6085055+davidraehles@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 18, 2026 19:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements a Prisma Client singleton pattern to prevent multiple database connection pools from being created across different route files. The changes consolidate all Prisma Client instantiation into a single module (backend/src/lib/prisma.ts) and update route files to import this shared instance. Additionally, TypeScript compilation errors in Express 5 route handlers were fixed by explicitly casting req.params values to string.

Changes:

  • Created centralized Prisma Client singleton in backend/src/lib/prisma.ts
  • Updated auth.ts and notes.ts routes to use the singleton instance
  • Fixed Express 5 TypeScript typing issues by casting route parameters to string
  • Added pnpm lockfile for dependency management

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
backend/src/lib/prisma.ts New file introducing Prisma Client singleton pattern
backend/src/routes/notes.ts Replaced local PrismaClient instantiation with singleton import; fixed TypeScript type casting for req.params.id
backend/src/routes/auth.ts Replaced local PrismaClient instantiation with singleton import
backend/pnpm-lock.yaml Added pnpm lockfile for dependency management
.jules/bolt.md Added documentation explaining the rationale for Prisma singleton and Express 5 type casting
Files not reviewed (1)
  • backend/pnpm-lock.yaml: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/pnpm-lock.yaml Outdated
@@ -0,0 +1,1443 @@
lockfileVersion: '9.0'
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

This PR adds a pnpm-lock.yaml file, but backend/package-lock.json still exists in the repository. Having both npm and pnpm lockfiles can lead to dependency conflicts and inconsistent installations. You should remove package-lock.json and commit that deletion in this PR to ensure the project uses pnpm exclusively.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed pnpm-lock.yaml from the branch.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have removed the backend/pnpm-lock.yaml file from the branch as requested.

Comment thread backend/src/lib/prisma.ts Outdated
Comment on lines +3 to +4
const prisma = new PrismaClient();

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

The Prisma Client singleton implementation is missing recommended development environment handling. According to Prisma best practices, in development mode with hot-reloading (nodemon), you should prevent creating multiple instances by caching the client on the global object. This prevents "too many clients" errors during development. Consider implementing the pattern recommended in Prisma's documentation that checks for globalThis.prisma and reuses it in development environments.

Suggested change
const prisma = new PrismaClient();
declare global {
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}
const prisma = globalThis.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalThis.prisma = prisma;
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Implemented the standard Prisma singleton pattern with globalThis caching for development environments to prevent multiple client instances during hot reload.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've updated the Prisma singleton implementation to use the standard pattern with globalThis caching. This will prevent multiple instances from being created during hot reloads in development.

Comment thread backend/src/lib/prisma.ts
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

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

Consider adding graceful shutdown handling for the Prisma Client. When the application terminates, the database connections should be properly closed using prisma.$disconnect(). This should be added to the server.ts file with process event handlers for SIGINT and SIGTERM to ensure clean shutdowns and prevent connection leaks.

Suggested change
if (typeof process !== 'undefined' && typeof process.on === 'function') {
const gracefulShutdown = async (signal: string) => {
try {
await prisma.$disconnect();
} catch (error) {
console.error(`Error disconnecting PrismaClient on ${signal}:`, error);
} finally {
// Let the process exit naturally after cleanup
}
};
process.on('beforeExit', () => {
gracefulShutdown('beforeExit');
});
process.on('SIGINT', () => {
gracefulShutdown('SIGINT').finally(() => {
process.exit(0);
});
});
process.on('SIGTERM', () => {
gracefulShutdown('SIGTERM').finally(() => {
process.exit(0);
});
});
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Added graceful shutdown handlers for SIGINT and SIGTERM that disconnect the Prisma client before process exit.

OpenCode and others added 2 commits February 18, 2026 21:15
- Updated Prisma singleton to use globalThis caching for dev hot-reloads.
- Removed backend/pnpm-lock.yaml to keep the PR focused.
- Fixed TypeScript errors in notes.ts routes.

Co-authored-by: davidraehles <6085055+davidraehles@users.noreply.github.com>
@davidraehles davidraehles merged commit 761d2a1 into main Feb 18, 2026
davidraehles added a commit that referenced this pull request Feb 18, 2026
…6-7-8-10-11-12-13-14

Merge PRs #1, #2, #3, #4, #5, #6, #7, #8, #10, #11, #12, #13, #14 into main
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.

3 participants