CRUD Operations
createManyAndReturn
Insert multiple records and get results
Insert multiple records and receive the created records back.
const users = await client.user.createManyAndReturn({
data: [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
],
});
users.forEach((user) => {
console.log(`Created user: ${user.id} - ${user.name}`);
});Return Type
Returns an array of created records:
const users: User[] = await client.user.createManyAndReturn({
data: [
/* ... */
],
});Compare with createMany which only returns a count.
With Selection
Control which fields are returned:
const users = await client.user.createManyAndReturn({
data: [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
],
select: {
id: true,
name: true,
// email field excluded
},
});With Relations
Include related records:
const users = await client.user.createManyAndReturn({
data: [
{
name: "Alice",
profile: { create: { bio: "Alice's bio" } },
},
{
name: "Bob",
profile: { create: { bio: "Bob's bio" } },
},
],
include: { profile: true },
});
users.forEach((user) => {
console.log(`${user.name}: ${user.profile?.bio}`);
});With Options
const users = await client.user.createManyAndReturn(
{
data: [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
],
},
{
tx: transaction, // Use existing transaction
silent: false, // Emit events (default: false)
addToOutbox: true, // Queue for sync (default: true if sync is enabled)
}
);Processing Results
Chain operations on the returned records:
const users = await client.user.createManyAndReturn({
data: [
{ name: "Alice", email: "alice@example.com" },
{ name: "Bob", email: "bob@example.com" },
],
});
// Create todos for each user
for (const user of users) {
await client.todo.create({
data: {
title: `Welcome ${user.name}`,
userId: user.id,
},
});
}See Also
- createMany - Batch without getting results
- create - Create single record
- Transactions - Atomic operations