Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): Improve Prisma Client Initialization #9107

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

dpaulos6
Copy link
Contributor

@dpaulos6 dpaulos6 commented Sep 4, 2024

Description:

This PR refactors the Prisma Client initialization to use a more robust and concise singleton pattern. The changes ensure that only one instance of PrismaClient is created and reused throughout the application, which improves performance and avoids potential issues with multiple instances.

Changes:

  1. Singleton Pattern: Introduced a prismaClientSingleton function to create a single instance of PrismaClient.
  2. Global Scope Handling: Utilized globalThis to manage the global instance of PrismaClient, ensuring compatibility across different environments (Node.js, browser, etc.).
  3. Environment Check: Added a check to set the global instance only in non-production environments to avoid issues in serverless environments.

Benefits:

  • Performance Improvement: Ensures a single instance of PrismaClient is used, reducing overhead.
  • Code Cleanliness: The new implementation is more concise and easier to understand.
  • Future-Proofing: Using globalThis makes the code more robust and compatible with future JavaScript standards.

Testing:

  • Verified that the application initializes and uses a single instance of PrismaClient.
  • Ensured that the global instance is only set in development environments.

I was forced to come up with a different approach as it wasn't always working in my own monorepo. Please review the changes and provide feedback if you don't like anything. Thank you!

@dpaulos6 dpaulos6 requested a review from a team as a code owner September 4, 2024 15:15
@turbo-orchestrator turbo-orchestrator bot added area: examples Improvements or additions to examples needs: triage New issues get this label. Remove it after triage owned-by: turborepo labels Sep 4, 2024
Copy link

vercel bot commented Sep 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

6 Skipped Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Sep 5, 2024 8:49am

Copy link

vercel bot commented Sep 4, 2024

@dpaulos6 is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

@ijjk
Copy link
Member

ijjk commented Sep 4, 2024

Allow CI Workflow Run

  • approve CI run for commit: ad98e11

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@ijjk
Copy link
Member

ijjk commented Sep 4, 2024

Allow CI Workflow Run

  • approve CI run for commit: 97fc50f

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@dpaulos6 dpaulos6 changed the title feat: Improve Prisma Client Initialization feat(examples): Improve Prisma Client Initialization Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: examples Improvements or additions to examples needs: triage New issues get this label. Remove it after triage owned-by: turborepo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants