-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds updateManyAndReturn to crud and prisma client reference
- Loading branch information
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -681,7 +681,6 @@ const updateUsers = await prisma.user.updateMany({ | |
}, | ||
}) | ||
``` | ||
|
||
</cmd> | ||
<cmdResult> | ||
|
||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|