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/recipes/prisma.md
+44-36Lines changed: 44 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -65,30 +65,20 @@ $ npx prisma init
65
65
This command creates a new `prisma` directory with the following contents:
66
66
67
67
-`schema.prisma`: Specifies your database connection and contains the database schema
68
+
-`prisma.config.ts`: A configuration file for your projects
68
69
-`.env`: A [dotenv](https://github.com/motdotla/dotenv) file, typically used to store your database credentials in a group of environment variables
69
70
70
71
#### Set the generator output path
71
72
72
-
> warning **Warning** In Prisma ORM 7, Prisma Client will no longer be generated in `node_modules` by default and will require an output path to be defined. [Learn more below on how to define an output path](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path).
73
-
74
-
Specify your output `path` for the generated Prisma client either by passing `--output ../generated/prisma` during prisma init, or directly in your Prisma schema:
73
+
Specify your output `path` for the generated Prisma client either by passing `--output ../src/generated/prisma` during prisma init, or directly in your Prisma schema:
75
74
76
75
```groovy
77
76
generator client {
78
-
provider = "prisma-client-js"
79
-
output = "../generated/prisma"
77
+
provider = "prisma-client"
78
+
output = "../src/generated/prisma"
80
79
}
81
80
```
82
81
83
-
By default, Nest does not include the generated Prisma client in the build. To fix this, the path should be explicitly defined in `tsconfig.build.json`:
@@ -97,12 +87,11 @@ Your database connection is configured in the `datasource` block in your `schema
97
87
```groovy
98
88
datasource db {
99
89
provider = "sqlite"
100
-
url = env("DATABASE_URL")
101
90
}
102
91
103
92
generator client {
104
-
provider = "prisma-client-js"
105
-
output = "../generated/prisma"
93
+
provider = "prisma-client"
94
+
output = "../src/generated/prisma"
106
95
}
107
96
```
108
97
@@ -129,12 +118,11 @@ If you're using PostgreSQL, you have to adjust the `schema.prisma` and `.env` fi
129
118
```groovy
130
119
datasource db {
131
120
provider = "postgresql"
132
-
url = env("DATABASE_URL")
133
121
}
134
122
135
123
generator client {
136
-
provider = "prisma-client-js"
137
-
output = "../generated/prisma"
124
+
provider = "prisma-client"
125
+
output = "../src/generated/prisma"
138
126
}
139
127
```
140
128
@@ -161,12 +149,11 @@ If you're using MySQL, you have to adjust the `schema.prisma` and `.env` files a
161
149
```groovy
162
150
datasource db {
163
151
provider = "mysql"
164
-
url = env("DATABASE_URL")
165
152
}
166
153
167
154
generator client {
168
-
provider = "prisma-client-js"
169
-
output = "../generated/prisma"
155
+
provider = "prisma-client"
156
+
output = "../src/generated/prisma"
170
157
}
171
158
```
172
159
@@ -187,12 +174,11 @@ If you're using Microsoft SQL Server or Azure SQL Server, you have to adjust the
187
174
```groovy
188
175
datasource db {
189
176
provider = "sqlserver"
190
-
url = env("DATABASE_URL")
191
177
}
192
178
193
179
generator client {
194
-
provider = "prisma-client-js"
195
-
output = "../generated/prisma"
180
+
provider = "prisma-client"
181
+
output = "../src/generated/prisma"
196
182
}
197
183
```
198
184
@@ -279,17 +265,39 @@ CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
279
265
280
266
#### Install and generate Prisma Client
281
267
282
-
Prisma Client is a type-safe database client that's _generated_ from your Prisma model definition. Because of this approach, Prisma Client can expose [CRUD](https://www.prisma.io/docs/concepts/components/prisma-client/crud) operations that are _tailored_ specifically to your models.
268
+
Prisma Client is a type-safe database client that's _generated_ from your Prisma model definition. Because of this approach, Prisma Client can expose [CRUD](https://www.prisma.io/docs/concepts/components/prisma-client/crud) operations that are _tailored_ specifically to your models.
283
269
284
270
To install Prisma Client in your project, run the following command in your terminal:
285
271
286
272
```bash
287
273
$ npm install @prisma/client
288
274
```
289
275
290
-
Note that during installation, Prisma automatically invokes the `prisma generate` command for you. In the future, you need to run this command after _every_ change to your Prisma models to update your generated Prisma Client.
276
+
Once installed, you can run the generate command to generate the types and Client needed for your project. If any changes are made to your schema, you will need to rerun the `generate` command to keep those types in sync.
277
+
278
+
```bash
279
+
$ npx prisma generate
280
+
```
281
+
282
+
In addition to Prisma Client, you also need to a driver adapter for the type of database you are working with. For SQLite, you can install the `@prisma/adapter-better-sqlite3` driver.
283
+
284
+
```bash
285
+
npm install @prisma/adapter-better-sqlite3
286
+
```
291
287
292
-
> info **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`.
288
+
<details> <summary>Expand if you're using PostgreSQL, MySQL, MsSQL, or AzureSQL</summary>
289
+
290
+
- For PostgreSQL
291
+
```bash
292
+
npm install @prisma/adapter-pg
293
+
```
294
+
295
+
- For MySQL, MsSQL, AzureSQL:
296
+
```bash
297
+
npm install @prisma/adapter-mariadb`
298
+
```
299
+
300
+
</details>
293
301
294
302
#### Use Prisma Client in your NestJS services
295
303
@@ -300,19 +308,19 @@ When setting up your NestJS application, you'll want to abstract away the Prisma
300
308
Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it:
0 commit comments