Skip to content

Commit c2fcbb2

Browse files
AmanVarshney01aidankmcalister
authored andcommitted
Update nuxt guides (#7275)
* update nuxt guide * update
1 parent 141ef26 commit c2fcbb2

File tree

2 files changed

+151
-94
lines changed

2 files changed

+151
-94
lines changed

content/200-orm/800-more/600-help-and-troubleshooting/900-prisma-nuxt-module.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ The Nuxt Prisma module simplifies the integration of Prisma ORM into your Nuxt a
1111

1212
This module provides several features to streamline the setup and usage of Prisma ORM in a Nuxt application, making it easier to interact with your database.
1313

14+
:::warning
15+
The `@prisma/nuxt` module is currently not supported with Prisma ORM 7 or when using a custom `generator client { output = ... }`. For Prisma 7 projects, follow the general Nuxt guide instead.
16+
:::
17+
1418
## Features
1519

1620
- **Project initialization**: Automatically sets up a Prisma ORM project with a SQLite database within your Nuxt project.

content/800-guides/100-nuxt.mdx

Lines changed: 147 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: 'How to use Prisma ORM with Nuxt'
33
metaTitle: 'Build a Nuxt app with Prisma ORM and Prisma Postgres'
4-
description: 'A step-by-step guide to setting up and using Prisma ORM and Prisma Postgres with the Prisma Nuxt module and deploying to Vercel.'
4+
description: 'A step-by-step guide to setting up and using Prisma ORM and Prisma Postgres in a Nuxt app and deploying to Vercel.'
55
sidebar_label: 'Nuxt'
66
completion_time: '10 min'
77
image: '/img/guides/prisma-postgres-and-prisma-nuxt-guide.png'
@@ -12,12 +12,12 @@ tags:
1212
community_section: true
1313
---
1414

15-
This guide explains how to set up a Nuxt application, configure [Prisma Postgres](https://prisma.io/postgres) using the [Prisma Nuxt module](/orm/more/help-and-troubleshooting/prisma-nuxt-module), and deploy the project to [Vercel](https://vercel.com/) for production.
15+
This guide explains how to set up a Nuxt application, configure [Prisma Postgres](https://prisma.io/postgres) with Prisma ORM, and deploy the project to [Vercel](https://vercel.com/) for production.
1616

1717
Here's what you'll learn:
1818

19-
- How to set up a Nuxt project with the Prisma Nuxt module.
20-
- How to configure and use Prisma Postgres with the Prisma Nuxt module in your Nuxt app.
19+
- How to set up a Nuxt project that uses Prisma ORM directly.
20+
- How to configure and use Prisma Postgres with Prisma ORM in your Nuxt app.
2121
- How to deploy the project to Vercel.
2222

2323
## Prerequisites
@@ -30,36 +30,21 @@ To follow this guide, ensure you have the following:
3030
- [Vercel](https://vercel.com)
3131
- Basic knowledge of Git and Vercel deployment (helpful but not required).
3232

33-
## 1. Create a New Nuxt Project and set up the Prisma Nuxt module
33+
## 1. Create a New Nuxt Project and install Prisma ORM dependencies
3434

3535
1. Initialize [a new Nuxt project](https://nuxt.com/docs/getting-started/installation#new-project), select `npm` as the package manager and initialize git:
3636
```terminal
3737
npm create nuxt hello-world
3838
```
39-
:::note
40-
We recommend using `npm` as it is the most stable option with the `@prisma/nuxt` module.
41-
:::
42-
43-
2. Navigate into the project directory and install the `@prisma/nuxt` module:
39+
2. Navigate into the project directory and install Prisma ORM dependencies along with the PostgreSQL driver adapter:
4440
```terminal
4541
cd hello-world
46-
npm i @prisma/nuxt
42+
npm install @prisma/client @prisma/adapter-pg pg dotenv tsx
43+
npm install -D prisma @types/pg
4744
```
48-
3. Install the [Prisma Accelerate client extension](https://www.npmjs.com/package/@prisma/extension-accelerate) as it's required to use Prisma Postgres:
45+
3. Initialize Prisma with PostgreSQL by running:
4946
```terminal
50-
npm i @prisma/extension-accelerate
51-
```
52-
4. Add the `@prisma/nuxt` module with the following configuration to your `nuxt.config.ts` file:
53-
```typescript
54-
// https://nuxt.com/docs/api/configuration/nuxt-config
55-
export default defineNuxtConfig({
56-
compatibilityDate: "2024-11-01",
57-
modules: ["@prisma/nuxt"],
58-
experimental: {
59-
componentIslands: true,
60-
},
61-
devtools: { enabled: true },
62-
});
47+
npx prisma init --db
6348
```
6449

6550
## 2. Configure Prisma
@@ -68,62 +53,36 @@ Before running the development server, create a `prisma.config.ts` file in the r
6853

6954
```typescript file=prisma.config.ts
7055
import 'dotenv/config'
71-
import { defineConfig, env } from 'prisma/config';
56+
import { defineConfig, env } from 'prisma/config'
7257

7358
export default defineConfig({
7459
schema: 'prisma/schema.prisma',
7560
migrations: {
7661
path: 'prisma/migrations',
62+
seed: 'tsx ./prisma/seed.ts',
7763
},
7864
datasource: {
7965
url: env('DATABASE_URL'),
8066
},
81-
});
67+
})
8268
```
8369

84-
:::note
85-
86-
You'll need to install the `dotenv` package to load environment variables:
87-
88-
```bash
89-
npm install dotenv
90-
```
91-
92-
:::
93-
94-
## 3. Setup Prisma ORM by running the development server locally
70+
## 3. Set up Prisma ORM
9571

96-
After configuring your Nuxt project with the Prisma module, the next step is to set up Prisma ORM. This process begins by starting the development server, which automatically configures Prisma with a [SQLite database](/orm/overview/databases/sqlite).
72+
With Prisma initialized, define your data model and run a migration to create the PostgreSQL database.
9773

98-
Run the following command to start the development server:
99-
100-
```terminal
101-
npm run dev
102-
```
103-
104-
After running this command, you will be prompted to run a database migration with [Prisma Migrate](/orm/prisma-migrate/understanding-prisma-migrate/overview):
105-
106-
```terminal
107-
? Do you want to migrate database changes to your database? › (Y/n)
108-
```
109-
110-
Confirm that you want to migrate your database and create your initial tables by hitting <kbd>Y</kbd> on your keyboard.
111-
112-
Once the setup flow has terminated, it:
113-
114-
1. Installed the [Prisma CLI](/orm/reference/prisma-cli-reference).
115-
2. Initialized a Prisma project with a SQLite database.
116-
3. Created sample `User` and `Post` models in the `schema.prisma` file:
74+
1. Update the `prisma/schema.prisma` file with the following configuration:
11775
```prisma file=prisma/schema.prisma
11876
// This is your Prisma schema file,
11977
// learn more about it in the docs: https://pris.ly/d/prisma-schema
12078
12179
generator client {
12280
provider = "prisma-client"
81+
output = "./generated"
12382
}
12483
12584
datasource db {
126-
provider = "sqlite"
85+
provider = "postgresql"
12786
}
12887
12988
model User {
@@ -142,39 +101,52 @@ Once the setup flow has terminated, it:
142101
authorId Int
143102
}
144103
```
145-
4. Created the database tables for the `User` and `Post` models from the previous steps.
146-
:::note
147-
The database migrates automatically the first time you start the module if there isn't a `migrations` folder. After that, you need to run `npx prisma migrate dev` manually in the CLI to apply any schema changes. Running the `npx prisma migrate dev` command manually makes it easier and safer to manage migrations and also to [troubleshoot](/orm/prisma-migrate/workflows/troubleshooting) any migration-related errors.
148-
:::
149-
5. Installed and generated [Prisma Client](https://da-2255.docs-51g.pages.dev/orm/reference/prisma-client-reference) which enables you to query your DB.
150-
6. Installed [Prisma Studio](/orm/tools/prisma-studio).
151-
152-
When the Prisma setup is complete, the development server should start on `https://localhost:3000`.
153-
154-
Next, stop the server, as we need to make some code changes.
104+
2. Create the initial migration and database tables:
105+
```terminal
106+
npx prisma migrate dev --name init
107+
```
108+
3. Once the command succeeds, start the Nuxt development server:
109+
```terminal
110+
npm run dev
111+
```
112+
4. When the server is running at `http://localhost:3000`, stop it for now—we’ll update some files before continuing.
155113

156114
## 4. Update the application code
157115

158116
With Prisma configured, the next step is to update your application code to fetch and display data from your database.
159117

160-
1. In the root directory of your project, create a folder named `components`.
118+
1. In the root directory of your project, create a folder named `lib` and add a `prisma.ts` file to share a single Prisma Client instance that connects through the PostgreSQL adapter:
119+
```ts file=lib/prisma.ts
120+
import { PrismaPg } from '@prisma/adapter-pg'
121+
import { PrismaClient } from '../prisma/generated/client'
122+
123+
const prismaClientSingleton = () => {
124+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! })
125+
return new PrismaClient({ adapter })
126+
}
161127

162-
2. Inside the `components` folder, create a file named `User.server.vue`. This server component will fetch and display the name of the first user from the database:
128+
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>
129+
130+
const globalForPrisma = globalThis as unknown as {
131+
prisma: PrismaClientSingleton | undefined
132+
}
133+
134+
export const prisma = globalForPrisma.prisma ?? prismaClientSingleton()
135+
136+
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
137+
```
138+
139+
2. Create a folder named `components` (if it doesn’t exist yet) and inside it add `User.server.vue`. This server component fetches and displays the name of the first user from the database:
163140
```html file=components/User.server.vue
164141
<script setup>
165-
import { withAccelerate } from "@prisma/extension-accelerate";
166-
const prisma = usePrismaClient().$extends(withAccelerate());
167-
const user = await prisma.user.findFirst();
142+
import prisma from '~/lib/prisma'
143+
const user = await prisma.user.findFirst()
168144
</script>
169145

170146
<template>
171147
<p>{{ user?.name ?? "No user has been added yet." }}</p>
172148
</template>
173149
```
174-
175-
:::note
176-
We're extending the `usePrismaClient()` composable with the `withAccelerate()` extension method to ensure [compatibility with Prisma Postgres](/postgres/introduction/overview#using-the-client-extension-for-prisma-accelerate-required). This extension will also allow you to [cache your queries](/postgres/database/caching).
177-
:::
178150

179151
3. Modify the `app.vue` file in the root directory to include the new server component using Nuxt Islands:
180152
```html file=app.vue
@@ -198,7 +170,93 @@ With Prisma configured, the next step is to update your application code to fetc
198170

199171
By completing these steps, your application is now capable of fetching data from your Prisma database and rendering it on the frontend.
200172

201-
## 5. Create a Prisma Postgres instance
173+
## 5. Seed your database (optional)
174+
175+
If you want sample data, create a `prisma/seed.ts` file. The following script uses the PostgreSQL adapter:
176+
177+
```ts file=prisma/seed.ts
178+
import 'dotenv/config'
179+
import { PrismaClient } from './generated/client'
180+
import { PrismaPg } from '@prisma/adapter-pg'
181+
182+
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL! })
183+
const prisma = new PrismaClient({ adapter })
184+
185+
const userData = [
186+
{
187+
name: 'Alice',
188+
189+
posts: {
190+
create: [
191+
{
192+
title: 'Join the Prisma Discord',
193+
content: 'https://pris.ly/discord',
194+
published: true,
195+
},
196+
],
197+
},
198+
},
199+
{
200+
name: 'Nilu',
201+
202+
posts: {
203+
create: [
204+
{
205+
title: 'Follow Prisma on Twitter',
206+
content: 'https://www.twitter.com/prisma',
207+
published: true,
208+
},
209+
],
210+
},
211+
},
212+
{
213+
name: 'Mahmoud',
214+
215+
posts: {
216+
create: [
217+
{
218+
title: 'Ask a question about Prisma on GitHub',
219+
content: 'https://www.github.com/prisma/prisma/discussions',
220+
published: true,
221+
},
222+
{
223+
title: 'Prisma on YouTube',
224+
content: 'https://pris.ly/youtube',
225+
},
226+
],
227+
},
228+
},
229+
]
230+
231+
async function main() {
232+
console.log(`Start seeding ...`)
233+
for (const u of userData) {
234+
const user = await prisma.user.create({
235+
data: u,
236+
})
237+
console.log(`Created user with id: ${user.id}`)
238+
}
239+
console.log(`Seeding finished.`)
240+
}
241+
242+
main()
243+
.then(async () => {
244+
await prisma.$disconnect()
245+
})
246+
.catch(async (e) => {
247+
console.error(e)
248+
await prisma.$disconnect()
249+
process.exit(1)
250+
})
251+
```
252+
253+
Run the seed command whenever you need demo data:
254+
255+
```terminal
256+
npx prisma db seed
257+
```
258+
259+
## 6. Create a Prisma Postgres instance
202260

203261
To store your app's data, you'll create a Prisma Postgres database instance using the Prisma Data Platform.
204262

@@ -223,7 +281,7 @@ DATABASE_URL=<your-database-url>
223281

224282
The `DATABASE_URL` environment variable will be required in the next steps.
225283

226-
## 6. Set up Prisma Postgres in your Nuxt app
284+
## 7. Set up Prisma Postgres in your Nuxt app
227285

228286
Now that the Prisma Postgres instance is ready, update your Nuxt application to use this database:
229287

@@ -233,28 +291,23 @@ Now that the Prisma Postgres instance is ready, update your Nuxt application to
233291
DATABASE_URL="prisma+postgres://accelerate.prisma-data.net/?api_key=PRISMA_POSTGRES_API_KEY"
234292
```
235293

236-
2. Modify the `schema.prisma` file by changing the database provider in the `datasource` block of the `schema.prisma` file located in the `prisma` folder:
237-
```prisma file=prisma/schema.prisma
238-
datasource db {
239-
// edit-next-line
240-
provider = "postgresql"
241-
}
242-
```
243-
3. Delete the SQLite database files (`dev.db` and `dev.db-journal`) along with the `migrations` folder, all located in the `prisma` directory. This cleans up the existing SQLite setup and prepares your project to migrate to PostgreSQL.
244-
4. Manually create a new migration for the Postgres database by running the `prisma migrate` command:
294+
2. Manually run a migration to sync your schema to Prisma Postgres:
245295
```terminal
246296
npx prisma migrate dev --name init
247297
```
248-
5. Start the development server again:
298+
3. (Optional) Seed your database with sample data:
299+
```terminal
300+
npx prisma db seed
301+
```
302+
4. Start the development server again:
249303
```terminal
250304
npm run dev
251305
```
252-
6. Open the Nuxt DevTools (by hitting <kbd>Shift</kbd>+<kbd>Option</kbd>+ <kbd>D</kbd>) and click the Prisma logo in the left sidenav to open Prisma Studio. Then add a new `User` record by specifying values for the `name` and `email` fields.
253-
7. Verify the data in the application by refreshing your application at `https://localhost:3000`. The page should display the name of the user you added in Prisma Studio. For example, if you added a user named `Jon`, the application will display `Jon` in the browser.
306+
5. Verify the data in the application by opening `https://localhost:3000`. If you seeded the database, you should see the first user's name displayed.
254307

255308
Congratulations, your Nuxt app is now fully integrated with Prisma Postgres!
256309

257-
## 7. Deploy to Vercel
310+
## 8. Deploy to Vercel
258311

259312
Deploy your Nuxt application with Prisma Postgres integration to Vercel by following these steps:
260313

@@ -295,4 +348,4 @@ Congratulations! Your Nuxt application with Prisma Postgres integration is now l
295348

296349
## Considerations
297350

298-
This guide helps you get started with Prisma Postgres using the Nuxt module. Because the Nuxt module is actively evolving, it does not cover all of Prisma's features or support every edge case. For more advanced functionality or edge deployments, consider using Prisma directly.
351+
This guide helps you get started with Prisma Postgres in a Nuxt application. For more advanced functionality or edge deployments, explore Prisma’s broader guides and tailor the setup to your project’s needs.

0 commit comments

Comments
 (0)