Skip to content

Commit

Permalink
Add updateManyAndReturn to crud and prisma client reference (#6568)
Browse files Browse the repository at this point in the history
* adds updateManyAndReturn to crud and prisma client reference

* Update content/200-orm/200-prisma-client/100-queries/030-crud.mdx

Co-authored-by: Jon Harrell <[email protected]>

* Update content/200-orm/500-reference/050-prisma-client-reference.mdx

Co-authored-by: Jon Harrell <[email protected]>

* add notes about updateManyAndReturn

* minor wording improvment

---------

Co-authored-by: Nikolas <[email protected]>
Co-authored-by: Jon Harrell <[email protected]>
  • Loading branch information
3 people authored Jan 7, 2025
1 parent e166ddd commit f008253
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,7 @@ Every model in the data model definition will result in a number of CRUD queries
- [`createMany()`](/orm/reference/prisma-client-reference#createmany)
- [`createManyAndReturn()`](/orm/reference/prisma-client-reference#createmanyandreturn)
- [`updateMany()`](/orm/reference/prisma-client-reference#updatemany)
- [`updateManyAndReturn()`](/orm/reference/prisma-client-reference#updatemanyandreturn)
- [`deleteMany()`](/orm/reference/prisma-client-reference#deletemany)

The operations are accessible via a generated property on the Prisma Client instance. By default the name of the property is the lowercase form of the model name, e.g. `user` for a `User` model or `post` for a `Post` model.
Expand Down
57 changes: 56 additions & 1 deletion content/200-orm/200-prisma-client/100-queries/030-crud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,6 @@ const updateUsers = await prisma.user.updateMany({
},
})
```

</cmd>
<cmdResult>

Expand All @@ -694,6 +693,62 @@ const updateUsers = await prisma.user.updateMany({
</cmdResult>
</CodeWithResult>

### Update and return multiple records

:::info

This feature is available in Prisma ORM version 6.2.0 and later for PostgreSQL, CockroachDB, and SQLite.

:::

You can use `updateManyAndReturn()` in order to update many records and return the resulting objects.

<CodeWithResult outputResultText="query">
<cmd>

```ts
const users = await prisma.user.updateManyAndReturn({
where: {
email: {
contains: 'prisma.io',
}
},
data: {
role: 'ADMIN'
}
})
```

</cmd>
<cmdResult>

```js no-copy
[{
id: 22,
name: 'Alice',
email: '[email protected]',
profileViews: 0,
role: 'ADMIN',
coinflips: []
}, {
id: 23,
name: 'Bob',
email: '[email protected]',
profileViews: 0,
role: 'ADMIN',
coinflips: []
}]
```

</cmdResult>
</CodeWithResult>

:::warning

`relationLoadStrategy: join` is not available when using `updateManyAndReturn()`.

:::

### Update _or_ create records

The following query uses [`upsert()`](/orm/reference/prisma-client-reference#upsert) to update a `User` record with a specific email address, or create that `User` record if it does not exist:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ const updatedPost: Post = await prisma.post.update({

The following bulk operations run as transactions:

- `deleteMany()`
- `updateMany()`
- `createMany()`
- `createManyAndReturn()`
- `updateMany()`
- `updateManyAndReturn()`
- `deleteMany()`

> Refer to the section about [bulk operations](#bulk-operations) for more examples.
Expand Down Expand Up @@ -776,10 +777,12 @@ Depending on your requirements, Prisma Client has four options for handling inde

Bulk writes allow you to write multiple records of the same type in a single transaction - if any operation fails, Prisma Client rolls back the entire transaction. Prisma Client currently supports:

- `updateMany()`
- `deleteMany()`
- `createMany()`
- `createManyAndReturn()`
- `updateMany()`
- `updateManyAndReturn()`
- `deleteMany()`


#### When to use bulk operations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ It is generally more performant to read and write large amounts of data in bulk
- [`createManyAndReturn()`](/orm/reference/prisma-client-reference#createmanyandreturn)
- [`deleteMany()`](/orm/reference/prisma-client-reference#deletemany)
- [`updateMany()`](/orm/reference/prisma-client-reference#updatemany)
- [`updateManyAndReturn()`](/orm/reference/prisma-client-reference#updatemanyandreturn)
- [`findMany()`](/orm/reference/prisma-client-reference#findmany)

## Reuse `PrismaClient` or use connection pooling to avoid database connection pool exhaustion
Expand Down
64 changes: 62 additions & 2 deletions content/200-orm/500-reference/050-prisma-client-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,7 @@ const users = await prisma.user.createMany({

:::info

`createManyAndReturn()` is only available in Prisma ORM version 5.14.0 and up.
`createManyAndReturn()` is only available for PostgreSQL, CockroachDB, and SQLite.
This feature is available in Prisma ORM version 5.14.0 and later for PostgreSQL, CockroachDB and SQLite.

:::

Expand Down Expand Up @@ -1371,6 +1370,67 @@ const updatedUserCount = await prisma.user.updateMany({
});
```

### `updateManyAndReturn()`

:::info

This feature is available in Prisma ORM version 6.2.0 and later for PostgreSQL, CockroachDB and SQLite.

:::

`updateManyAndReturn` updates multiple records and returns the resulting objects.

#### Options

| Name | Type | Required | Description |
| ------- | ----------------------------------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `data` | `XOR<UserUpdateManyMutationInput,`<br />`UserUncheckedUpdateManyInput>` | **Yes** | Wraps all the fields of the model so that they can be provided when updating an existing record. Fields that are marked as optional or have default values in the datamodel are optional on `data`. |
| `where` | `UserWhereInput` | No | Wraps _all_ fields of a model so that the list can be filtered by any property. If you do not filter the list, all records will be updated. |


#### Return type

| Return type | Example | Description |
| ----------- | ------- | ----------- |
| JavaScript array object (typed) | `User[]` | |
| JavaScript array object (plain) | `[{ name: "Sonali" }]` | Use `select`, `omit` and `include` to determine which fields to return. |

#### Examples

##### Update and return multiple users

<CodeWithResult expanded="{true}">

<cmd>

```ts
const users = await prisma.user.updateManyAndReturn({
where: {
email: {
contains: 'prisma.io',
}
},
data: [
{ role: 'ADMIN' }
],
})
```

</cmd>

<cmdResult>

```json no-copy
[
{ "id": 0, "name": "Sonali", "email": "[email protected]", "role": "ADMIN", "profileViews": 0 },
{ "id": 1, "name": "Alex", "email": "[email protected]", "role": "ADMIN", "profileViews": 0 }
]
```

</cmdResult>

</CodeWithResult>

### `deleteMany()`

`deleteMany` deletes multiple records in a transaction.
Expand Down

0 comments on commit f008253

Please sign in to comment.