Skip to content

Commit e4bcf8f

Browse files
ArthurGambyArthur Gamby
andauthored
feat(docs): update nextjs ai prompt guide from prisma 6 to prisma 7 (#7356)
Co-authored-by: Arthur Gamby <[email protected]>
1 parent f21ef66 commit e4bcf8f

File tree

1 file changed

+172
-67
lines changed

1 file changed

+172
-67
lines changed

content/900-ai/prompts/nextjs.mdx

Lines changed: 172 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ description: 'Step-by-step guide for integrating Prisma ORM and Prisma Postgres
55
sidebar_label: 'NextJS + Prisma'
66
---
77

8+
## Prerequisites
9+
10+
Before using this prompt, you need to create a new Next.js project:
11+
12+
```terminal
13+
npx create-next-app@latest my-app
14+
cd my-app
15+
```
16+
17+
When prompted, select the following recommended options:
18+
- **TypeScript**: Yes
19+
- **ESLint**: Yes
20+
- **Tailwind CSS**: Yes (optional)
21+
- **`src/` directory**: No
22+
- **App Router**: Yes
23+
- **Turbopack**: Yes (optional)
24+
- **Import alias**: Use default (`@/*`)
25+
26+
Once your Next.js project is created, you can use the prompt below with your AI assistant to add Prisma and Prisma Postgres.
27+
828
## How to use
929

1030
Include this prompt in your AI assistant to guide consistent code generation for NextJS + Prisma + Prisma Postgres projects.
@@ -39,14 +59,21 @@ description: Guidelines for writing Next.js apps with Prisma Postgres
3959
alwaysApply: false
4060
---
4161

42-
# Bootstrap Next.js app with Prisma Postgres
62+
# Bootstrap Next.js app with Prisma Postgres (Prisma 7)
63+
64+
> **Note**: This guide is updated for **Prisma ORM 7**. Key changes from earlier versions:
65+
> - `engine` property removed from `prisma.config.ts`
66+
> - `url` removed from datasource in `schema.prisma` (now only in `prisma.config.ts`)
67+
> - Use `@prisma/adapter-pg` driver adapter for direct TCP connections
68+
> - `--no-engine` flag is no longer required for `prisma generate`
69+
> - Requires Node.js 20.19+ and TypeScript 5.4.0+
4370

4471
## Overview of implementing Prisma with Next.js
4572

4673
1. Install Prisma and required dependencies (including dotenv)
4774
2. Initialize Prisma and configure schema
4875
3. Configure dotenv for environment variables
49-
4. Create global Prisma client instance with Accelerate
76+
4. Create global Prisma client instance with Pg Adapter
5077
5. Add npm scripts for testing and database management
5178
6. Create test script to verify setup
5279
7. Use Prisma client in API routes and pages with proper error handling
@@ -67,9 +94,29 @@ import { PrismaClient } from '@prisma/client' // ❌ BREAKS APPLICATION
6794
// ❌ WRONG IMPORT PATH - MISSING /client - IT WILL BREAK THE APPLICATION
6895
import { PrismaClient } from "../app/generated/prisma" // ❌ BREAKS APPLICATION
6996

70-
// ❌ NEVER CREATE CLIENT WITHOUT ACCELERATE - IT WILL BREAK IN PRODUCTION
71-
const prisma = new PrismaClient() // ❌ BREAKS APPLICATION
97+
// ❌ NEVER USE ACCELERATE - IT IS NOT USED IN THIS WORKFLOW
98+
import { withAccelerate } from "@prisma/extension-accelerate" // ❌ BREAKS APPLICATION
7299

100+
// ❌ NEVER USE accelerateUrl - IT WILL BREAK THE APPLICATION
101+
const prisma = new PrismaClient({
102+
accelerateUrl: process.env.DATABASE_URL, // ❌ BREAKS APPLICATION - use adapter
103+
})
104+
105+
// ❌ NEVER include url in datasource block - IT WILL BREAK THE APPLICATION
106+
datasource db {
107+
provider = "postgresql"
108+
url = env("DATABASE_URL") // ❌ BREAKS APPLICATION - moved to prisma.config.ts
109+
}
110+
111+
// ❌ NEVER include engine property - IT WILL BREAK THE APPLICATION
112+
export default defineConfig({
113+
engine: "classic", // ❌ BREAKS APPLICATION - removed in Prisma 7
114+
})
115+
116+
// ❌ NEVER use Prisma Postgres HTTP URLs - ONLY use TCP URLs
117+
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/..." // ❌ BREAKS APPLICATION - use postgres://
118+
// ✅ ALWAYS use standard TCP URLs:
119+
DATABASE_URL="postgres://..." // ✅ CORRECT
73120
```
74121

75122
Instead, you MUST ALWAYS generate ONLY this pattern:
@@ -83,13 +130,17 @@ generator client {
83130

84131
// ✅ CRITICAL: MUST include /client at the end of import path
85132
import { PrismaClient } from "../app/generated/prisma/client"
86-
import { withAccelerate } from "@prisma/extension-accelerate"
133+
import { PrismaPg } from "@prisma/adapter-pg"
134+
135+
const adapter = new PrismaPg({
136+
connectionString: process.env.DATABASE_URL!,
137+
})
87138

88139
const globalForPrisma = global as unknown as { prisma: PrismaClient }
89140

90141
const prisma = globalForPrisma.prisma || new PrismaClient({
91-
datasourceUrl: process.env.DATABASE_URL,
92-
}).$extends(withAccelerate())
142+
adapter,
143+
})
93144

94145
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
95146

@@ -100,54 +151,84 @@ export default prisma
100151

101152
1. You MUST use `provider = "prisma-client"` (not "prisma-client-js")
102153
2. You MUST use custom output: `output = "../app/generated/prisma"`
103-
3. You MUST use Accelerate extension with `withAccelerate()` when using Prisma Postgres
154+
3. You MUST use `@prisma/adapter-pg` driver adapter
104155
4. You MUST create `lib/prisma.ts` as a global singleton instance
105156
5. You MUST wrap all database calls in try-catch blocks
106157
6. You MUST import from `'../app/generated/prisma/client'` (not `'@prisma/client'` or `'../app/generated/prisma'`)
107-
7. You MUST use `process.env.DATABASE_URL` in Next.js
158+
7. You MUST use `adapter` property in PrismaClient constructor
108159
8. You MUST install `dotenv` and add `import "dotenv/config"` to `prisma.config.ts`
109160
9. You MUST add npm scripts for `db:test` and `db:studio` to package.json
110161
10. You MUST create a test script at `scripts/test-database.ts` to verify setup
162+
11. You MUST NOT include `url` in the datasource block of `schema.prisma`
163+
12. You MUST NOT include `engine` property in `prisma.config.ts`
164+
13. You MUST use `npx prisma init --db --output ../app/generated/prisma` to create a real cloud database
165+
14. You MUST use standard TCP URLs (`postgres://...`) in .env
166+
15. You MUST NOT use `accelerateUrl` or `withAccelerate`
167+
168+
## VERSION REQUIREMENTS
169+
170+
- **Node.js**: 20.19 or higher (Node.js 18 is NOT supported)
171+
- **TypeScript**: 5.4.0 or higher (5.9.x recommended)
172+
- **Prisma**: 7.0.0 or higher
111173

112174
## CORRECT INSTALLATION
113175

114176
```bash
115177
# Dev dependencies
116178
npm install prisma tsx --save-dev
117179

118-
# Production dependencies
119-
npm install @prisma/extension-accelerate @prisma/client dotenv
180+
# Production dependencies
181+
npm install @prisma/adapter-pg @prisma/client dotenv
120182
```
121183

122184
## CORRECT PRISMA INITIALIZATION
123185

186+
> **FOR AI ASSISTANTS**: This command is **interactive** and requires user input. You **MUST ask the user to run this command manually** in their own terminal, then **wait for them to confirm completion** before proceeding with the next steps. Do NOT attempt to run this command yourself.
187+
124188
```bash
125-
# Initialize Prisma (creates prisma/schema.prisma and prisma.config.ts)
126-
npx prisma init
189+
# Initialize Prisma AND create a real Prisma Postgres cloud database
190+
npx prisma init --db --output ../app/generated/prisma
127191
```
128192

129-
**IMPORTANT**: The init command will create `prisma.config.ts`. You MUST update it to load environment variables.
193+
This command:
194+
- Authenticates you with Prisma Console (if needed)
195+
- Prompts for **region** and **project name**
196+
- **Creates a cloud Prisma Postgres database**
197+
- Generates:
198+
- `prisma/schema.prisma` (with correct output path)
199+
- `prisma.config.ts` (with dotenv import)
200+
- **`.env` with a `DATABASE_URL`**
201+
202+
**IMPORTANT**: Ensure the generated `.env` uses a `postgres://` URL scheme. If it generates `prisma+postgres://`, replace it with the standard TCP connection string available in the Prisma Console.
203+
204+
```env
205+
DATABASE_URL="postgres://..."
206+
```
207+
208+
**IMPORTANT**: Do NOT use `npx prisma init` without `--db` as this only creates local files without a database.
130209

131210
## CORRECT PRISMA CONFIG (prisma.config.ts)
132211

133-
**CRITICAL**: After running `npx prisma init`, update the generated `prisma.config.ts`:
212+
When using `npx prisma init --db`, the `prisma.config.ts` is **auto-generated** with the correct configuration:
134213

135214
```typescript
136-
import "dotenv/config" // ✅ CRITICAL: Add this line at the top
215+
import "dotenv/config" // ✅ Auto-included by prisma init --db
137216
import { defineConfig, env } from "prisma/config"
138217

139218
export default defineConfig({
140219
schema: "prisma/schema.prisma",
141220
migrations: {
142221
path: "prisma/migrations",
143222
},
144-
engine: "classic",
223+
// ✅ NO engine property - removed in Prisma 7
145224
datasource: {
146225
url: env("DATABASE_URL"),
147226
},
148227
})
149228
```
150229

230+
**Note**: If you need to manually create this file, ensure `import "dotenv/config"` is at the top.
231+
151232
## CORRECT SCHEMA CONFIGURATION (prisma/schema.prisma)
152233

153234
Update the generated `prisma/schema.prisma` file:
@@ -160,7 +241,7 @@ generator client {
160241

161242
datasource db {
162243
provider = "postgresql"
163-
url = env("DATABASE_URL")
244+
// ✅ NO url here - now configured in prisma.config.ts
164245
}
165246

166247
// Example User model for testing
@@ -179,13 +260,17 @@ Create `lib/prisma.ts` file:
179260

180261
```typescript
181262
import { PrismaClient } from "../app/generated/prisma/client" // ✅ CRITICAL: Include /client
182-
import { withAccelerate } from "@prisma/extension-accelerate"
263+
import { PrismaPg } from "@prisma/adapter-pg"
264+
265+
const adapter = new PrismaPg({
266+
connectionString: process.env.DATABASE_URL!,
267+
})
183268

184269
const globalForPrisma = global as unknown as { prisma: PrismaClient }
185270

186271
const prisma = globalForPrisma.prisma || new PrismaClient({
187-
datasourceUrl: process.env.DATABASE_URL,
188-
}).$extends(withAccelerate())
272+
adapter,
273+
})
189274

190275
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
191276

@@ -350,31 +435,59 @@ User should follow these steps (AI should provide these instructions):
350435
1. **Install dependencies**:
351436
```bash
352437
npm install prisma tsx --save-dev
353-
npm install @prisma/extension-accelerate @prisma/client dotenv
438+
npm install @prisma/adapter-pg @prisma/client dotenv
354439
```
355440

356-
2. **Initialize Prisma**:
441+
2. **Initialize Prisma AND create Prisma Postgres database** (⚠️ USER MUST RUN MANUALLY):
442+
443+
> **AI ASSISTANT**: Ask the user to run this command in their own terminal. This is interactive and requires user input. Wait for the user to confirm completion before continuing.
444+
357445
```bash
358-
npx prisma init
446+
npx prisma init --db --output ../app/generated/prisma
359447
```
448+
449+
The user should follow the terminal prompts to:
450+
- Authenticate with Prisma Console (if needed)
451+
- Choose a region (e.g., us-east-1)
452+
- Name your project
453+
454+
Once complete, this creates `prisma/schema.prisma`, `prisma.config.ts`, AND `.env` with the `DATABASE_URL`.
455+
456+
**User should confirm when done** so the AI can proceed with the next steps.
360457

361-
3. **Update `prisma.config.ts`** - Add `import "dotenv/config"` at the top
362-
363-
4. **Update `prisma/schema.prisma`** - Set provider to "prisma-client" and output path
458+
3. **Verify `.env` was created** - Ensure `DATABASE_URL` uses `postgres://`. If it uses `prisma+postgres://`, change it to the TCP connection string.
459+
```env
460+
DATABASE_URL="postgres://..."
461+
```
462+
**Do NOT invent or manually change this URL. Use the one from Prisma Console.**
463+
464+
4. **Update `prisma/schema.prisma`** - Add the User model (generator and datasource are already configured):
465+
```prisma
466+
model User {
467+
id Int @id @default(autoincrement())
468+
email String @unique
469+
name String?
470+
createdAt DateTime @default(now())
471+
updatedAt DateTime @updatedAt
472+
}
473+
```
364474

365-
5. **Create `lib/prisma.ts`** with correct import path including `/client`
475+
5. **Create `lib/prisma.ts`** with correct import path including `/client` and using `@prisma/adapter-pg`.
366476

367477
6. **Add npm scripts** to `package.json` for `db:test` and `db:studio`
368478

369479
7. **Create `scripts/test-database.ts`** test script
370480

371-
8. **Create `.env`** file with DATABASE_URL (user provides this from Prisma Console)
372-
373-
9. **Push schema to database**:
481+
8. **Push schema to database**:
374482
```bash
375483
npx prisma db push
376484
```
377485

486+
9. **Generate Prisma Client**:
487+
```bash
488+
npx prisma generate
489+
```
490+
378491
10. **Test the setup**:
379492
```bash
380493
npm run db:test
@@ -394,10 +507,16 @@ Before generating any code, you MUST verify:
394507
3. Are you importing from `'../app/generated/prisma/client'` (with `/client`)? If not, STOP and FIX.
395508
4. Did you add `import "dotenv/config"` to `prisma.config.ts`? If not, STOP and FIX.
396509
5. Did you add `import "dotenv/config"` to `scripts/test-database.ts`? If not, STOP and FIX.
397-
6. Are you using `withAccelerate()` extension? If not, STOP and FIX.
398-
7. Are you wrapping database operations in try-catch? If not, STOP and FIX.
399-
8. Did you create the test script at `scripts/test-database.ts`? If not, STOP and FIX.
400-
9. Did you add `db:test` and `db:studio` scripts to package.json? If not, STOP and FIX.
510+
6. Are you using `@prisma/adapter-pg`? If not, STOP and FIX.
511+
7. Are you using `adapter` property in PrismaClient constructor? If not, STOP and FIX.
512+
8. Are you wrapping database operations in try-catch? If not, STOP and FIX.
513+
9. Did you create the test script at `scripts/test-database.ts`? If not, STOP and FIX.
514+
10. Did you add `db:test` and `db:studio` scripts to package.json? If not, STOP and FIX.
515+
11. Did you remove `url` from the datasource block in `schema.prisma`? If not, STOP and FIX.
516+
12. Did you remove `engine` property from `prisma.config.ts`? If not, STOP and FIX.
517+
13. Are you using `npx prisma init --db` (not just `npx prisma init`)? If not, STOP and FIX.
518+
14. Is the DATABASE_URL a TCP URL (`postgres://...`)? If it's a `prisma+postgres://` URL, STOP and FIX.
519+
15. Did Prisma generate the `.env` file? If you invented the URL manually, STOP and FIX.
401520

402521
## CONSEQUENCES OF INCORRECT IMPLEMENTATION
403522

@@ -408,8 +527,13 @@ If you generate code using:
408527
- Missing `import "dotenv/config"` in prisma.config.ts → **DATABASE_URL NOT FOUND ERROR**
409528
- Missing `import "dotenv/config"` in test scripts → **ENVIRONMENT VARIABLE ERROR**
410529
- Default import from `@prisma/client` → **IMPORT ERROR**
411-
- PrismaClient without Accelerate extension → **PRODUCTION CONNECTION FAILURES**
530+
- Using `accelerateUrl` or `withAccelerate` → **UNNECESSARY ACCELERATE DEPENDENCY / CONFIG ERROR**
412531
- Missing custom output path → **WRONG CLIENT GENERATED**
532+
- Including `url` in datasource block → **DEPRECATED CONFIGURATION ERROR**
533+
- Including `engine` property → **DEPRECATED CONFIGURATION ERROR**
534+
- Using local URL (`postgres://localhost:...`) → **VERSION INCOMPATIBILITY ERRORS WITH PRISMA 7**
535+
- Using `npx prisma init` without `--db` → **NO DATABASE CREATED, ONLY LOCAL FILES**
536+
- Manually inventing DATABASE_URL → **INVALID CONNECTION STRING ERRORS**
413537

414538
The implementation will:
415539

@@ -418,12 +542,13 @@ The implementation will:
418542
3. Cause connection pool exhaustion in production
419543
4. Result in import errors that prevent compilation
420544
5. Cause performance issues and connection failures
545+
6. Fail with "HTTP connection string is not supported" errors when using local URLs
421546

422547
## USEFUL COMMANDS
423548

424549
```bash
425550
# After changing schema
426-
npx prisma generate # Regenerate client
551+
npx prisma generate # Regenerate client (--no-engine flag no longer needed)
427552

428553
# Push schema to database (no migrations)
429554
npx prisma db push
@@ -482,35 +607,15 @@ When asked about Prisma + Next.js implementation, you MUST:
482607
8. ALWAYS add npm scripts for `db:test` and `db:studio`
483608
9. ALWAYS include error handling in API routes and server components
484609
10. ALWAYS use the global prisma instance from `lib/prisma.ts`
485-
11. VERIFY your response against ALL the patterns shown here before responding
610+
11. ALWAYS use `@prisma/adapter-pg` and `adapter` property
611+
12. NEVER include `url` in the datasource block of schema.prisma
612+
13. NEVER include `engine` property in prisma.config.ts
613+
14. ALWAYS ask the user to run `npx prisma init --db --output ../app/generated/prisma` **manually in their own terminal** (this command is interactive and requires user input for region and project name)
614+
15. ALWAYS wait for user confirmation after they run the interactive `prisma init --db` command before proceeding
615+
16. NEVER attempt to run interactive commands yourself - ask the user to do it
616+
17. NEVER use `prisma+postgres://` URLs - ONLY `postgres://` TCP URLs
617+
18. NEVER manually invent or fabricate DATABASE_URL values
618+
19. ALWAYS let Prisma generate the `.env` file with the real DATABASE_URL (and ensure it's correct type)
619+
20. VERIFY your response against ALL the patterns shown here before responding
486620

487621
Remember: There are NO EXCEPTIONS to these rules. Every requirement is MANDATORY for the setup to work.
488-
489-
490-
````
491-
492-
## Running the application
493-
494-
Get your application running locally in three quick steps:
495-
496-
**1. Generate the Prisma Client:**
497-
498-
```terminal
499-
npx prisma generate --no-engine
500-
```
501-
502-
**2. View your database in Prisma Studio:**
503-
504-
```terminal
505-
npm run db:studio
506-
```
507-
508-
Prisma Studio opens at `localhost:5555` where you can inspect your `User` table and see the test user stored in your database.
509-
510-
**3. Start your Next.js development server:**
511-
512-
```terminal
513-
npm run dev
514-
```
515-
516-
Visit `http://localhost:3000` to see your Next.js application live, displaying your first user fetched directly from your Prisma Postgres database!

0 commit comments

Comments
 (0)