⚠️ This lesson is retired and might contain outdated information.

Perform read operations using prisma client

Share this video with your friends

Social Share Links

Send Tweet

In this lesson you will learn how to perform the simplest findOne and findMany read operations from postgres database using prisma client.

Dimitri Ivashchuk: [0:00] In this lesson, we'll cover the basic read operations. I've created the users in my database. Let's see what we have there. psql Prisma Postgres to connect to other Postgres database. Then select all from users.

[0:19] You see that we have three entries for Dmitri, Alex, and Max. First of all, let's try to get all the users from the database and get the output here to see what we have. Let's do const users equals await Prisma.

[0:40] Then we want to go for users. Then we want to go for findMany. That'd be it, let's log the users. You see that we have the array of users coming straight from our database, but we have just written the neat short Prisma function, which already uses autocomplete, of course.

[1:05] The second step would be trying to read the user with some specific ID or name. Let's do that, const Dmitri equals await Prisma.users, findOne. Then here, we need to specify where. In where, we can pass the argument that we want to do the lookup with.

[1:38] In our case, we want to go for ID one. Let's log Dmitri. We see that we got the correct entry from database. As the ID is the unique field, we can only get the findOne to work with ID for now. If we would like to, for example, search for all users which have the name Alex, which at the same time can be that there are several users with unique IDs, but the same name Alex.

[2:10] It would be something like const allAlexUsers, and do await. Let's delete this one, so it doesn't distract us. Await Prisma.users.findMany, where name Alex. As easy as that. Now, when we log the allAlexUsers, we see that we got the correct ID, three, and name Alex in the array of all users with the correct query parameter.

[2:50] To verify that everything is working OK, let's now create another user with the same name, Alex, to see what we will get here. We do await Prisma.users.create, and as the data, we want to pass name Alex again.

[3:17] We have done that, and you see that straight ahead, we got the ID three, name Alex, and ID eight, name Alex, which is the newly-created entry in our database.