Use Prima Client to Select a Subset of Data

Ryan Chenkie
InstructorRyan Chenkie

Share this video with your friends

Send Tweet
Published 4 months ago
Updated 2 months ago

Prisma Client has a full API for selecting subsets of data in our tables. We can use the WHERE clause to match specific conditions for which we would like to return data. In this lesson, we look at how to use the WHERE and its operators to return a subset of data.

Instructor: [0:00] Prisma offers us a type-safe way to scope down our queries. If we have multiple records in a database table, which we most likely will have, we can use the WHERE clause to tell Prisma which records we would like to get back. In the call to findMany on product, supply a WHERE clause.

[0:16] The WHERE clause takes an object on which we can put a column name that were interested in. For example, we can put name. The column name then takes on objects on which we can use any of the operators that we might be interested in. For example, we might want to get products where name equals something like T-shirt. Save the query and test it out in the browser.

[0:40] We're not limited to strict equals comparisons. If we want to find a range, for example, on price, we can use the greater-than operator and supply a number to it. Let's get products where the price is greater than 1,000. We can supply multiple operators to do a compound query, for example, where the price is greater than 1,000, but less than 20,000.

[1:03] Because Prisma offers us a type-safe way to access our database, we won't be allowed to supply queries that are invalid. For example, Prisma knows that the price field is a number, so it won't allow us to pass a string here. If we try to do that, we'll get a red squiggly with a TypeError.