Use Prima Client to Select a Subset of Data

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 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.

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