CRUD Operations
updateMany
Modify multiple records at once
Update multiple records that match a filter.
const result = await client.user.updateMany({
where: { role: "admin" },
data: { lastLogin: new Date() },
});
console.log(`Updated ${result.count} records`);Basic Updates
Update records matching a filter:
// Mark all inactive users as archived
const result = await client.user.updateMany({
where: { active: false },
data: { archived: true },
});
console.log(`Updated ${result.count} users`);Complex Filters
Multiple conditions:
// Archive users inactive for 90 days
const result = await client.user.updateMany({
where: {
active: false,
lastLogin: {
lt: new Date(Date.now() - 90 * 24 * 60 * 60 * 1000),
},
},
data: { archived: true },
});Partial Updates
Only change specific fields:
// Update only the role field
const result = await client.user.updateMany({
where: { department: "sales" },
data: { role: "sales-lead" },
});Return Value
Returns count of updated records:
const result = await client.user.updateMany({
where: { active: false },
data: { archived: true },
});
console.log(result.count); // Number of updated recordsWith Options
const result = await client.user.updateMany(
{
where: { role: "user" },
data: { verified: true },
},
{
tx: transaction, // Use existing transaction
silent: false, // Emit events (default: false)
addToOutbox: true, // Queue for sync (default: true if sync is enabled)
}
);Bulk Operations
Update many records with different data:
// Update multiple records with specific changes
// Note: updateMany updates ALL matching records with SAME data
// For different updates per record, use update in a loop or use a transaction
const result = await client.todo.updateMany({
where: { done: false, priority: { lt: 3 } },
data: { priority: 5 }, // Same data for all
});For per-record updates, use a transaction:
const updated = await client.$transaction(async (tx) => {
const todos = await tx.todo.findMany({
where: { userId },
});
return Promise.all(
todos.map((todo) =>
tx.todo.update({
where: { id: todo.id },
data: { priority: calculatePriority(todo) },
})
)
);
});Batch Update Pattern
For simple bulk updates, use updateMany directly:
const result = await client.user.updateMany({
where: {
active: false,
lastLogin: { lt: ninetyDaysAgo },
},
data: { archived: true },
});
console.log(`Updated ${result.count} total records`);For real batching with pagination, fetch a page and update per page:
const batch = 100;
let updated = 0;
for (let skip = 0; ; skip += batch) {
const users = await client.user.findMany({
where: {
active: false,
lastLogin: { lt: ninetyDaysAgo },
},
skip,
take: batch,
});
if (users.length === 0) break;
const result = await client.user.updateMany({
where: {
id: { in: users.map((u) => u.id) },
},
data: { archived: true },
});
updated += result.count;
}
console.log(`Updated ${updated} total records`);Events
updateMany emits update events for each record (unless silent: true):
client.user.subscribe(["update"], (event) => {
console.log("User updated:", event.detail.id);
});
await client.user.updateMany({
where: { role: "user" },
data: { verified: true },
});
// Logs event for each updated userPerformance Notes
- updateMany is efficient - filters in memory, updates all at once
- Avoid massive updates without filters
- Use in transactions for atomic multi-model updates
Common Use Cases
Verify users in bulk:
const result = await client.user.updateMany({
where: { email: { in: verifiedEmails } },
data: { verified: true },
});Reset state:
// Clear all sessions for users who logged out
const result = await client.session.updateMany({
where: { active: true, userId: { in: loggedOutUserIds } },
data: { active: false, endedAt: new Date() },
});Update based on conditions:
// Promote users with high engagement
const result = await client.user.updateMany({
where: { tier: "free", engagementScore: { gte: 100 } },
data: { tier: "pro" },
});See Also
- update - Update a single record
- upsert - Update or create a single record
- Transactions - Atomic multi-record operations