Prisma
1
2
What are we learning?
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
Prisma Parts
5
Setup a Prisma Project
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
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
Installing Prisma
-- save-exact will save prisma as only this version and not a newer one.
Initializes prisma and creates connection to DB
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
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
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
Prisma Types
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
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
Attributes
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
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
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
CRUD with our Client
Just import this index.js from our DB folder and use the variable to CRUD.
20
Prisma Methods
21
CRUD with our Client
We can include or JOIN other tables with our methods by using the include property.
22
Let’s go!
Design Thinking
23