Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

findMany

Find multiple records matching your criteria with filtering and pagination

findMany returns every record matching a filter, as an array.

const users = await client.user.findMany();
// Result: all users

With Filtering

const activeAdmins = await client.user.findMany({
  where: {
    role: "ADMIN",
    active: true,
  },
});

See Filtering for the full operator reference.

With Sorting

const users = await client.user.findMany({
  orderBy: { createdAt: "desc" },
});

See Sorting for multi-field, relation, and aggregate ordering.

With Pagination

const page2 = await client.user.findMany({
  take: 10,
  skip: 1,  // Skip the cursor itself
  cursor: { id: lastId },
  orderBy: { id: "asc" },
});

See Pagination for offset and cursor pagination.

All Options

await client.user.findMany({
  where: { ... },           // Filter conditions
  orderBy: { ... },         // Sort order
  take: 10,                 // Max records
  skip: 0,                  // Offset
  cursor: { id: "..." },    // Cursor for pagination
  select: { ... },          // Fields to return
  include: { ... },         // Relations to include
  distinct: ["field"],      // Distinct by field
});

select and include control the returned shape — see Selecting Fields.

A negative take returns the last N records in the order. It works at the top level and inside a nested relation, in both the include and the select spelling — as does a nested cursor, which composes with skip and take the same way it does at the top level:

// The last two posts of every user
await client.user.findMany({
  include: { posts: { orderBy: { id: "asc" }, take: -2 } },
});

// A cursor page of posts, per user, through select
await client.user.findMany({
  select: {
    id: true,
    posts: { orderBy: { id: "asc" }, cursor: { id: lastId }, skip: 1, take: 2 },
  },
});

Results always come back in the query’s logical order, so don’t reverse a negative-take page yourself.

Return Type

// Default: all scalars
const users = await client.user.findMany();
// Type: { id: string; email: string; name: string; ... }[]

// With select: only selected fields
const users = await client.user.findMany({
  select: { id: true, email: true },
});
// Type: { id: string; email: string }[]

// With include: scalars + included relations
const users = await client.user.findMany({
  include: { posts: true },
});
// Type: { id: string; ...; posts: Post[] }[]

Examples

Search with Filters

async function searchUsers(query: string, role?: string) {
  return client.user.findMany({
    where: {
      AND: [
        {
          OR: [
            { name: { contains: query, mode: "insensitive" } },
            { email: { contains: query, mode: "insensitive" } },
          ],
        },
        role ? { role } : {},
      ],
    },
    take: 20,
  });
}

Was this page helpful?