Create a Database Record with Prisma Client

Ryan Chenkie
InstructorRyan Chenkie
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Prisma Client offers methods to create new database records. These methods accept a data object and return the result to be consumed by clients. In this lesson, we use Prisma Client's create method to create new records in the database.

Ryan Chenkie: [0:00] Prisma gives us methods to create data. We can create an endpoint here to add new products. Let's copy over the get endpoint. We'll make it a post endpoint going to products. In the handler, we can add in new products.

[0:13] Our constant of product now, singular, will be a call to await prisma.product.create(). The create method needs an object, on which we need to have a data key. Supply an object to the create method, put in a data key, and then on the data object, put in values for all of the columns that we'll need.

[0:33] On Mac OS, we can do Control-Space bar to find the property names that are required. We can supply name, description, and price.

[0:48] Save the call to create and then open up a res client to make a POST request to the products endpoint. To stick within VS Code, you can try Thunder Client. It's an extension that you can install in the Extensions Marketplace.

[1:03] We get back the new record with a unique id, and we can verify within Prisma Studio that the record was created.

egghead
egghead
~ 22 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