CRUD Operations
findMany
Query multiple records with filtering and pagination
Retrieve multiple records from a model store.
// Get all records
const users = await client.user.findMany();
// With filtering
const users = await client.user.findMany({
where: { active: true },
});Filtering
Use the where clause to filter records:
// Equality
const users = await client.user.findMany({
where: { name: "Alice" },
});
// Comparisons
const users = await client.user.findMany({
where: { age: { gt: 18 } }, // greater than
});
const users = await client.user.findMany({
where: { priority: { gte: 5, lte: 10 } }, // between
});
// Multiple conditions (AND)
const users = await client.user.findMany({
where: {
active: true,
age: { gt: 18 },
},
});Sorting
Use orderBy to sort results:
// Ascending (default)
const users = await client.user.findMany({
orderBy: { createdAt: "asc" },
});
// Descending
const users = await client.user.findMany({
orderBy: { name: "desc" },
});
// Multiple fields
const users = await client.user.findMany({
orderBy: [{ active: "desc" }, { name: "asc" }],
});Pagination
Use skip and take for pagination:
const pageSize = 10;
const page = 1;
const users = await client.user.findMany({
skip: (page - 1) * pageSize,
take: pageSize,
});Selection
Control which fields are included using explicit inclusion:
// Select specific fields
const users = await client.user.findMany({
select: { id: true, name: true },
});Relations
Include related records:
const users = await client.user.findMany({
include: { profile: true },
});
// Nested relations
const users = await client.user.findMany({
include: {
profile: {
include: { settings: true },
},
},
});
// Filtered relations
const users = await client.user.findMany({
include: {
todos: {
where: { done: false },
orderBy: { createdAt: "desc" },
},
},
});Combined Example
const recentIncompleteTodos = await client.todo.findMany({
where: {
done: false,
createdAt: { gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) }, // Last 7 days
},
select: {
id: true,
title: true,
priority: true,
},
orderBy: { priority: "desc" },
take: 10,
});With Transactions
const tx = client._db.transaction(["user", "profile"], "readonly");
const users = await client.user.findMany({ include: { profile: true } }, { tx });
await tx.done;Performance Notes
- Filtering: Complex
whereclauses are evaluated in-memory after fetching. Use indexed fields when possible. - Sorting: Large result sets with sorting may be slow. Consider pagination.
- Relations: Each relation adds overhead. Be specific with
selectwhen possible.
See Also
- findFirst - Get first matching record
- findUnique - Get by unique field
- count - Count matching records
- Transactions - Atomic operations