-
Notifications
You must be signed in to change notification settings - Fork 870
docs: update local development guide for Prisma 7 Accelerate #7421
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
Open
therbta
wants to merge
1
commit into
prisma:main
Choose a base branch
from
therbta:fix/prisma7-import-7372
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,12 @@ Accelerate does not work with a local database. However, in a development enviro | |
|
|
||
| The following steps outline how to use Prisma ORM and Prisma Accelerate with a local PostgreSQL database. | ||
|
|
||
| ### Prisma 7 | ||
|
|
||
| **Note:** In Prisma 7, the Accelerate extension requires a connection string starting with `prisma://` or `prisma+postgres://`. Using a local database connection string with the Accelerate extension will cause an error. | ||
|
|
||
| To use a local database in development with Prisma 7, use the `@prisma/adapter-pg` adapter conditionally: | ||
|
|
||
| 1. Update the `DATABASE_URL` environment variable with your local database's connection string: | ||
|
|
||
| ```.env | ||
|
|
@@ -41,16 +47,24 @@ The following steps outline how to use Prisma ORM and Prisma Accelerate with a l | |
|
|
||
| > Note: The `--no-engine` flag should only be used in preview and production environments. The command generates Prisma Client artifacts without a [Query Engine](/orm/more/under-the-hood/engines) file, which requires an Accelerate connection string. | ||
|
|
||
| 3. Set up Prisma Client with the Accelerate client extension: | ||
| 3. Set up Prisma Client with conditional logic for development and production: | ||
|
|
||
| ```typescript | ||
| import { PrismaClient } from '@prisma/client' | ||
| import { withAccelerate } from '@prisma/extension-accelerate' | ||
| import { PrismaPg } from '@prisma/adapter-pg' | ||
| import { Pool } from 'pg' | ||
|
|
||
| const pool = new Pool({ connectionString: process.env.DATABASE_URL }) | ||
|
|
||
| const adapter = process.env.ACCELERATE_URL | ||
| ? withAccelerate() // Production: Use Accelerate | ||
| : new PrismaPg(pool) // Development: Use pg adapter | ||
|
|
||
| const prisma = new PrismaClient().$extends(withAccelerate()) | ||
| const prisma = new PrismaClient().$extends(adapter) | ||
| ``` | ||
|
|
||
| > The extended instance of Prisma Client will use the local database. Hence, Prisma Accelerate will not be used in your development environment to respond to your Prisma Client queries. | ||
| > In production, set `ACCELERATE_URL` to your Prisma Accelerate connection string. In development, omit this environment variable to use the local database via the pg adapter. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Clarify environment variable configuration. The note mentions setting
Consider expanding this note to be more explicit:
🤖 Prompt for AI Agents |
||
|
|
||
|  | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
Prisma 7 withAccelerate extension API documentation💡 Result:
You can find the Prisma 7 withAccelerate extension API docs and usage examples here:
Minimal usage example:
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'
const prisma = new PrismaClient().$extends(withAccelerate())
await prisma.user.findMany({ cacheStrategy: { ttl: 30, swr: 60 } })
Sources:
[1] Prisma Docs — Accelerate overview & examples.
[2] Prisma Docs — Guides: Neon + Accelerate (setup & generate client).
[3] npm — @prisma/extension-accelerate package (install and README).
🌐 Web query:
Prisma 7 PrismaPg driver adapter API constructor vs extends💡 Result:
Short answer: Prisma 7 requires passing a driver adapter (e.g. PrismaPg) into PrismaClient. You can either instantiate PrismaClient with the adapter, or subclass/extend PrismaClient and forward the adapter to super — both are equivalent; the difference is just where you construct/hold the adapter and how you type the constructor.
Examples and tips
import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new PrismaClient({ adapter })
(Prisma docs show this pattern). [1][2]
Define an options interface that includes adapter, then forward to super. You may need to cast to satisfy TypeScript because PrismaClient's built-in options don't declare the adapter property yet:
import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
interface CustomPrismaOptions extends Omit<Prisma.PrismaClientOptions, 'datasources'> {
adapter: PrismaPg
}
class CustomPrismaClient extends PrismaClient {
constructor(options: CustomPrismaOptions) {
super(options as unknown as Prisma.PrismaClientOptions) // forward adapter to base ctor
}
}
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
const prisma = new CustomPrismaClient({ adapter })
Notes
Sources
[1] Prisma docs — Database drivers / PostgreSQL driver + adapter examples.
[2] Prisma discussion and examples showing extending PrismaClient with PrismaPg adapter.
🌐 Web query:
Prisma Accelerate datasourceUrl connection configuration💡 Result:
Short summary — how to configure Prisma Accelerate connection in your schema:
Use the Accelerate HTTP connection string as your datasource url (env var). Example .env and schema:
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=YOUR_API_KEY"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
(Prisma will route queries through Accelerate.) [1][2][3]
If you need Prisma CLI commands that require a direct DB TCP connection (migrate, introspect, Studio), add a direct connection env var and set directUrl in the datasource:
DIRECT_DATABASE_URL="postgresql://user:pass@host:port/db?schema=public"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_DATABASE_URL")
}
Migrations/introspection use directUrl while runtime uses the accelerate url. [1][2][4]
Notes/tips:
Sources: Prisma Accelerate docs and connection-URL/schema references. [1][2][3][4]
Critical: Driver adapter and extension patterns are incompatible in this code.
The code mixes two fundamentally different Prisma APIs:
withAccelerate()returns a Prisma Client extension (used with.$extends())new PrismaPg(pool)returns a driver adapter (required in thePrismaClientconstructor)These cannot be used interchangeably. The code will fail at runtime in development because driver adapters must be passed to the PrismaClient constructor, not to
.$extends().The correct patterns are:
new PrismaClient().$extends(withAccelerate())new PrismaClient({ adapter: new PrismaPg(pool) })Rewrite the example to conditionally instantiate PrismaClient differently for each case—not to apply the same
.$extends()call to both. Also add the missing installation step for@prisma/adapter-pgandpg.🤖 Prompt for AI Agents