Skip to content

Mistake in example code #6804

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

Closed
Mosaab-Emam opened this issue Apr 6, 2025 · 0 comments
Closed

Mistake in example code #6804

Mosaab-Emam opened this issue Apr 6, 2025 · 0 comments
Assignees
Labels
docs Documentation creation, updates or corrections

Comments

@Mosaab-Emam
Copy link

Description

The following part of the documentation specifies how to create a prisma client that attaches to the global object:

Now, add the following code to your `lib/prisma.ts` file:
```ts file=lib/prisma.ts
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'
const globalForPrisma = global as unknown as { prisma: typeof prisma }
const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
export default prisma
```
This file creates a Prisma Client and attaches it to the global object so that only one instance of the client is created in your application. This helps resolve issues with hot reloading that can occur when using Prisma ORM with Next.js in development mode.

This code results in a prisma client with a type of any, as well as raising the following errors:

  1. 'prisma' is referenced directly or indirectly in its own type annotation. ts(2502)
  2. 'prisma' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer .ts(7022)

A fix should be made in line 289:

const globalForPrisma = global as unknown as { prisma: typeof prisma }

Where { prisma: typeof prisma } should become { prisma: typeof PrismaClient }.

This fix removes the errors as well as makes the exported client type safe.

@Mosaab-Emam Mosaab-Emam added the docs Documentation creation, updates or corrections label Apr 6, 2025
Mosaab-Emam added a commit to Mosaab-Emam/prisma-docs that referenced this issue Apr 6, 2025
Mosaab-Emam added a commit to Mosaab-Emam/prisma-docs that referenced this issue Apr 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation creation, updates or corrections
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants