Install and Model Data with Prisma

Ian Jones
InstructorIan Jones
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

Most applications need data persisted to a database. A really great way to manage data is through an (Object-relational mapping) ORM so you aren’t stuck writing SQL queries when you want to be building websites.

Prisma is a modern ORM that lets you get started with a database in just a couple steps. It supports TypeScript to which you will take advantage of.

In this lesson you will initialize Prisma in your application and model out what a Post will look like. You’ll set a few properties that Prisma will manage (e.g. id, createdAt) and a few that you will manage. Prisma also comes with a studio that you can run to create and manage data super easily when you don’t have other means to CRUD that data (we don’t yet!).

Instructor: [0:00] It's time to add a database to your project. Prisma is one of the leading object-relational mapping tools or O/RMs for typescript and node.js.

[0:11] Over in your terminal, install the Prisma CLI tool with the command npm i prisma --save-dev. With the CLI installed, you can initialize Prisma with the command npx prisma init passing the data source provider flag with the value of SQLite.

[0:36] SQLite can speed up your development process by avoiding installation of heavy database software, such as Postgres and MySQL. SQLite uses a single file to store your database information. Later in the course, you will replace SQLite with a MySQL database. By running the init command, you created a Prisma folder in your project.

[1:04] When you run the OS Prisma command in your terminal, you will see a schema.prisma file. This file tells Prisma everything it needs to know about your data model. Go ahead and open that file in your editor.

[1:19] By default, Prisma defines the type of client your app will use and the data source provider information. Here is where things get fun. You should add a Post model to your schema. The first attribute you will add is an ID. It's going to be a string with a default of UUID.

[1:43] UUIDs are nice because they are harder to guess compared to auto incrementing number IDs. The next two fields createdAt and updatedAt, will be managed by Prisma as well. When you create a post, createdAt will be set to the current time.

[2:02] Likewise, Prisma will update the value of updatedAt whenever you modify a post. These fields are standard to almost all of the models you will create. Now it's time to add your custom fields. You'll add an optional title of type string.

[2:24] The question mark at the end of string signifies the fields optionality. Your post will need more than just a title. Add a required body field of type string. While you've defined your schema, you haven't told Prisma about this new model yet. Head back over to your terminal and run the command npx prisma db push.

[2:53] This created a SQLite dev.db file in your Prisma folder. It's finally time to add data to your database. To do this, you will use a tool called Prisma Studio. In your terminal, type npx prisma studio, and open the URL at localhost:5555.

[3:27] Here, you can see the post model you've created. Go ahead and click on Posts, then Add Record. You can type something like, "Persistence yay" into the body field. Now, add your first DB post to the title and save again.

[3:48] Time to celebrate because you just added your first post to your Prisma database. In review, you added the Prisma's CLI tool to your devDependencies. You ran the npx prisma init command with SQLite as your data source provider.

[4:04] Next, you opened up the schema.prisma file and added a post model to it. The post type has five fields. These fields being ID, createdAt, updatedAt, title, and body. Three of them managed by Prisma, and two of your own.

[4:22] Finally, you started Prisma Studio with npx prisma studio and opened it up in the browser, where you were able to add your own content to your database.

egghead
egghead
~ 27 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today