Perform basic database queries (CRUD) with Prisma Client

Xiaoru Li
InstructorXiaoru Li

Share this video with your friends

Send Tweet
Published 2 years ago
Updated a year ago

Apart from creating entries, it's also very intuitive to read, update, or delete data from the database with Prisma Client.

In this lesson, we will explore the Prisma Client API for findMany, findOne, updateMany, deleteMany and so on.

Notice that since all these methods are tailored to the schema that we defined earlier, VS Code is aware of what is valid and is able to prompt us with helpful autocomplete options, making it much easier to explore the Prisma Client API.

Xiaoru Li: [0:01] Apart from creating entries, it's also very intuitive to read, update, or delete data from the database with Prisma Client. Let's first populate the database with some more dummy entries to work with.

[0:21] We can use findMany to return all the profile entries. We can also add constraints to filter or search for the entries we care about. For example, only the profiles where the name contains a capital letter A.

[0:45] We can also use findOne to search for a unique entry. Because right now the only unique field in the profile data model is id, only this option is available.

[1:05] Similarly, we can perform single entry updates with new data or perform a batch update with updateMany. This method will return the number of entries affected. We'll use the findMany method again to get all the entries printed out.

[1:41] We can also delete entries with deleteMany and some constraints. Finally, let's clean up the database, so later on we won't have issues with the migration.