# Nuxt + Prisma (/docs/ai/prompts/nuxt) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Step-by-step guide for integrating Prisma ORM and Prisma Postgres in a Nuxt project Location: AI > Prompts > Nuxt + Prisma ## Prerequisites [#prerequisites] Before using this prompt, you need to create a new Nuxt project: ```bash npx nuxi@latest init hello-prisma cd hello-prisma ``` Once your Nuxt project is created, you can use the prompt below with your AI assistant to add Prisma and Prisma Postgres. ## How to use [#how-to-use] Include this prompt in your AI assistant to guide consistent code generation for Nuxt + Prisma + Prisma Postgres projects. * **GitHub Copilot**: Type `#` to reference the prompt file. * **Cursor**: Use `@Files` and select your prompt file. * **Zed**: Use `/file` followed by your prompt's path. * **Windsurf**: Use `@Files` and choose your prompt file from the list. ## Prompt [#prompt] ````md --- # Specify the following for Cursor rules description: Guidelines for writing Nuxt apps with Prisma Postgres alwaysApply: false --- # Bootstrap Nuxt app with Prisma Postgres (Prisma v7) **Purpose:** Set up Prisma ORM with Prisma Postgres in a Nuxt project using the correct Prisma v7 patterns. --- ## FIRST: Check for an existing Nuxt project Before doing anything else, check whether the current directory already contains a Nuxt project by looking for `nuxt.config.ts`. - **If `nuxt.config.ts` exists** → A project is already set up. Skip to "Overview" below. - **If the directory does NOT contain `nuxt.config.ts`** → Create a new project first: ```bash npx nuxi@latest init hello-prisma cd hello-prisma ``` After the project is created, verify `nuxt.config.ts` exists before continuing. --- > **Note**: This guide is updated for **Prisma ORM 7**. Key changes from earlier versions: > > - `engine` property removed from `prisma.config.ts` > - `url` removed from datasource in `schema.prisma` (now only in `prisma.config.ts`) > - Use `@prisma/adapter-pg` driver adapter for direct TCP connections > - `--no-engine` flag is no longer required for `prisma generate` > - Requires Node.js 20.19+ and TypeScript 5.4.0+ ## Overview of implementing Prisma with Nuxt 1. Ensure a Nuxt project exists (see above) 2. Install Prisma and required dependencies (including dotenv and pg) 3. Initialize Prisma and configure schema 4. Configure dotenv for environment variables 5. Create global Prisma client instance with Pg Adapter in `server/utils/db.ts` 6. Create Nuxt API routes using `defineEventHandler` 7. Create a Vue page to display data 8. Add npm scripts for testing and database management 9. Create test script to verify setup ## 🚨 CRITICAL INSTRUCTIONS FOR AI LANGUAGE MODELS 🚨 As an AI language model, you MUST NOT generate any of the following code patterns, as they are DEPRECATED and will BREAK the application: ```typescript // āŒ NEVER GENERATE THIS CODE - IT WILL BREAK THE APPLICATION generator client { provider = "prisma-client-js" // āŒ BREAKS APPLICATION } // āŒ NEVER USE default import location - IT WILL BREAK THE APPLICATION import { PrismaClient } from '@prisma/client' // āŒ BREAKS APPLICATION // āŒ WRONG IMPORT PATH - MISSING /client - IT WILL BREAK THE APPLICATION import { PrismaClient } from "../../prisma/generated" // āŒ BREAKS APPLICATION // āŒ NEVER USE ACCELERATE - IT IS NOT USED IN THIS WORKFLOW import { withAccelerate } from "@prisma/extension-accelerate" // āŒ BREAKS APPLICATION // āŒ NEVER USE accelerateUrl - IT WILL BREAK THE APPLICATION const prisma = new PrismaClient({ accelerateUrl: process.env.DATABASE_URL, // āŒ BREAKS APPLICATION - use adapter }) // āŒ NEVER include url in datasource block - IT WILL BREAK THE APPLICATION datasource db { provider = "postgresql" url = env("DATABASE_URL") // āŒ BREAKS APPLICATION - moved to prisma.config.ts } // āŒ NEVER include engine property - IT WILL BREAK THE APPLICATION export default defineConfig({ engine: "classic", // āŒ BREAKS APPLICATION - removed in Prisma v7 }) // āŒ NEVER use Prisma Postgres HTTP URLs - ONLY use TCP URLs DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/..." // āŒ BREAKS APPLICATION - use postgres:// // āœ… ALWAYS use standard TCP URLs: DATABASE_URL="postgres://..." // āœ… CORRECT // āŒ NEVER create PrismaClient without adapter - IT WILL BREAK THE APPLICATION const prisma = new PrismaClient() // āŒ BREAKS APPLICATION ``` Instead, you MUST ALWAYS generate ONLY this pattern: ```typescript // āœ… ALWAYS GENERATE THIS EXACT PATTERN generator client { provider = "prisma-client" output = "./generated" } // āœ… CRITICAL: MUST include /client at the end of import path import { PrismaClient } from "../../prisma/generated/client" import { PrismaPg } from "@prisma/adapter-pg" const pool = new PrismaPg({ connectionString: process.env.DATABASE_URL! }) const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined } const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter: pool }) if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma export { prisma } ``` ## ABSOLUTE REQUIREMENTS FOR AI CODE GENERATION 1. You MUST use `provider = "prisma-client"` (not "prisma-client-js") 2. You MUST use custom output: `output = "./generated"` (relative to prisma directory) 3. You MUST use `@prisma/adapter-pg` driver adapter with `PrismaPg` 4. You MUST create `server/utils/db.ts` as a global singleton instance (Nuxt auto-imports from `server/utils/`) 5. You MUST wrap all database calls in try-catch blocks 6. You MUST import from `'../../prisma/generated/client'` (not `'@prisma/client'` or `'../../prisma/generated'`) 7. You MUST use `adapter` property in PrismaClient constructor 8. You MUST install `dotenv` and add `import "dotenv/config"` to `prisma.config.ts` 9. You MUST add npm scripts for `db:test` and `db:studio` to package.json 10. You MUST create a test script at `scripts/test-database.ts` to verify setup 11. You MUST NOT include `url` in the datasource block of `schema.prisma` 12. You MUST NOT include `engine` property in `prisma.config.ts` 13. You MUST use `npx prisma init --output ./generated` to scaffold Prisma, then `npx create-db` to create a real cloud database 14. You MUST use standard TCP URLs (`postgres://...`) in .env 15. You MUST NOT use `accelerateUrl` or `withAccelerate` 16. You MUST install `pg` and `@types/pg` as dependencies ## VERSION REQUIREMENTS - **Node.js**: 20.19 or higher (Node.js 18 is NOT supported) - **TypeScript**: 5.4.0 or higher (5.9.x recommended) - **Prisma**: 7.0.0 or higher ## CORRECT INSTALLATION ```bash # Production dependencies npm install @prisma/client@7 @prisma/adapter-pg pg dotenv # Dev dependencies npm install -D prisma@prev @types/pg tsx ``` ## CORRECT PRISMA INITIALIZATION > **FOR AI ASSISTANTS**: `npx prisma init` is not interactive. Run it yourself if your environment allows it. If you need a real Prisma Postgres database, either run `npx create-db` or ask the user to run it and update `DATABASE_URL` before you continue. ```bash # Initialize Prisma and scaffold the Prisma files npx prisma init --output ./generated # Then create a Prisma Postgres database npx create-db ``` This step: - Generates `prisma/schema.prisma` with the correct output path - Generates `prisma.config.ts` - Generates `.env` with a local `DATABASE_URL` - Requires `npx create-db` if you want a real Prisma Postgres database **IMPORTANT**: After `npx create-db`, replace the generated `DATABASE_URL` in `.env` with the returned `postgres://...` connection string. ```text DATABASE_URL="postgres://..." ``` ## CORRECT PRISMA CONFIG (prisma.config.ts) When using `npx prisma init`, the `prisma.config.ts` is **auto-generated** with the correct configuration: ```typescript import "dotenv/config"; // āœ… Auto-included by prisma init import { defineConfig, env } from "prisma/config"; export default defineConfig({ schema: "prisma/schema.prisma", migrations: { path: "prisma/migrations", seed: "tsx ./prisma/seed.ts", }, // āœ… NO engine property - removed in Prisma v7 datasource: { url: env("DATABASE_URL"), }, }); ``` **Note**: If you need to manually create this file, ensure `import "dotenv/config"` is at the top. ## CORRECT SCHEMA CONFIGURATION (prisma/schema.prisma) Update the generated `prisma/schema.prisma` file: ```prisma generator client { provider = "prisma-client" output = "./generated" } datasource db { provider = "postgresql" // āœ… NO url here - now configured in prisma.config.ts } model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User? @relation(fields: [authorId], references: [id]) authorId Int? } ``` ## CORRECT GLOBAL PRISMA CLIENT Create `server/utils/db.ts` file. Nuxt auto-imports exports from `server/utils/`, making `prisma` available in all server API routes without explicit imports: ```typescript import { PrismaPg } from "@prisma/adapter-pg"; import { PrismaClient } from "../../prisma/generated/client"; // āœ… CRITICAL: Include /client const prismaClientSingleton = () => { const pool = new PrismaPg({ connectionString: process.env.DATABASE_URL! }); return new PrismaClient({ adapter: pool }); }; type PrismaClientSingleton = ReturnType; const globalForPrisma = globalThis as unknown as { prisma: PrismaClientSingleton | undefined; }; export const prisma = globalForPrisma.prisma ?? prismaClientSingleton(); if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma; ``` ## ADD NPM SCRIPTS TO PACKAGE.JSON Update your `package.json` to include these scripts: ```json { "scripts": { "build": "nuxt build", "dev": "nuxt dev", "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "prisma generate && nuxt prepare", "db:test": "tsx scripts/test-database.ts", "db:studio": "prisma studio" } } ``` ## CREATE TEST SCRIPT Create `scripts/test-database.ts` to verify your setup: ```typescript import "dotenv/config"; // āœ… CRITICAL: Load environment variables import { PrismaPg } from "@prisma/adapter-pg"; import { PrismaClient } from "../prisma/generated/client"; const pool = new PrismaPg({ connectionString: process.env.DATABASE_URL! }); const prisma = new PrismaClient({ adapter: pool }); async function testDatabase() { console.log("šŸ” Testing Prisma Postgres connection...\n"); try { // Test 1: Check connection console.log("āœ… Connected to database!"); // Test 2: Create a test user console.log("\nšŸ“ Creating a test user..."); const newUser = await prisma.user.create({ data: { email: "demo@example.com", name: "Demo User", }, }); console.log("āœ… Created user:", newUser); // Test 3: Fetch all users console.log("\nšŸ“‹ Fetching all users..."); const allUsers = await prisma.user.findMany(); console.log(`āœ… Found ${allUsers.length} user(s):`); allUsers.forEach((user) => { console.log(` - ${user.name} (${user.email})`); }); console.log("\nšŸŽ‰ All tests passed! Your database is working perfectly.\n"); } catch (error) { console.error("āŒ Error:", error); process.exit(1); } } testDatabase(); ``` ## CORRECT API ROUTE IMPLEMENTATION Nuxt uses file-based API routing in `server/api/`. The `prisma` instance is auto-imported from `server/utils/db.ts`. Create `server/api/users.get.ts` to fetch users: ```typescript export default defineEventHandler(async () => { try { const users = await prisma.user.findMany({ include: { posts: true }, }); return users; } catch (error) { console.error("Error fetching users:", error); throw createError({ statusCode: 500, statusMessage: "Failed to fetch users", }); } }); ``` Create `server/api/users.post.ts` to create a user: ```typescript export default defineEventHandler(async (event) => { try { const body = await readBody<{ name: string; email: string }>(event); if (!body.email) { throw createError({ statusCode: 400, statusMessage: "Email is required", }); } const user = await prisma.user.create({ data: { name: body.name, email: body.email, }, }); return user; } catch (error) { console.error("Error creating user:", error); throw createError({ statusCode: 500, statusMessage: "Failed to create user", }); } }); ``` ## CORRECT USAGE IN VUE PAGES Update `app.vue` to display users from the database: ```html ``` ## COMPLETE SETUP WORKFLOW User should follow these steps (AI should provide these instructions): 1. **Ensure a Nuxt project exists** — check for `nuxt.config.ts`. If missing, create one: ```bash npx nuxi@latest init hello-prisma cd hello-prisma ``` 2. **Install dependencies**: ```bash npm install @prisma/client@7 @prisma/adapter-pg pg dotenv ``` ```bash npm install -D prisma@prev @types/pg tsx ``` 3. **Initialize Prisma AND create Prisma Postgres database** (āš ļø USER MUST RUN MANUALLY): > **AI ASSISTANT**: You can run `npx prisma init` yourself. If you should not provision cloud resources automatically, ask the user to run `npx create-db` and update `DATABASE_URL` before continuing. ```bash npx prisma init --output ./generated npx create-db ``` This creates `prisma/schema.prisma`, `prisma.config.ts`, and `.env`, then returns a `postgres://...` connection string for Prisma Postgres. **If you asked the user to run `npx create-db`, wait for them to share or paste the returned connection string before proceeding.** 4. **Verify `.env` was created** — Replace the generated `DATABASE_URL` with the `postgres://...` connection string returned by `npx create-db`. ```text DATABASE_URL="postgres://..." ``` **Do NOT invent this URL. Use the one returned by `npx create-db`.** 5. **Update `prisma/schema.prisma`** — Add the User and Post models (generator and datasource are already configured): ```prisma model User { id Int @id @default(autoincrement()) email String @unique name String? posts Post[] } model Post { id Int @id @default(autoincrement()) title String content String? published Boolean @default(false) author User? @relation(fields: [authorId], references: [id]) authorId Int? } ``` 6. **Create `server/utils/db.ts`** with correct import path including `/client` and using `@prisma/adapter-pg`. 7. **Add npm scripts** to `package.json` for `db:test`, `db:studio`, and `postinstall` 8. **Create `scripts/test-database.ts`** test script 9. **Create API routes** — `server/api/users.get.ts` and `server/api/users.post.ts` 10. **Update `app.vue`** to display users 11. **Push schema to database**: ```bash npx prisma db push ``` 12. **Generate Prisma Client**: ```bash npx prisma generate ``` 13. **Test the setup**: ```bash npm run db:test ``` 14. **Start development server**: ```bash npm run dev ``` ## AI MODEL VERIFICATION STEPS Before generating any code, you MUST verify: 1. Does the current directory contain `nuxt.config.ts`? If not, create the project first. 2. Are you using `provider = "prisma-client"` (not "prisma-client-js")? If not, STOP and FIX. 3. Are you using `output = "./generated"`? If not, STOP and FIX. 4. Are you importing from `'../../prisma/generated/client'` (with `/client`)? If not, STOP and FIX. 5. Did you add `import "dotenv/config"` to `prisma.config.ts`? If not, STOP and FIX. 6. Did you add `import "dotenv/config"` to `scripts/test-database.ts`? If not, STOP and FIX. 7. Are you using `@prisma/adapter-pg` with `PrismaPg`? If not, STOP and FIX. 8. Are you using `adapter` property in PrismaClient constructor? If not, STOP and FIX. 9. Are you wrapping database operations in try-catch? If not, STOP and FIX. 10. Did you create the Prisma client in `server/utils/db.ts` (not `lib/prisma.ts`)? If not, STOP and FIX. 11. Did you create the test script at `scripts/test-database.ts`? If not, STOP and FIX. 12. Did you add `db:test` and `db:studio` scripts to package.json? If not, STOP and FIX. 13. Did you remove `url` from the datasource block in `schema.prisma`? If not, STOP and FIX. 14. Did you remove `engine` property from `prisma.config.ts`? If not, STOP and FIX. 15. Did you run `npx prisma init` with the documented output path? If not, STOP and FIX. 16. Is the DATABASE_URL a TCP URL (`postgres://...`)? If it's a `prisma+postgres://` URL, STOP and FIX. 17. Did Prisma generate the `.env` file? If you invented the URL manually, STOP and FIX. 18. Are you using Nuxt auto-imports (`defineEventHandler`, `readBody`, `createError`, `prisma`)? If not, STOP and FIX. ## CONSEQUENCES OF INCORRECT IMPLEMENTATION If you generate code using: - `prisma-client-js` provider → **CLIENT GENERATION FAILS** - Wrong import path (missing `/client`) → **MODULE NOT FOUND ERROR** - Missing `import "dotenv/config"` in prisma.config.ts → **DATABASE_URL NOT FOUND ERROR** - Missing `import "dotenv/config"` in test scripts → **ENVIRONMENT VARIABLE ERROR** - Default import from `@prisma/client` → **IMPORT ERROR** - Using `accelerateUrl` or `withAccelerate` → **UNNECESSARY ACCELERATE DEPENDENCY / CONFIG ERROR** - Missing custom output path → **WRONG CLIENT GENERATED** - Including `url` in datasource block → **DEPRECATED CONFIGURATION ERROR** - Including `engine` property → **DEPRECATED CONFIGURATION ERROR** - Using local URL (`postgres://localhost:...`) → **VERSION INCOMPATIBILITY ERRORS WITH Prisma v7** - Using `npx prisma init` without `--db` → **NO DATABASE CREATED, ONLY LOCAL FILES** - Manually inventing DATABASE_URL → **INVALID CONNECTION STRING ERRORS** - Creating client in `lib/prisma.ts` instead of `server/utils/db.ts` → **NOT AUTO-IMPORTED IN NUXT SERVER ROUTES** - Missing `pg` dependency → **ADAPTER INITIALIZATION FAILURE** The implementation will: 1. Break immediately with module errors 2. Fail to read environment variables 3. Cause connection pool exhaustion in production 4. Result in import errors that prevent compilation 5. Cause performance issues and connection failures 6. Fail with "HTTP connection string is not supported" errors when using local URLs ## USEFUL COMMANDS ```bash # After changing schema npx prisma generate # Regenerate client (--no-engine flag no longer needed) # Push schema to database (no migrations) npx prisma db push # Test database connection npm run db:test # Open visual database editor npm run db:studio # Create and apply migrations (for production) npx prisma migrate dev --name your_migration_name ``` ## TESTING WORKFLOW After setup, test with these steps: 1. **Test database connection**: ```bash npm run db:test ``` Should create a demo user and display it. 2. **Open Prisma Studio**: ```bash npm run db:studio ``` Visual interface at `localhost:5555` to view/edit data. 3. **Test API routes**: ```bash # Create a user via API curl -X POST http://localhost:3000/api/users \ -H "Content-Type: application/json" \ -d '{"email":"test@example.com","name":"Test User"}' # Get all users curl http://localhost:3000/api/users ``` 4. **View in browser**: Open `localhost:3000` to see users displayed on the homepage. ## AI MODEL RESPONSE TEMPLATE When asked about Prisma + Nuxt implementation, you MUST: 1. ONLY use code patterns from this guide 2. NEVER suggest deprecated approaches 3. ALWAYS use the exact patterns shown above 4. ALWAYS include `/client` in import paths 5. ALWAYS add `import "dotenv/config"` to prisma.config.ts 6. ALWAYS add `import "dotenv/config"` to test scripts 7. ALWAYS create the Prisma client in `server/utils/db.ts` (Nuxt auto-imports) 8. ALWAYS create the test script at `scripts/test-database.ts` 9. ALWAYS add npm scripts for `db:test` and `db:studio` 10. ALWAYS include error handling in API routes using `createError` 11. ALWAYS use the auto-imported `prisma` instance in server routes 12. ALWAYS use `@prisma/adapter-pg` with `PrismaPg` and `adapter` property 13. NEVER include `url` in the datasource block of schema.prisma 14. NEVER include `engine` property in prisma.config.ts 15. ALWAYS run `npx prisma init --output ./generated` before editing Prisma files. If you cannot provision cloud resources automatically, ask the user to run `npx create-db` and update `DATABASE_URL` before continuing 16. ALWAYS wait for user confirmation after they run `npx create-db` and share or apply the returned `postgres://...` connection string before proceeding 17. NEVER attempt to run interactive commands yourself - ask the user to do it 18. NEVER use `prisma+postgres://` URLs - ONLY `postgres://` TCP URLs 19. NEVER manually invent or fabricate DATABASE_URL values 20. ALWAYS let Prisma generate the `.env` file with the real DATABASE_URL (and ensure it's correct type) 21. VERIFY your response against ALL the patterns shown here before responding Remember: There are NO EXCEPTIONS to these rules. Every requirement is MANDATORY for the setup to work. ```` ## Related pages - [`Astro + Prisma`](https://www.prisma.io/docs/ai/prompts/astro): Step-by-step guide for integrating Prisma ORM and Prisma Postgres in an Astro.js project - [`Migrate to Prisma v7`](https://www.prisma.io/docs/ai/prompts/prisma-7): Step-by-step guide for migration your app to use the version 7 of Prisma ORM - [`Next.js + Prisma`](https://www.prisma.io/docs/ai/prompts/nextjs): Step-by-step guide for integrating Prisma ORM and Prisma Postgres in an NextJS project - [`Turborepo + Prisma`](https://www.prisma.io/docs/ai/prompts/turborepo): Step-by-step guide for integrating Prisma ORM and Prisma Postgres in a Turborepo monorepo