Skip to content

Commit cede292

Browse files
committed
docs(prisma): update prisma getting started guide
1 parent a117444 commit cede292

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

content/recipes/prisma.md

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,20 @@ $ npx prisma init
6565
This command creates a new `prisma` directory with the following contents:
6666

6767
- `schema.prisma`: Specifies your database connection and contains the database schema
68+
- `prisma.config.ts`: A configuration file for your projects
6869
- `.env`: A [dotenv](https://github.com/motdotla/dotenv) file, typically used to store your database credentials in a group of environment variables
6970

7071
#### Set the generator output path
7172

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:
7574

7675
```groovy
7776
generator client {
78-
provider = "prisma-client-js"
79-
output = "../generated/prisma"
77+
provider = "prisma-client"
78+
output = "../src/generated/prisma"
8079
}
8180
```
8281

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`:
84-
85-
```json
86-
{
87-
"extends": "./tsconfig.json",
88-
"include": ["src", "generated"],
89-
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
90-
}
91-
```
9282

9383
#### Set the database connection
9484

@@ -97,12 +87,11 @@ Your database connection is configured in the `datasource` block in your `schema
9787
```groovy
9888
datasource db {
9989
provider = "sqlite"
100-
url = env("DATABASE_URL")
10190
}
10291
10392
generator client {
104-
provider = "prisma-client-js"
105-
output = "../generated/prisma"
93+
provider = "prisma-client"
94+
output = "../src/generated/prisma"
10695
}
10796
```
10897

@@ -129,12 +118,11 @@ If you're using PostgreSQL, you have to adjust the `schema.prisma` and `.env` fi
129118
```groovy
130119
datasource db {
131120
provider = "postgresql"
132-
url = env("DATABASE_URL")
133121
}
134122
135123
generator client {
136-
provider = "prisma-client-js"
137-
output = "../generated/prisma"
124+
provider = "prisma-client"
125+
output = "../src/generated/prisma"
138126
}
139127
```
140128

@@ -161,12 +149,11 @@ If you're using MySQL, you have to adjust the `schema.prisma` and `.env` files a
161149
```groovy
162150
datasource db {
163151
provider = "mysql"
164-
url = env("DATABASE_URL")
165152
}
166153
167154
generator client {
168-
provider = "prisma-client-js"
169-
output = "../generated/prisma"
155+
provider = "prisma-client"
156+
output = "../src/generated/prisma"
170157
}
171158
```
172159

@@ -187,12 +174,11 @@ If you're using Microsoft SQL Server or Azure SQL Server, you have to adjust the
187174
```groovy
188175
datasource db {
189176
provider = "sqlserver"
190-
url = env("DATABASE_URL")
191177
}
192178
193179
generator client {
194-
provider = "prisma-client-js"
195-
output = "../generated/prisma"
180+
provider = "prisma-client"
181+
output = "../src/generated/prisma"
196182
}
197183
```
198184

@@ -279,17 +265,39 @@ CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
279265

280266
#### Install and generate Prisma Client
281267

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.
283269

284270
To install Prisma Client in your project, run the following command in your terminal:
285271

286272
```bash
287273
$ npm install @prisma/client
288274
```
289275

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+
```
291287

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>
293301

294302
#### Use Prisma Client in your NestJS services
295303

@@ -300,19 +308,19 @@ When setting up your NestJS application, you'll want to abstract away the Prisma
300308
Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it:
301309

302310
```typescript
303-
import { Injectable, OnModuleInit } from '@nestjs/common';
304-
import { PrismaClient } from 'generated/prisma';
311+
import { Injectable } from '@nestjs/common';
312+
import { PrismaClient } from './generated/prisma/client';
313+
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
305314
306315
@Injectable()
307-
export class PrismaService extends PrismaClient implements OnModuleInit {
308-
async onModuleInit() {
309-
await this.$connect();
316+
export class PrismaService extends PrismaClient {
317+
constructor() {
318+
const adapter = new PrismaBetterSqlite3({ url: process.env.DATABASE_URL });
319+
super({ adapter });
310320
}
311321
}
312322
```
313323
314-
> info **Note** The `onModuleInit` is optional — if you leave it out, Prisma will connect lazily on its first call to the database.
315-
316324
Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema.
317325
318326
Still inside the `src` directory, create a new file called `user.service.ts` and add the following code to it:

0 commit comments

Comments
 (0)