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
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Prisma is used as an alternative to writing plain SQL, or using another database
6
6
7
7
While Prisma is a _toolkit_ that contains _multiple_ tools, the focus of this guide will be on using [Prisma Client](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client). Prisma Client is an auto-generated and type-safe query builder that lets you read and write data in your database.
8
8
9
-
> **Note** If you want to get a quick overview of how Prisma works, you can follow the [Quickstart](https://www.prisma.io/docs/getting-started/quickstart) or read the [Introduction](https://www.prisma.io/docs/understand-prisma/introduction) in the [documentation](https://www.prisma.io/docs/).
9
+
> **Note** If you want to get a quick overview of how Prisma works, you can follow the [Quickstart](https://www.prisma.io/docs/getting-started/quickstart) or read the [Introduction](https://www.prisma.io/docs/understand-prisma/introduction) in the [documentation](https://www.prisma.io/docs/). There also is a great [`nestjs-prisma-starter`](https://github.com/fivethree-team/nestjs-prisma-starter) project available if you want to get started using NestJS with Prisma in production.
10
10
11
11
#### Getting started
12
12
@@ -260,14 +260,14 @@ Note that this command automatically invokes the `prisma generate` command for y
260
260
261
261
> **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`.
262
262
263
+
263
264
##### 7. Use Prisma Client in your NestJS services
264
265
265
266
You're now able to send database queries with Prisma Client! If you want to learn about the possible queries with Prisma Client, check out the [API documentation](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud).
266
267
267
-
268
268
When setting up your NestJS application, you'll want to abstract away the Prisma Client API for database queries within a _service_. To get started, you can create a new `PrismaService` that takes care of instantiating `PrismaClient` and connecting to your database.
269
269
270
-
Inside the `src`dirtectory, create a new file called `prisma.service.ts` and add the following code to it:
270
+
Inside the `src`directory, create a new file called `prisma.service.ts` and add the following code to it:
@@ -291,7 +291,7 @@ export class PrismaService extends PrismaClient
291
291
292
292
Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema.
293
293
294
-
Still inside the `src`dirtectory, create a new file called `user.service.ts` and add the following code to it:
294
+
Still inside the `src`directory, create a new file called `user.service.ts` and add the following code to it:
295
295
296
296
```typescript
297
297
import { Injectable } from'@nestjs/common';
@@ -361,7 +361,8 @@ Notice how you're using Prisma Client's generated types to ensure that the metho
361
361
362
362
Now do the same for the `Post` model.
363
363
364
-
Still inside the `src` dirtectory, create a new file called `post.service.ts` and add the following code to it:
364
+
365
+
Still inside the `src` directory, create a new file called `post.service.ts` and add the following code to it:
365
366
366
367
```typescript
367
368
import { Injectable } from'@nestjs/common';
@@ -522,13 +523,14 @@ export class AppController {
522
523
}
523
524
```
524
525
525
-
This implements the following routes:
526
+
This controller implements the following routes:
527
+
526
528
527
529
###### `GET`
528
530
529
531
-`/post/:id`: Fetch a single post by its `id`
530
532
-`/feed`: Fetch all _published_ posts
531
-
-`/filterPosts?searchString={searchString}`: Filter posts by `title` or `content`
533
+
-`/filterPosts/:searchString`: Filter posts by `title` or `content`
532
534
533
535
###### `POST`
534
536
@@ -550,6 +552,7 @@ This implements the following routes:
550
552
551
553
-`/post/:id`: Delete a post by its `id`
552
554
555
+
553
556
#### Summary
554
557
555
558
In this guide, you learned how to implement a use NestJS together with Prisma to implement a REST API. The controller who implements the routes of the API is calling a `PrismaService` which in turn uses Prisma Client to send queries to a database to fulfill the data needs of incoming requests.
0 commit comments