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
+54-73Lines changed: 54 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,42 +1,31 @@
1
1
### Prisma
2
2
3
-
[Prisma](https://www.prisma.io) is an [open-source](https://github.com/prisma/prisma) database toolkit. You can use it to query data from a database inside a Node.js or TypeScript application. Prisma and NestJS go great together since you can use Prisma in your _services_ to fulfill the data needs from your _controllers_.
3
+
[Prisma](https://www.prisma.io) is an [open-source](https://github.com/prisma/prisma) database toolkit. You can use it to query data from a database inside a Node.js or TypeScript application. Prisma and NestJS go great together since you can use Prisma in your services to fulfill the data needs from your controllers.
4
4
5
-
Prisma is used as an alternative to writing plain SQL, or using another database access tool such as SQL query builders (like [knex.js](http://knexjs.org/)) or ORMs (like [TypeORM](http://typeorm.io/) and [Sequelize](https://sequelize.org/)). Prisma currently supports PostgreSQL, MySQL and SQLite.
5
+
Prisma is used as an **alternative** to writing plain SQL, or using another database access tool such as SQL query builders (like [knex.js](http://knexjs.org/)) or ORMs (like [TypeORM](http://typeorm.io/) and [Sequelize](https://sequelize.org/)). Prisma currently supports PostgreSQL, MySQL and SQLite.
6
6
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.
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/). 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.
9
+
> info **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/).
10
10
11
11
#### Getting started
12
12
13
-
In this recipe, you'll learn how to get started with NestJS and Prisma from scratch. You are going to build a sample NestJS application with a REST API that can read and write data in a database.
13
+
In this recipe, you'll learn how to get started with NestJS and Prisma from scratch. You are going to build a sample NestJS application with a REST API that can read and write data in a database.
14
14
15
15
For the purpose of this guide, you'll use a [SQLite](http://sqlite.org/) database to save the overhead of setting up a database server. Note that you can still follow this guide, even if you're using PostgreSQL or MySQL – you'll get extra instructions for using these databases at the right places.
16
16
17
-
In the following sections, you will:
18
-
19
-
1. Create your NestJS project
20
-
1. Set up Prisma
21
-
1. Create your SQLite database file and set the database connection
22
-
1. Create two database tables
23
-
1. Introspect your database to obtain your Prisma models in the Prisma schema
24
-
1. Install and generate Prisma Client
25
-
1. Use Prisma Client in your NestJS services
26
-
1. Implement your REST API routes in the main app controller
27
-
28
-
##### 1. Create your NestJS project
17
+
#### Create your NestJS project
29
18
30
19
To get started, install the NestJS CLI and create your app skeleton with the following commands:
31
20
32
21
```bash
33
-
npm install -g @nestjs/cli
34
-
nest create hello-prisma
22
+
$ npm install -g @nestjs/cli
23
+
$ nest new hello-prisma
35
24
```
36
25
37
-
See the [First steps](https://docs.nestjs.com/first-steps) page to learn more about the project files created by this command. Note also that you can now run `npm start` to start your application. The REST API running at `http://localhost:3000/` currently serves a single route that's implemented in `src/app.controller.ts`. Over the course of this guide, you'll implement additional routes to store and retrieve data about _users_ and _posts_.
26
+
See the [First steps](https://docs.nestjs.com/first-steps) page to learn more about the project files created by this command. Note also that you can now run `npm start` to start your application. The REST API running at `http://localhost:3000/` currently serves a single route that's implemented in `src/app.controller.ts`. Over the course of this guide, you'll implement additional routes to store and retrieve data about _users_ and _posts_.
38
27
39
-
##### 2. Set up Prisma
28
+
#### Set up Prisma
40
29
41
30
Start by installing the Prisma CLI as a development dependency in your project:
42
31
@@ -77,11 +66,11 @@ This command creates a new `prisma` directory with the following contents:
77
66
-`schema.prisma`: Specifies your database connection and contains the database schema
78
67
-`.env`: A [dotenv](https://github.com/motdotla/dotenv) file, typically used to store your database credentials in a group of environment variables
79
68
80
-
##### 3. Create your SQLite database file and set the database connection
69
+
#### Create your SQLite database file and set the database connection
81
70
82
71
SQLite databases are simple files; no server is required to use a SQLite database. You can therefore create a new SQLite database by manually creating a new file on your file system.
83
72
84
-
Navigate into the new `prisma` directory and create a file called `dev.db` inside of it.
73
+
Navigate into the new `prisma` directory and create a file called `dev.db` inside of it.
85
74
86
75
Next, open up `schema.prisma` and adjust the `provider` field of the `datasource` block to `sqlite`:
87
76
@@ -104,7 +93,7 @@ DATABASE_URL="file:./dev.db"
104
93
105
94
<details><summary>Expand if you're using PostgreSQL or MySQL</summary>
106
95
107
-
#### PostgreSQL
96
+
**PostgreSQL**
108
97
109
98
If you're using PostgreSQL, you have to adjust the `schema.prisma` and `.env` files as follows:
110
99
@@ -121,7 +110,6 @@ generator client {
121
110
}
122
111
```
123
112
124
-
125
113
**`.env`**
126
114
127
115
```bash
@@ -134,10 +122,9 @@ Replace the placeholders spelled in all uppercase letters with your database cre
If you want to learn how to set up a PostgreSQL database, you can follow this guide on [setting up a free PostgreSQL database on Heroku](https://dev.to/prisma/how-to-setup-a-free-postgresql-database-on-heroku-1dc1).
125
+
If you want to learn how to set up a PostgreSQL database, you can follow this guide on [setting up a free PostgreSQL database on Heroku](https://dev.to/prisma/how-to-setup-a-free-postgresql-database-on-heroku-1dc1).
138
126
139
-
140
-
#### MySQL
127
+
**MySQL**
141
128
142
129
If you're using MySQL, you have to adjust the `schema.prisma` and `.env` files as follows:
143
130
@@ -164,7 +151,7 @@ Replace the placeholders spelled in all uppercase letters with your database cre
164
151
165
152
</details>
166
153
167
-
##### 4. Create two database tables
154
+
#### Create two database tables
168
155
169
156
In this section, you'll create two new tables in your database. Run the following SQL statements in your terminal:
170
157
@@ -190,7 +177,7 @@ CREATE TABLE "Post" (
190
177
**Windows**
191
178
192
179
```bash
193
-
$ sqlite3 ./prisma/dev.db
180
+
$ sqlite3 ./prisma/dev.db
194
181
CREATE TABLE "User" (
195
182
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
196
183
"name" TEXT,
@@ -205,11 +192,11 @@ CREATE TABLE "Post" (
205
192
);
206
193
```
207
194
208
-
Note that Prisma also features a _schema migration_ tool called [Prisma Migrate](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-migrate). Prisma Migrate lets you manually define your models in your Prisma schema and takes care of creating the tables in your database. Because Prisma Migrate is currently considered experimental, this guide uses an alternative workflow of using plain SQL to create your database tables and generate Prisma models via [introspection](https://www.prisma.io/docs/reference/tools-and-interfaces/introspection).
195
+
Note that Prisma also features a **schema migration** tool called [Prisma Migrate](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-migrate). Prisma Migrate lets you manually define your models in your Prisma schema and takes care of creating the tables in your database. Because Prisma Migrate is currently considered experimental, this guide uses an alternative workflow of using plain SQL to create your database tables and generate Prisma models via [introspection](https://www.prisma.io/docs/reference/tools-and-interfaces/introspection).
209
196
210
-
##### 5. Introspect your database to obtain your Prisma models in the Prisma schema
197
+
#### Introspect your database to obtain your Prisma models in the Prisma schema
211
198
212
-
Now that you've created your database tables, you can _introspect_ the database to generate your [Prisma models](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/models). After that, you will install and generate Prisma Client, which will expose queries that are _tailored_ to these models.
199
+
Now that you've created your database tables, you can **introspect** the database to generate your [Prisma models](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/models). After that, you will install and generate Prisma Client, which will expose queries that are tailored to these models.
213
200
214
201
To introspect your database, run the following command in your terminal:
215
202
@@ -248,7 +235,7 @@ model Post {
248
235
249
236
With your Prisma models in place, you can install and generate Prisma Client.
250
237
251
-
##### 6. Install and generate Prisma Client
238
+
#### Install and generate Prisma Client
252
239
253
240
To install Prisma Client in your project, run the following command in your terminal:
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.
260
247
261
-
> **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`.
262
-
248
+
> info **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`.
263
249
264
-
##### 7. Use Prisma Client in your NestJS services
250
+
#### Use Prisma Client in your NestJS services
265
251
266
252
You're now able to send database queries with Prisma Client. If you want to learn more about building queries with Prisma Client, check out the [API documentation](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/crud).
267
253
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.
254
+
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
255
270
256
Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it:
271
257
@@ -276,9 +262,6 @@ import { PrismaClient } from '@prisma/client';
Your `UserService` and `PostService` currently wrap the CRUD queries that are available in Prisma Client. In a real world application, the service would also be the place to add business logic to your application. For example, you could have a method called `updatePassword` inside the `UserService` that would be responsible for updating the password of a user.
432
414
433
-
##### 8. Implement your REST API routes in the main app controller
415
+
##### Implement your REST API routes in the main app controller
434
416
435
417
Finally, you'll use the services you created in the previous sections to implement the different routes of your app. For the purpose of this guide, you'll put all your routes into the already existing `AppController` class.
436
418
@@ -457,19 +439,19 @@ export class AppController {
@@ -525,12 +507,11 @@ export class AppController {
525
507
526
508
This controller implements the following routes:
527
509
528
-
529
510
###### `GET`
530
511
531
512
-`/post/:id`: Fetch a single post by its `id`
532
513
-`/feed`: Fetch all _published_ posts
533
-
-`/filterPosts/:searchString`: Filter posts by `title` or `content`
514
+
-`/filter-posts/:searchString`: Filter posts by `title` or `content`
534
515
535
516
###### `POST`
536
517
@@ -556,4 +537,4 @@ This controller implements the following routes:
556
537
557
538
In this recipe, you learned how to use Prisma along with NestJS to implement a REST API. The controller that 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.
558
539
559
-
If you want to learn more about Prisma, be sure to check out the [documentation](https://www.prisma.io/docs/).
540
+
If you want to learn more about Prisma, be sure to check out the [documentation](https://www.prisma.io/docs/).
0 commit comments