You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/200-orm/800-more/600-help-and-troubleshooting/900-prisma-nuxt-module.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,10 @@ The Nuxt Prisma module simplifies the integration of Prisma ORM into your Nuxt a
11
11
12
12
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.
13
13
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
+
14
18
## Features
15
19
16
20
-**Project initialization**: Automatically sets up a Prisma ORM project with a SQLite database within your Nuxt project.
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.
16
16
17
17
Here's what you'll learn:
18
18
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.
21
21
- How to deploy the project to Vercel.
22
22
23
23
## Prerequisites
@@ -30,36 +30,21 @@ To follow this guide, ensure you have the following:
30
30
-[Vercel](https://vercel.com)
31
31
- Basic knowledge of Git and Vercel deployment (helpful but not required).
32
32
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
34
34
35
35
1. Initialize [a new Nuxt project](https://nuxt.com/docs/getting-started/installation#new-project), select `npm` as the package manager and initialize git:
36
36
```terminal
37
37
npm create nuxt hello-world
38
38
```
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:
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:
49
46
```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:
@@ -68,62 +53,36 @@ Before running the development server, create a `prisma.config.ts` file in the r
68
53
69
54
```typescript file=prisma.config.ts
70
55
import'dotenv/config'
71
-
import { defineConfig, env } from'prisma/config';
56
+
import { defineConfig, env } from'prisma/config'
72
57
73
58
exportdefaultdefineConfig({
74
59
schema: 'prisma/schema.prisma',
75
60
migrations: {
76
61
path: 'prisma/migrations',
62
+
seed: 'tsx ./prisma/seed.ts',
77
63
},
78
64
datasource: {
79
65
url: env('DATABASE_URL'),
80
66
},
81
-
});
67
+
})
82
68
```
83
69
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
95
71
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.
97
73
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:
117
75
```prisma file=prisma/schema.prisma
118
76
// This is your Prisma schema file,
119
77
// learn more about it in the docs: https://pris.ly/d/prisma-schema
120
78
121
79
generator client {
122
80
provider = "prisma-client"
81
+
output = "./generated"
123
82
}
124
83
125
84
datasource db {
126
-
provider = "sqlite"
85
+
provider = "postgresql"
127
86
}
128
87
129
88
model User {
@@ -142,39 +101,52 @@ Once the setup flow has terminated, it:
142
101
authorId Int
143
102
}
144
103
```
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.
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.
155
113
156
114
## 4. Update the application code
157
115
158
116
With Prisma configured, the next step is to update your application code to fetch and display data from your database.
159
117
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:
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:
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:
<p>{{ user?.name ?? "No user has been added yet." }}</p>
172
148
</template>
173
149
```
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
-
:::
178
150
179
151
3. Modify the `app.vue` file in the root directory to include the new server component using Nuxt Islands:
180
152
```html file=app.vue
@@ -198,7 +170,93 @@ With Prisma configured, the next step is to update your application code to fetc
198
170
199
171
By completing these steps, your application is now capable of fetching data from your Prisma database and rendering it on the frontend.
200
172
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:
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:
245
295
```terminal
246
296
npx prisma migrate dev --name init
247
297
```
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:
249
303
```terminal
250
304
npm run dev
251
305
```
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.
254
307
255
308
Congratulations, your Nuxt app is now fully integrated with Prisma Postgres!
256
309
257
-
## 7. Deploy to Vercel
310
+
## 8. Deploy to Vercel
258
311
259
312
Deploy your Nuxt application with Prisma Postgres integration to Vercel by following these steps:
260
313
@@ -295,4 +348,4 @@ Congratulations! Your Nuxt application with Prisma Postgres integration is now l
295
348
296
349
## Considerations
297
350
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