Skip to content

Commit

Permalink
adds updateManyAndReturn to crud and prisma client reference
Browse files Browse the repository at this point in the history
  • Loading branch information
chenkie committed Jan 3, 2025
1 parent 38b134e commit ead24e8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
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
62 changes: 62 additions & 0 deletions content/200-orm/500-reference/050-prisma-client-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,68 @@ const updatedUserCount = await prisma.user.updateMany({
});
```

### `updateManyAndReturn()`

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

:::info

`updateManyAndReturn()` is only available in Prisma ORM version 6.2.0 and up.
`updateManyAndReturn()` is only available for PostgreSQL, CockroachDB, and SQLite.

:::

#### 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 ead24e8

Please sign in to comment.