Create a Database Record with Prisma Client

Ryan Chenkie
InstructorRyan Chenkie
Share this video with your friends

Social Share Links

Send Tweet

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.