1 of 23

Prisma

1

2 of 23

2

What are we learning?

  1. What is Prisma?
  2. How to setup a Prisma project.
  3. Define models with Prisma.
  4. Generate a Prisma Client and connect to DB.

3 of 23

3

What is Prisma?

Prisma is a powerful ORM (Object-Relational Mapper) that helps you simplify database access in your applications. It supports multiple databases like PostgreSQL, MySQL, and MongoDB, and integrates well with TypeScript.

In this unit, we will learn all the fundamentals of working with Prisma, including how to set up Prisma in our applications, define the structure of our database, write queries to interact with the database, and manage changes in the structure of the database as our application evolves.

4 of 23

4

Prisma Parts

5 of 23

5

Setup a Prisma Project

  1. First we will utilize the ironlauncher from Ironhack to setup an Express API with Mongoose.
  2. We will modify the existing code to incorporate Prisma and uninstall Mongoose.
  3. Once the code is modified to use Prisma instead of Mongoose, now we can begin to define our models with Prisma and the relationship between them.
  4. After we are happy with the models shape, then we are going to use a tool given to us by Prisma to ‘Generate’ a Prisma Client.
  5. The Client will connect to our Database and we will be able to import it into other files to use in our routes to CRUD our models.
  6. Migrate is another tool that Prisma gives us, this tool create all of the SQL code that we need to effectively change or create our Database. (make sure they are structured in a way that allows the migration)
  7. After we have our Database created and we are ready to add data, we can use another great tool from Prisma called ‘Prisma Studio’ This is a GUI that will let us see our Database.

6 of 23

6

Ironlauncher

npx will execute directly the ironlauncher package

@latest will always use the most up to date version

The third argument is the name of your folder

--json will select the json question(creating an API server only)

7 of 23

7

Removing Mongoose

After doing these 4 steps, your project will run and listen on port 5005 but it will have no connection to any database. In the next steps we will setup Prisma to connect instead of Mongoose.

8 of 23

8

Installing Prisma

-- save-exact will save prisma as only this version and not a newer one.

Initializes prisma and creates connection to DB

9 of 23

9

Update .env

prisma init also created a variable in the .env for us, named DATABASE_URL, this is the connection string to our postgresql DB but we need to modify it with our personal information.

Note: Make sure .env is inside the .gitignore file

10 of 23

10

Prisma folder

The init command created a new folder named prisma for us, inside this folder you will find your schema.prisma file. Inside this file you will find the boiler plate code for your ‘client’ & ‘DB Connection’

11 of 23

11

Creating a Model

Under our boilerplate code we can start to define our models. These will describe the shapes of our tables. It needs to be written in prismas easy to understand language and prisma will ‘generate’ the SQL code.

12 of 23

12

Prisma Types

13 of 23

13

Modifiers

There are two types of modifiers, the ‘?’ means the field is optional and the [ ] means it will be an array of that type

14 of 23

14

Formatting Prisma Files

Prisma gives us a tool that will format our file for us, or we can add this code in the settings.json to configure our vs code on save for us.

15 of 23

15

Attributes

16 of 23

16

Migrating our DB

Prisma gives us a tool that will migrate or update our existing DB. It will also keep track of the previous migrations. This command will generate all of our SQL code and also it will create the Prisma client that we will use to CRUD data.

Note: migrate will not work if the current data that you have in the DB does not fit the new shape. You can drop the DB to start fresh.

17 of 23

17

Prisma Studio

Prisma gives us a tool that will help us visually see the Database. We only need to run the command and it will open in the browser for us and we can CRUD all the data in the DB much like with Mongo DB Compass.

18 of 23

18

Connect to DB

Now we have a client, we can put a new instance of our client and then export it so we can use it in our routes. This prisma variable is what is going to be used to CRUD.

19 of 23

19

CRUD with our Client

Just import this index.js from our DB folder and use the variable to CRUD.

20 of 23

20

Prisma Methods

21 of 23

21

CRUD with our Client

We can include or JOIN other tables with our methods by using the include property.

22 of 23

22

Let’s go!

Design Thinking

23 of 23

23