diff --git a/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx b/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx index fa4b9d6329..f8b2711c3e 100644 --- a/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx +++ b/content/200-orm/100-prisma-schema/20-data-model/10-models.mdx @@ -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. diff --git a/content/200-orm/200-prisma-client/100-queries/030-crud.mdx b/content/200-orm/200-prisma-client/100-queries/030-crud.mdx index ce7cfa30ac..023b53fdaa 100644 --- a/content/200-orm/200-prisma-client/100-queries/030-crud.mdx +++ b/content/200-orm/200-prisma-client/100-queries/030-crud.mdx @@ -681,7 +681,6 @@ const updateUsers = await prisma.user.updateMany({ }, }) ``` - @@ -694,6 +693,62 @@ const updateUsers = await prisma.user.updateMany({ +### 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. + + + + +```ts +const users = await prisma.user.updateManyAndReturn({ + where: { + email: { + contains: 'prisma.io', + } + }, + data: { + role: 'ADMIN' + } +}) +``` + + + + +```js no-copy +[{ + id: 22, + name: 'Alice', + email: 'alice@prisma.io', + profileViews: 0, + role: 'ADMIN', + coinflips: [] +}, { + id: 23, + name: 'Bob', + email: 'bob@prisma.io', + profileViews: 0, + role: 'ADMIN', + coinflips: [] +}] +``` + + + + +:::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: diff --git a/content/200-orm/500-reference/050-prisma-client-reference.mdx b/content/200-orm/500-reference/050-prisma-client-reference.mdx index 9c3755e365..f8db72a691 100644 --- a/content/200-orm/500-reference/050-prisma-client-reference.mdx +++ b/content/200-orm/500-reference/050-prisma-client-reference.mdx @@ -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`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 + + + + + +```ts +const users = await prisma.user.updateManyAndReturn({ + where: { + email: { + contains: 'prisma.io', + } + }, + data: [ + { role: 'ADMIN' } + ], +}) +``` + + + + + +```json no-copy +[ + { "id": 0, "name": "Sonali", "email": "sonali@prisma.io", "role": "ADMIN", "profileViews": 0 }, + { "id": 1, "name": "Alex", "email": "alex@prisma.io", "role": "ADMIN", "profileViews": 0 } +] +``` + + + + + ### `deleteMany()` `deleteMany` deletes multiple records in a transaction.