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..09dba52754 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/200-prisma-client/100-queries/058-transactions.mdx b/content/200-orm/200-prisma-client/100-queries/058-transactions.mdx index 1faee26a55..63b93505b5 100644 --- a/content/200-orm/200-prisma-client/100-queries/058-transactions.mdx +++ b/content/200-orm/200-prisma-client/100-queries/058-transactions.mdx @@ -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. @@ -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 diff --git a/content/200-orm/200-prisma-client/100-queries/100-query-optimization-performance.mdx b/content/200-orm/200-prisma-client/100-queries/100-query-optimization-performance.mdx index 5650052a69..70ca0cf52a 100644 --- a/content/200-orm/200-prisma-client/100-queries/100-query-optimization-performance.mdx +++ b/content/200-orm/200-prisma-client/100-queries/100-query-optimization-performance.mdx @@ -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 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 2c462acadc..ef6f103ff7 100644 --- a/content/200-orm/500-reference/050-prisma-client-reference.mdx +++ b/content/200-orm/500-reference/050-prisma-client-reference.mdx @@ -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. ::: @@ -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`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. diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 1f5e258c5d..71805d2b03 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -6,7 +6,6 @@ import type { Config } from "@docusaurus/types"; import type * as Preset from "@docusaurus/preset-classic"; const DOCUSAURUS_BASE_URL = process.env.DOCUSAURUS_BASE_URL ?? "/"; -const DOCUSAURUS_POST_HOG_KEY = process.env.DOCUSAURUS_POST_HOG_KEY ?? ""; const config: Config = { title: "Prisma Documentation", @@ -81,8 +80,9 @@ const config: Config = { "docusaurus-plugin-sass", [path.resolve(__dirname, 'client-plugins', 'posthog-docusaurus'), { - apiKey: DOCUSAURUS_POST_HOG_KEY, - appUrl: 'https://proxyhog.prisma-data.net', // this is safe to have in version control + // these are safe to have in version control + apiKey: "phc_cmc85avbWyuJ2JyKdGPdv7dxXli8xLdWDBPbvIXWJfs", + appUrl: 'https://proxyhog.prisma-data.net', person_profiles: 'identified_only', enableInDevelopment: false },