1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Grab A Limited Set Of Records as a Prisma TIL

This commit is contained in:
jbranchaud
2022-05-25 10:09:44 -06:00
parent ab6278eb82
commit 81246ecb7b
2 changed files with 33 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
# Grab A Limited Set Of Records
Let's say you want to grab some records from a table, but you want to limit the
result set to 10 records.
You can do that with the `take` option.
```javascript
const posts = await prisma.post.findMany({
take: 10
});
```
It is generally good to not assume anything about the ordering. Instead, you
should be explicit about the order you want, so let's include an `orderBy` as
well.
```javascript
const posts = await prisma.post.findMany({
take: 10,
orderBy: { createdAt: "asc" },
});
```
This will return the 10 most recently created posts.
[source](https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#findmany)