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 2f4c9022e2..9a52d3f460 100644
--- a/content/200-orm/500-reference/050-prisma-client-reference.mdx
+++ b/content/200-orm/500-reference/050-prisma-client-reference.mdx
@@ -84,7 +84,7 @@ From version 5.2.0 and upwards, you can also use the [`datasourceUrl`](#datasour
##### Programmatically override a datasource `url`
```ts
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
datasources: {
@@ -92,7 +92,7 @@ const prisma = new PrismaClient({
url: 'file:./dev_qa.db',
},
},
-})
+});
```
Based on the following `datasource` block:
@@ -117,11 +117,11 @@ Programmatically overrides the [`datasource`](#datasources) block in the `schema
#### Examples
```ts
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
datasourceUrl: 'postgresql://johndoe:randompassword@localhost:5432/mydb',
-})
+});
```
### `log`
@@ -157,12 +157,12 @@ The `query` event type:
```ts file=index.d.ts
export type QueryEvent = {
- timestamp: Date
- query: string // Query sent to the database
- params: string // Query parameters
- duration: number // Time elapsed (in milliseconds) between client issuing query and database responding - not only time taken to run query
- target: string
-}
+ timestamp: Date;
+ query: string; // Query sent to the database
+ params: string; // Query parameters
+ duration: number; // Time elapsed (in milliseconds) between client issuing query and database responding - not only time taken to run query
+ target: string;
+};
```
Note that for MongoDB, the `params` and `duration` fields will be undefined.
@@ -171,10 +171,10 @@ All other log level event types:
```ts file=index.d.ts
export type LogEvent = {
- timestamp: Date
- message: string
- target: string
-}
+ timestamp: Date;
+ message: string;
+ target: string;
+};
```
#### Examples
@@ -186,23 +186,23 @@ export type LogEvent = {
```ts highlight=3;normal;
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
-const prisma = new PrismaClient({ log: ['query', 'info'] })
+const prisma = new PrismaClient({ log: ['query', 'info'] });
async function main() {
- const countUsers = await prisma.user.count({})
+ const countUsers = await prisma.user.count({});
}
main()
.then(async () => {
- await prisma.$disconnect()
+ await prisma.$disconnect();
})
.catch(async (e) => {
- console.error(e)
- await prisma.$disconnect()
- process.exit(1)
- })
+ console.error(e);
+ await prisma.$disconnect();
+ process.exit(1);
+ });
```
@@ -225,29 +225,29 @@ prisma:query SELECT COUNT(*) FROM (SELECT "public"."User"."id" FROM "public"."Us
```ts
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
log: [{ level: 'query', emit: 'event' }],
-})
+});
prisma.$on('query', (e) => {
- console.log(e)
-})
+ console.log(e);
+});
async function main() {
- const countUsers = await prisma.user.count({})
+ const countUsers = await prisma.user.count({});
}
main()
.then(async () => {
- await prisma.$disconnect()
+ await prisma.$disconnect();
})
.catch(async (e) => {
- console.error(e)
- await prisma.$disconnect()
- process.exit(1)
- })
+ console.error(e);
+ await prisma.$disconnect();
+ process.exit(1);
+ });
```
@@ -274,7 +274,7 @@ main()
```ts
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({
log: [
@@ -282,33 +282,33 @@ const prisma = new PrismaClient({
{ level: 'info', emit: 'event' },
{ level: 'error', emit: 'event' },
],
-})
+});
prisma.$on('warn', (e) => {
- console.log(e)
-})
+ console.log(e);
+});
prisma.$on('info', (e) => {
- console.log(e)
-})
+ console.log(e);
+});
prisma.$on('error', (e) => {
- console.log(e)
-})
+ console.log(e);
+});
async function main() {
- const countUsers = await prisma.user.count({})
+ const countUsers = await prisma.user.count({});
}
main()
.then(async () => {
- await prisma.$disconnect()
+ await prisma.$disconnect();
})
.catch(async (e) => {
- console.error(e)
- await prisma.$disconnect()
- process.exit(1)
- })
+ console.error(e);
+ await prisma.$disconnect();
+ process.exit(1);
+ });
```
@@ -350,7 +350,7 @@ Determines the level and formatting of errors returned by Prisma Client.
```ts
const prisma = new PrismaClient({
// Defaults to colorless
-})
+});
```
##### `pretty` error formatting
@@ -358,7 +358,7 @@ const prisma = new PrismaClient({
```ts
const prisma = new PrismaClient({
errorFormat: 'pretty',
-})
+});
```
##### `colorless` error formatting
@@ -366,7 +366,7 @@ const prisma = new PrismaClient({
```ts
const prisma = new PrismaClient({
errorFormat: 'colorless',
-})
+});
```
##### `minimal` error formatting
@@ -374,7 +374,7 @@ const prisma = new PrismaClient({
```ts
const prisma = new PrismaClient({
errorFormat: 'minimal',
-})
+});
```
### `adapter`
@@ -392,19 +392,19 @@ This is available from version 5.4.0 and newer behind the `driverAdapters` featu
The example below uses the [Neon driver adapter](/orm/overview/databases/neon#how-to-use-neons-serverless-driver-with-prisma-orm-preview)
```ts
-import { Pool, neonConfig } from '@neondatabase/serverless'
-import { PrismaNeon } from '@prisma/adapter-neon'
-import { PrismaClient } from '@prisma/client'
-import dotenv from 'dotenv'
-import ws from 'ws'
+import { Pool, neonConfig } from '@neondatabase/serverless';
+import { PrismaNeon } from '@prisma/adapter-neon';
+import { PrismaClient } from '@prisma/client';
+import dotenv from 'dotenv';
+import ws from 'ws';
-dotenv.config()
-neonConfig.webSocketConstructor = ws
-const connectionString = `${process.env.DATABASE_URL}`
+dotenv.config();
+neonConfig.webSocketConstructor = ws;
+const connectionString = `${process.env.DATABASE_URL}`;
-const pool = new Pool({ connectionString })
-const adapter = new PrismaNeon(pool)
-const prisma = new PrismaClient({ adapter })
+const pool = new Pool({ connectionString });
+const adapter = new PrismaNeon(pool);
+const prisma = new PrismaClient({ adapter });
```
### `rejectOnNotFound`
@@ -437,7 +437,7 @@ Use the `rejectOnNotFound` parameter to configure `findUnique()` and/or `findFir
```ts
const prisma = new PrismaClient({
rejectOnNotFound: true,
-})
+});
```
##### Enable globally for a specific operation
@@ -447,7 +447,7 @@ const prisma = new PrismaClient({
rejectOnNotFound: {
findUnique: true,
},
-})
+});
```
##### Throw a custom error per model and operation if record is not found
@@ -464,7 +464,7 @@ const prisma = new PrismaClient({
Post: (err) => new Error('Post error!'),
},
},
-})
+});
```
### `transactionOptions`
@@ -498,7 +498,7 @@ const prisma = new PrismaClient({
maxWait: 5000, // default: 2000
timeout: 10000, // default: 5000
},
-})
+});
```
## Model queries
@@ -524,7 +524,7 @@ Use model queries to perform CRUD operations on your models. See also: [CRUD](/o
| Name | Example type (`User`) | Required | Description |
| ------------------------------- | ------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all _unique_ fields of a model so that individual records can be selected.
From version 4.5.0, this type wraps all fields of a model. [Learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput) |
+| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all fields of a model so that a record can be selected ([learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput)).
Before version 4.5.0, this type only wraps _unique_ fields of a model. |
| `select` | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
| `include` | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
| `omit` | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
@@ -549,7 +549,7 @@ const result = await prisma.user.findUnique({
where: {
id: 42,
},
-})
+});
```
##### Get the `User` record with an `email` of `alice@prisma.io`
@@ -559,7 +559,7 @@ const result = await prisma.user.findUnique({
where: {
email: 'alice@prisma.io',
},
-})
+});
```
##### Get the `User` record with `firstName` of `Alice` and `lastName` of `Smith` (`@@unique`)
@@ -588,7 +588,7 @@ const result = await prisma.user.findUnique({
lastName: 'Smith',
},
},
-})
+});
```
##### Get the `User` record with `firstName` of `Alice` and `lastName` of `Smith` (`@@id`)
@@ -616,7 +616,7 @@ const result = await prisma.user.findUnique({
lastName: 'Smith',
},
},
-})
+});
```
### `findUniqueOrThrow()`
@@ -652,7 +652,7 @@ We introduced `findUniqueOrThrow` in v4.0.0. It replaces the [`rejectOnNotFound`
#### Options
| Name | Example type (`User`) | Required | Description |
-| ------------------------------- | ------------------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| ------------------------------- | ------------------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --- |
| `select` | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
| `include` | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
| `omit` | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0. |
@@ -688,28 +688,28 @@ See [Filter conditions and operators](#filter-conditions-and-operators) for exam
```ts
const user = await prisma.user.findFirst({
where: { name: 'Alice' },
-})
+});
```
##### Get the first `Post` record where the `title` starts with `A test`, reverse the list with `take`
```ts
-import { PrismaClient } from '@prisma/client'
+import { PrismaClient } from '@prisma/client';
-const prisma = new PrismaClient({})
+const prisma = new PrismaClient({});
async function main() {
const a = await prisma.post.create({
data: {
title: 'A test 1',
},
- })
+ });
const b = await prisma.post.create({
data: {
title: 'A test 2',
},
- })
+ });
const c = await prisma.post.findFirst({
where: {
@@ -721,10 +721,10 @@ async function main() {
title: 'asc',
},
take: -1, // Reverse the list
- })
+ });
}
-main()
+main();
```
### `findFirstOrThrow()`
@@ -785,7 +785,7 @@ See [Filter conditions and operators](#filter-conditions-and-operators) for exam
```ts
const user = await prisma.user.findMany({
where: { name: 'Alice' },
-})
+});
```
### `create()`
@@ -794,13 +794,13 @@ const user = await prisma.user.findMany({
#### Options
-| Name | Type | Required | Description |
-| ----------------------------- | -------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `data` | `XOR`UserUncheckedCreateInput>` | **Yes** | Wraps all the model fields in a type so that they can be provided when creating new records. It also includes relation fields which lets you perform (transactional) nested inserts. Fields that are marked as optional or have default values in the datamodel are optional. |
-| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
-| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
-| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
-| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
+| Name | Type | Required | Description |
+| ----------------------- | -------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `data` | `XOR`UserUncheckedCreateInput>` | **Yes** | Wraps all the model fields in a type so that they can be provided when creating new records. It also includes relation fields which lets you perform (transactional) nested inserts. Fields that are marked as optional or have default values in the datamodel are optional. |
+| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
+| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
+| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
+| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
#### Return type
@@ -820,7 +820,7 @@ const user = await prisma.user.findMany({
```ts
const user = await prisma.user.create({
data: { email: 'alice@prisma.io' },
-})
+});
```
##### Create multiple new records
@@ -834,9 +834,9 @@ The following example results in **two** `INSERT` statements:
```ts
-import { Prisma, PrismaClient } from '@prisma/client'
+import { Prisma, PrismaClient } from '@prisma/client';
-const prisma = new PrismaClient({ log: ['query'] })
+const prisma = new PrismaClient({ log: ['query'] });
async function main() {
let users: Prisma.UserCreateInput[] = [
@@ -854,26 +854,26 @@ async function main() {
coinflips: [true, false, false],
role: 'ADMIN',
},
- ]
+ ];
await Promise.all(
users.map(async (user) => {
await prisma.user.create({
data: user,
- })
+ });
})
- )
+ );
}
main()
.then(async () => {
- await prisma.$disconnect()
+ await prisma.$disconnect();
})
.catch(async (e) => {
- console.error(e)
- await prisma.$disconnect()
- process.exit(1)
- })
+ console.error(e);
+ await prisma.$disconnect();
+ process.exit(1);
+ });
```
@@ -900,14 +900,14 @@ prisma:query COMMIT
#### Options
-| Name | Type | Required | Description |
-| ----------------------------- | ------------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `data` | `XOR`UserUncheckedUpdateInput>` | **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. |
-| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all _unique_ fields of a model so that individual records can be selected.
From version 4.5.0, this type wraps all fields of a model. [Learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput) |
-| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
-| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
-| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0. |
-| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
+| Name | Type | Required | Description |
+| ----------------------- | ------------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `data` | `XOR`UserUncheckedUpdateInput>` | **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. |
+| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all fields of a model so that a record can be selected ([learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput)).
Before version 4.5.0, this type only wraps _unique_ fields of a model. |
+| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
+| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
+| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0. |
+| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
#### Return type
@@ -930,7 +930,7 @@ prisma:query COMMIT
const user = await prisma.user.update({
where: { id: 1 },
data: { email: 'alice@prisma.io' },
-})
+});
```
### `upsert()`
@@ -948,15 +948,15 @@ This section covers the usage of the `upsert()` operation. To learn about using
#### Options
-| Name | Type | Required | Description |
-| ------------------------------ | ------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `create` | `XOR`UserUncheckedCreateInput>` | **Yes** | Wraps all the fields of the model so that they can be provided when creating new records. It also includes relation fields which lets you perform (transactional) nested inserts. Fields that are marked as optional or have default values in the datamodel are optional. |
-| `update` | `XOR`UserUncheckedUpdateInput>` | **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. |
-| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all _unique_ fields of a model so that individual records can be selected.
From version 4.5.0, this type wraps all fields of a model. [Learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput) |
-| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
-| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
-| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
-| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
+| Name | Type | Required | Description |
+| ----------------------- | ------------------------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `create` | `XOR`UserUncheckedCreateInput>` | **Yes** | Wraps all the fields of the model so that they can be provided when creating new records. It also includes relation fields which lets you perform (transactional) nested inserts. Fields that are marked as optional or have default values in the datamodel are optional. |
+| `update` | `XOR`UserUncheckedUpdateInput>` | **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. |
+| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all fields of a model so that a record can be selected ([learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput)).
Before version 4.5.0, this type only wraps _unique_ fields of a model. |
+| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
+| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
+| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
+| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
#### Return type
@@ -980,7 +980,7 @@ const user = await prisma.user.upsert({
where: { id: 1 },
update: { email: 'alice@prisma.io' },
create: { email: 'alice@prisma.io' },
-})
+});
```
#### Unique key constraint errors on upserts
@@ -1065,7 +1065,7 @@ prisma.user.upsert({
update: {
email: 'updated@example.com',
},
-})
+});
```
In this situation, Prisma uses the following SQL query:
@@ -1094,7 +1094,7 @@ prisma.User.upsert({
update: {
email: 'updated@example.com',
},
-})
+});
```
In the following query, the values for `userName` in the `where` and `create` options are different, so Prisma Client does _not_ use a database upsert.
@@ -1113,7 +1113,7 @@ prisma.User.upsert({
update: {
email: 'updated@example.com',
},
-})
+});
```
In the following query, the selection on the `title` field in `posts` is a nested read, so Prisma Client does _not_ use a database upsert.
@@ -1142,7 +1142,7 @@ prisma.user.upsert({
update: {
email: 'updated@example.com',
},
-})
+});
```
### `delete()`
@@ -1156,13 +1156,13 @@ To delete records that match a certain criteria, use [`deleteMany`](#deletemany)
#### Options
-| Name | Type | Required | Description |
-| ------------------------------ | ------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all _unique_ fields of a model so that individual records can be selected.
From version 4.5.0, this type wraps all fields of a model. [Learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput) |
-| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
-| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
-| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
-| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
+| Name | Type | Required | Description |
+| ----------------------- | ------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `where` | `UserWhereUniqueInput` | **Yes** | Wraps all fields of a model so that a record can be selected ([learn more](#filter-on-non-unique-fields-with-userwhereuniqueinput)).
Before version 4.5.0, this type only wraps _unique_ fields of a model. |
+| [`select`](#select) | `XOR` | No | [Specifies which properties to include](/orm/prisma-client/queries/select-fields) on the returned object. |
+| [`include`](#include) | `XOR` | No | [Specifies which relations should be eagerly loaded](/orm/prisma-client/queries/relation-queries) on the returned object. |
+| [`omit`](#omit-preview) | `XOR` | No | Specifies which properties to exclude on the returned object. In [Preview](/orm/more/releases#preview) since 5.13.0 |
+| `relationLoadStrategy` | `'join'` or `'query'` | No | **Default: `join`**. Specifies the [load strategy](/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview) for a relation query. Only available in combination with `include` (or `select` on a relation field). In [Preview](/orm/more/releases#preview) since 5.9.0. |
#### Return type
@@ -1183,7 +1183,7 @@ To delete records that match a certain criteria, use [`deleteMany`](#deletemany)
```ts
const user = await prisma.user.delete({
where: { id: 1 },
-})
+});
```
##### Delete the `User` record where `email` equals `else@prisma.io`
@@ -1203,7 +1203,7 @@ const deleteUser = await prisma.user.delete({
email: true,
name: true,
},
-})
+});
```
@@ -1252,7 +1252,7 @@ const users = await prisma.user.createMany({
{ name: 'Sonali', email: 'sonali@prisma.io' },
{ name: 'Alex', email: 'alex@prisma.io' },
],
-})
+});
```
### `createManyAndReturn()`
@@ -1341,8 +1341,8 @@ const users = await prisma.user.createManyAndReturn({
```ts
export type BatchPayload = {
- count: number
-}
+ count: number;
+};
```
#### Examples
@@ -1353,7 +1353,7 @@ export type BatchPayload = {
const updatedUserCount = await prisma.user.updateMany({
where: { name: 'Alice' },
data: { name: 'ALICE' },
-})
+});
```
##### Update all `User` records where the `email` contains `prisma.io` and at least one related `Post` has more than 10 likes
@@ -1375,7 +1375,7 @@ const updatedUserCount = await prisma.user.updateMany({
data: {
role: 'USER',
},
-})
+});
```
### `deleteMany()`
@@ -1396,8 +1396,8 @@ const updatedUserCount = await prisma.user.updateMany({
```ts
export type BatchPayload = {
- count: number
-}
+ count: number;
+};
```
#### Examples
@@ -1405,7 +1405,7 @@ export type BatchPayload = {
##### Delete all `User` records
```ts
-const deletedUserCount = await prisma.user.deleteMany({})
+const deletedUserCount = await prisma.user.deleteMany({});
```
##### Delete all `User` records where the `name` is `Alice`
@@ -1413,7 +1413,7 @@ const deletedUserCount = await prisma.user.deleteMany({})
```ts
const deletedUserCount = await prisma.user.deleteMany({
where: { name: 'Alice' },
-})
+});
```
See [Filter conditions and operators](#filter-conditions-and-operators) for examples of how to filter the records to delete.
@@ -1442,7 +1442,7 @@ See [Filter conditions and operators](#filter-conditions-and-operators) for exam
##### Count all `User` records
```ts
-const result = await prisma.user.count()
+const result = await prisma.user.count();
```
##### Count all `User` records with at least one published `Post`
@@ -1456,7 +1456,7 @@ const result = await prisma.user.count({
},
},
},
-})
+});
```
##### Use `select` to perform three separate counts
@@ -1474,7 +1474,7 @@ const c = await prisma.user.count({
city: true,
name: true,
},
-})
+});
```
### `aggregate()`
@@ -1514,7 +1514,7 @@ const minMaxAge = await prisma.user.aggregate({
_min: {
profileViews: true,
},
-})
+});
```
@@ -1541,7 +1541,7 @@ const setValue = await prisma.user.aggregate({
_sum: {
profileViews: true,
},
-})
+});
```
@@ -1607,14 +1607,14 @@ const groupUsers = await prisma.user.groupBy({
},
},
},
-})
+});
```
```js no-copy
-;[
+[
{
country: 'Denmark',
city: 'Copenhagen',
@@ -1633,7 +1633,7 @@ const groupUsers = await prisma.user.groupBy({
city: 3,
},
},
-]
+];
```
@@ -1672,7 +1672,7 @@ const result = await prisma.user.findUnique({
name: true,
profileViews: true,
},
-})
+});
```
@@ -1699,14 +1699,14 @@ const result = await prisma.user.findMany({
email: true,
role: true,
},
-})
+});
```
```js no-copy
-;[
+[
{
email: 'alice@prisma.io',
role: 'ADMIN',
@@ -1715,7 +1715,7 @@ const result = await prisma.user.findMany({
email: 'bob@prisma.io',
role: 'USER',
},
-]
+];
```
@@ -1733,7 +1733,7 @@ const usersWithCount = await prisma.user.findMany({
select: { posts: true },
},
},
-})
+});
```
@@ -1742,7 +1742,7 @@ const usersWithCount = await prisma.user.findMany({
```js no-copy
{
_count: {
- posts: 3
+ posts: 3;
}
}
```
@@ -1767,14 +1767,14 @@ const result = await prisma.user.findMany({
},
},
},
-})
+});
```
```ts no-copy
-;[
+[
{
id: 1,
name: 'Alice',
@@ -1788,7 +1788,7 @@ const result = await prisma.user.findMany({
name: 'Bob',
posts: [],
},
-]
+];
```
@@ -1810,14 +1810,14 @@ const result = await prisma.user.findMany({
},
},
},
-})
+});
```
```js no-copy
-;[
+[
{
id: 1,
name: 'Alice',
@@ -1850,7 +1850,7 @@ const result = await prisma.user.findMany({
},
],
},
-]
+];
```
@@ -1865,7 +1865,7 @@ const selectNameEmailNotPosts = Prisma.validator()({
name: true,
email: true,
posts: false,
-})
+});
```
### `include`
@@ -1886,7 +1886,7 @@ const users = await prisma.user.findMany({
posts: true, // Returns all fields for all posts
profile: true, // Returns all Profile fields
},
-})
+});
```
##### Include the `posts` relation on the returned objects when creating a new `User` record with two `Post` records
@@ -1896,14 +1896,11 @@ const user = await prisma.user.create({
data: {
email: 'alice@prisma.io',
posts: {
- create: [
- { title: 'This is my first post' },
- { title: 'Here comes a second post' },
- ],
+ create: [{ title: 'This is my first post' }, { title: 'Here comes a second post' }],
},
},
include: { posts: true }, // Returns all fields for all posts
-})
+});
```
#### Generated types for `include`
@@ -1913,7 +1910,7 @@ The following example demonstrates how to use the [`validator`](/orm/prisma-clie
```ts
const includePosts = Prisma.validator()({
posts: true,
-})
+});
```
##### Include a `_count` of relations
@@ -1928,7 +1925,7 @@ const usersWithCount = await prisma.user.findMany({
select: { posts: true },
},
},
-})
+});
```
@@ -1973,7 +1970,7 @@ const result = await prisma.user.findMany({
omit: {
password: true,
},
-})
+});
```
@@ -1991,7 +1988,7 @@ const result = await prisma.user.findMany({
email: 'rose@prisma.io',
name: 'Rose',
},
-]
+];
```
@@ -2014,7 +2011,7 @@ const results = await prisma.user.findMany({
},
},
},
-})
+});
```
@@ -2054,7 +2051,7 @@ const results = await prisma.user.findMany({
},
],
},
-]
+];
```
@@ -2067,7 +2064,7 @@ The following example demonstrates how to use the [`validator`](/orm/prisma-clie
```ts
const omitPassword = Prisma.validator()({
password: true,
-})
+});
```
### `relationLoadStrategy` (Preview)
@@ -2107,7 +2104,7 @@ const users = await prisma.user.findMany({
include: {
posts: true,
},
-})
+});
```
##### Load the `posts` relation via a database-level JOIN when using `select`
@@ -2118,7 +2115,7 @@ const users = await prisma.user.findMany({
select: {
posts: true,
},
-})
+});
```
### `where`
@@ -2134,7 +2131,7 @@ const results = await prisma.user.findMany({
endsWith: 'prisma.io',
},
},
-})
+});
```
#### Generated types for `where`
@@ -2147,7 +2144,7 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
// UserWhereInput
const whereNameIs = Prisma.validator()({
name: 'Rich',
- })
+ });
// It can be combined with conditional operators too
const whereNameIs = Prisma.validator()({
@@ -2159,7 +2156,7 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
},
},
],
- })
+ });
```
- `UserWhereUniqueInput` This type works by exposing any unique fields on the model. A field assigned `@id` is considered unique,
@@ -2179,7 +2176,7 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
```ts
const whereScalarTitleIs = Prisma.validator()({
title: 'boop',
- })
+ });
```
- `PostUpdateWithWhereUniqueWithoutAuthorInput` - This type accepts a unique `where` field (an `@id` or another assigned `@unique`)
@@ -2196,7 +2193,7 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
published: true,
title: 'This is a new title',
},
- })
+ });
```
- `PostUpsertWithWhereUniqueWithoutAuthorInput` - This type will update the `Post` records title field where the id matches, if it doesn't exist it will create it instead.
@@ -2214,23 +2211,22 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
id: 1,
title: 'If the title doesnt exist, then create one with this text',
},
- })
+ });
```
- `PostUpdateManyWithWhereWithoutAuthorInput` - This type will update all `Post` records where published is set to false.
```ts
- const publishAllPosts =
- Prisma.validator()({
- where: {
- published: {
- equals: false,
- },
- },
- data: {
- published: true,
+ const publishAllPosts = Prisma.validator()({
+ where: {
+ published: {
+ equals: false,
},
- })
+ },
+ data: {
+ published: true,
+ },
+ });
```
### `orderBy`
@@ -2276,7 +2272,7 @@ const users = await prisma.user.findMany({
orderBy: {
email: 'asc',
},
-})
+});
```
The following example returns all `User` records sorted by `email` descending:
@@ -2286,7 +2282,7 @@ const users = await prisma.user.findMany({
orderBy: {
email: 'desc',
},
-})
+});
```
#### Sort `Post` by the related `User` record's `name`
@@ -2300,7 +2296,7 @@ const posts = await prisma.post.findMany({
name: 'asc',
},
},
-})
+});
```
#### Sort `Post` by the related `User` record's `name`, with `null` records first
@@ -2314,7 +2310,7 @@ const posts = await prisma.post.findMany({
name: { sort: 'asc', nulls: 'first' },
},
},
-})
+});
```
#### Sort `Post` by relevance of the title
@@ -2349,7 +2345,7 @@ const getActiveusers = await prisma.user.findMany({
count: 'desc',
},
},
-})
+});
```
##### Sort `User` by multiple fields - `email` _and_ `role`
@@ -2374,7 +2370,7 @@ const users = await prisma.user.findMany({
role: 'desc',
},
],
-})
+});
```
@@ -2442,7 +2438,7 @@ const users = await prisma.user.findMany({
email: 'desc',
},
],
-})
+});
```
@@ -2507,7 +2503,7 @@ const users3 = await prisma.user.findMany({
name: true,
email: true,
},
-})
+});
```
@@ -2515,7 +2511,7 @@ const users3 = await prisma.user.findMany({
```js no-copy
-;[
+[
{
name: 'Alice',
email: 'alice@prisma.io',
@@ -2528,7 +2524,7 @@ const users3 = await prisma.user.findMany({
name: 'Bob',
email: 'bob@prisma.io',
},
-]
+];
```
@@ -2561,7 +2557,7 @@ const usersWithPosts = await prisma.user.findMany({
},
},
},
-})
+});
```
@@ -2637,7 +2633,7 @@ const userWithPosts = await prisma.user.findUnique({
},
},
},
-})
+});
```
@@ -2689,7 +2685,7 @@ const sort = await prisma.user.findMany({
email: true,
role: true,
},
-})
+});
```
@@ -2730,7 +2726,7 @@ The following examples demonstrate how to use the [`validator`](/orm/prisma-clie
```ts
const orderEmailsByDescending = Prisma.validator()({
email: 'desc',
- })
+ });
```
### `distinct`
@@ -2754,7 +2750,7 @@ const distinctCities = await prisma.user.findMany({
country: true,
},
distinct: ['city'],
-})
+});
```
@@ -2762,10 +2758,10 @@ const distinctCities = await prisma.user.findMany({
```js no-lines no-copy
-;[
+[
{ city: 'Paris', country: 'France' },
{ city: 'Lyon', country: 'France' },
-]
+];
```
@@ -2787,7 +2783,7 @@ const distinctCitiesAndCountries = await prisma.user.findMany({
country: true,
},
distinct: ['city', 'country'],
-})
+});
```
@@ -2795,11 +2791,11 @@ const distinctCitiesAndCountries = await prisma.user.findMany({
```js no-lines no-copy
-;[
+[
{ city: 'Paris', country: 'France' },
{ city: 'Paris', country: 'Denmark' },
{ city: 'Lyon', country: 'France' },
-]
+];
```
@@ -2828,7 +2824,7 @@ const distinctCitiesAndCountries = await prisma.user.findMany({
country: true,
},
distinct: ['city', 'country'],
-})
+});
```
@@ -2836,10 +2832,10 @@ const distinctCitiesAndCountries = await prisma.user.findMany({
```js no-copy
-;[
+[
{ city: 'Paris', country: 'Denmark' },
{ city: 'Lyon', country: 'France' },
-]
+];
```
@@ -2869,7 +2865,7 @@ const user = await prisma.user.create({
create: { bio: 'Hello World' },
},
},
-})
+});
```
##### Create a new `Profile` record with a new `User` record
@@ -2896,7 +2892,7 @@ const user = await prisma.user.create({
create: { title: 'Hello World' },
},
},
-})
+});
```
##### Create a new `User` record with two new `Post` records
@@ -2918,7 +2914,7 @@ const user = await prisma.user.create({
],
},
},
-})
+});
```
Note: You can also use a nested [`createMany`](#createmany-1) to achieve the same result.
@@ -2933,7 +2929,7 @@ const user = await prisma.user.update({
create: { bio: 'Hello World' },
},
},
-})
+});
```
##### Update an existing `User` record by creating a new `Post` record
@@ -2988,7 +2984,7 @@ const user = await prisma.user.update({
},
},
},
-})
+});
```
### `set`
@@ -3007,7 +3003,7 @@ const user = await prisma.user.update({
set: [{ id: 32 }, { id: 42 }],
},
},
-})
+});
```
### `connect`
@@ -3035,7 +3031,7 @@ const user = await prisma.profile.create({
connect: { email: 'alice@prisma.io' },
},
},
-})
+});
```
##### Create a new `Profile` record and connect it to an existing `User` record via an ID field
@@ -3048,7 +3044,7 @@ const user = await prisma.profile.create({
connect: { id: 42 }, // sets userId of Profile record
},
},
-})
+});
```
In [2.11.0](https://github.com/prisma/prisma/releases/2.11.0) and later, you can set the foreign key directly:
@@ -3059,7 +3055,7 @@ const user = await prisma.profile.create({
bio: 'Hello World',
userId: 42,
},
-})
+});
```
However, you can't use both the direct approach and the `connect` approach in the same query. See [this issue comment](https://github.com/prisma/prisma/issues/4322#issuecomment-737976117) for details.
@@ -3074,7 +3070,7 @@ const user = await prisma.post.create({
connect: { email: 'alice@prisma.io' },
},
},
-})
+});
```
##### Update an existing `User` record by connecting it to an existing `Profile` record
@@ -3087,7 +3083,7 @@ const user = await prisma.user.update({
connect: { id: 24 },
},
},
-})
+});
```
##### Update an existing `User` record by connecting it to two existing `Post` records
@@ -3100,7 +3096,7 @@ const user = await prisma.user.update({
connect: [{ id: 24 }, { id: 42 }],
},
},
-})
+});
```
### `connectOrCreate`
@@ -3221,7 +3217,7 @@ const user = await prisma.post.create({
},
},
},
-})
+});
```
##### Update an existing `User` record by connecting it to an existing `Profile` record, _or_ creating a new `Profile` record
@@ -3244,7 +3240,7 @@ const updateUser = await prisma.user.update({
},
},
},
-})
+});
```
##### Update an existing `User` record by connect it to two existing `Post` records, or creating two new `Post` records
@@ -3266,7 +3262,7 @@ const user = await prisma.user.update({
],
},
},
-})
+});
```
### `disconnect`
@@ -3297,7 +3293,7 @@ const user = await prisma.user.update({
disconnect: true,
},
},
-})
+});
```
##### Update an existing `User` record by disconnecting two `Post` records it's connected to
@@ -3310,7 +3306,7 @@ const user = await prisma.user.update({
disconnect: [{ id: 44 }, { id: 46 }],
},
},
-})
+});
```
### `update`
@@ -3344,7 +3340,7 @@ const user = await prisma.user.update({
update: { bio: 'Hello World' },
},
},
-})
+});
```
##### Update an existing `User` record by updating two `Post` records it's connected to
@@ -3366,7 +3362,7 @@ const user = await prisma.user.update({
],
},
},
-})
+});
```
### `upsert`
@@ -3394,7 +3390,7 @@ const user = await prisma.user.update({
},
},
},
-})
+});
```
##### Update an existing `User` record by updating two `Post` record it's connected to or creating new ones (_upsert_)
@@ -3418,7 +3414,7 @@ const user = await prisma.user.update({
],
},
},
-})
+});
```
### `delete`
@@ -3441,7 +3437,7 @@ const user = await prisma.user.update({
delete: true,
},
},
-})
+});
```
##### Update an existing `User` record by deleting two `Post` records it's connected to
@@ -3454,7 +3450,7 @@ const user = await prisma.user.update({
delete: [{ id: 34 }, { id: 36 }],
},
},
-})
+});
```
### `updateMany`
@@ -3482,7 +3478,7 @@ const result = await prisma.user.update({
},
},
},
-})
+});
```
### `deleteMany`
@@ -3504,7 +3500,7 @@ const result = await prisma.user.update({
deleteMany: {},
},
},
-})
+});
```
## Filter conditions and operators
@@ -3531,7 +3527,7 @@ const result = await prisma.user.findMany({
equals: 'Eleanor',
},
},
-})
+});
```
You can also exclude the `equals`:
@@ -3541,7 +3537,7 @@ const result = await prisma.user.findMany({
where: {
name: 'Eleanor',
},
-})
+});
```
##### Return all products with a quantity lower than the "warn quantity" threshold
@@ -3573,7 +3569,7 @@ const result = await prisma.user.findMany({
not: 'Eleanor',
},
},
-})
+});
```
:::warning
@@ -3614,7 +3610,7 @@ const getUser = await prisma.user.findMany({
where: {
id: { in: [22, 91, 14, 2, 5] },
},
-})
+});
```
##### Get `User` records where the `name` can be found in the following list: `['Saqui', 'Clementine', 'Bob']`
@@ -3624,7 +3620,7 @@ const getUser = await prisma.user.findMany({
where: {
name: { in: ['Saqui', 'Clementine', 'Bob'] },
},
-})
+});
```
##### Get `User` records where `name` is **not** present in the list
@@ -3638,7 +3634,7 @@ const getUser = await prisma.user.findMany({
name: { in: ['Saqui', 'Clementine', 'Bob'] },
},
},
-})
+});
```
##### Get a `User` record where at least one `Post` has at least one specified `Category`
@@ -3661,7 +3657,7 @@ const getUser = await prisma.user.findMany({
},
},
},
-})
+});
```
### `notIn`
@@ -3681,7 +3677,7 @@ const getUser = await prisma.user.findMany({
where: {
id: { notIn: [22, 91, 14, 2, 5] },
},
-})
+});
```
### `lt`
@@ -3699,7 +3695,7 @@ const getPosts = await prisma.post.findMany({
lt: 9,
},
},
-})
+});
```
### `lte`
@@ -3717,7 +3713,7 @@ const getPosts = await prisma.post.findMany({
lte: 9,
},
},
-})
+});
```
### `gt`
@@ -3735,7 +3731,7 @@ const getPosts = await prisma.post.findMany({
gt: 9,
},
},
-})
+});
```
### `gte`
@@ -3753,7 +3749,7 @@ const getPosts = await prisma.post.findMany({
gte: 9,
},
},
-})
+});
```
#### Examples
@@ -3764,12 +3760,10 @@ const getPosts = await prisma.post.findMany({
const result = await prisma.post.findMany({
where: {
date_created: {
- gte: new Date(
- '2020-03-19T14:21:00+0200'
- ) /* Includes time offset for UTC */,
+ gte: new Date('2020-03-19T14:21:00+0200') /* Includes time offset for UTC */,
},
},
-})
+});
```
### `contains`
@@ -3787,7 +3781,7 @@ const result = await prisma.post.count({
contains: 'databases',
},
},
-})
+});
```
##### Count all `Post` records where `content` **does not** contain `databases`
@@ -3801,7 +3795,7 @@ const result = await prisma.post.count({
},
},
},
-})
+});
```
### `search`
@@ -3829,7 +3823,7 @@ const result = await prisma.post.findMany({
search: 'cat | dog',
},
},
-})
+});
```
##### Find all posts with a title that contains `cat` and `dog`.
@@ -3841,7 +3835,7 @@ const result = await prisma.post.findMany({
search: 'cat & dog',
},
},
-})
+});
```
##### Find all posts with a title that doesn't contain `cat`.
@@ -3853,7 +3847,7 @@ const result = await prisma.post.findMany({
search: '!cat',
},
},
-})
+});
```
### `mode`
@@ -3874,7 +3868,7 @@ const result = await prisma.post.findMany({
mode: 'insensitive',
},
},
-})
+});
```
### `startsWith`
@@ -3890,7 +3884,7 @@ const result = await prisma.post.findMany({
startsWith: 'Pr',
},
},
-})
+});
```
### `endsWith`
@@ -3904,7 +3898,7 @@ const result = await prisma.user.findMany({
endsWith: 'prisma.io',
},
},
-})
+});
```
### `AND`
@@ -3931,7 +3925,7 @@ const result = await prisma.post.findMany({
},
],
},
-})
+});
```
##### Get all `Post` records where the `content` field contains `Prisma` and `published` is `false` (no `AND`)
@@ -3948,7 +3942,7 @@ const result = await prisma.post.findMany({
equals: false,
},
},
-})
+});
```
##### Get all `Post` records where the `title` field contains `Prisma` or `databases`, and `published` is `false`
@@ -3974,7 +3968,7 @@ const result = await prisma.post.findMany({
published: false,
},
},
-})
+});
```
### `OR`
@@ -4001,7 +3995,7 @@ const result = await prisma.post.findMany({
},
],
},
-})
+});
```
##### Get all `Post` records where the `title` field contains `Prisma` or `databases`, but not `SQL`
@@ -4029,7 +4023,7 @@ const result = await prisma.post.findMany({
},
},
},
-})
+});
```
##### Get all `Post` records where the `title` field contains `Prisma` or `databases`, and `published` is `false`
@@ -4055,7 +4049,7 @@ const result = await prisma.post.findMany({
published: false,
},
},
-})
+});
```
### `NOT`
@@ -4087,7 +4081,7 @@ const result = await prisma.post.findMany({
},
},
},
-})
+});
```
##### Get all `Post` records where the `title` field contains `Prisma` or `databases`, but not `SQL`, and the related `User` record' email address does not contain `sarah`
@@ -4123,7 +4117,7 @@ const result = await prisma.post.findMany({
include: {
user: true,
},
-})
+});
```
## Relation filters
@@ -4261,7 +4255,7 @@ Use `set` to overwrite the value of a scalar list field.
- `set` is optional - you can set the value directly:
```ts
- tags: ['computers', 'books']
+ tags: ['computers', 'books'];
```
#### Examples
@@ -4278,7 +4272,7 @@ const setTags = await prisma.post.update({
set: ['computing', 'books'],
},
},
-})
+});
```
##### Set `tags` to a list of values _without_ using the `set` keyword
@@ -4291,7 +4285,7 @@ const setTags = await prisma.post.update({
data: {
tags: ['computing', 'books'],
},
-})
+});
```
#### Set the value of `tags` to a single string value
@@ -4306,7 +4300,7 @@ const setTags = await prisma.post.update({
set: 'computing',
},
},
-})
+});
```
### `push`
@@ -4332,7 +4326,7 @@ const addTag = await prisma.post.update({
push: 'computing',
},
},
-})
+});
```
```ts
@@ -4345,7 +4339,7 @@ const addTag = await prisma.post.update({
push: ['computing', 'genetics'],
},
},
-})
+});
```
### `unset`
@@ -4373,7 +4367,7 @@ const setTags = await prisma.post.update({
unset: true,
},
},
-})
+});
```
## Scalar list filters
@@ -4409,7 +4403,7 @@ const posts = await client.post.findMany({
has: 'databases',
},
},
-})
+});
```
The following query returns all `Post` records where the `tags` list **does not** include `"databases"`:
@@ -4423,7 +4417,7 @@ const posts = await client.post.findMany({
},
},
},
-})
+});
```
### `hasEvery`
@@ -4441,7 +4435,7 @@ const posts = await prisma.post.findMany({
hasEvery: ['databases', 'typescript'],
},
},
-})
+});
```
### `hasSome`
@@ -4459,7 +4453,7 @@ const posts = await prisma.post.findMany({
hasSome: ['databases', 'typescript'],
},
},
-})
+});
```
### `isEmpty`
@@ -4477,7 +4471,7 @@ const posts = await prisma.post.findMany({
isEmpty: true,
},
},
-})
+});
```
### `isSet`
@@ -4502,7 +4496,7 @@ const posts = await prisma.post.findMany({
isSet: true,
},
},
-})
+});
```
### `equals`
@@ -4520,7 +4514,7 @@ const posts = await prisma.post.findMany({
equals: ['databases', 'typescript'],
},
},
-})
+});
```
## Composite type methods
@@ -4544,7 +4538,7 @@ Use `set` to overwrite the value of a composite type.
photos: [
{ height: 100, width: 200, url: '1.jpg' },
{ height: 100, width: 200, url: '2.jpg' },
- ]
+ ];
```
#### Examples
@@ -4567,7 +4561,7 @@ const order = await prisma.order.create({
},
},
},
-})
+});
```
##### Set an optional composite type to `null`
@@ -4580,7 +4574,7 @@ const order = await prisma.order.create({
set: null,
},
},
-})
+});
```
### `unset`
@@ -4603,7 +4597,7 @@ const order = await prisma.order.update({
unset: true,
},
},
-})
+});
```
### `update`
@@ -4631,7 +4625,7 @@ const order = await prisma.order.update({
},
},
},
-})
+});
```
### `upsert`
@@ -4667,7 +4661,7 @@ const order = await prisma.order.update({
},
},
},
-})
+});
```
### `push`
@@ -4689,7 +4683,7 @@ const product = prisma.product.update({
push: [{ height: 100, width: 200, url: '1.jpg' }],
},
},
-})
+});
```
## Composite type filters
@@ -4733,7 +4727,7 @@ const orders = await prisma.order.findMany({
},
},
},
-})
+});
```
##### Find products with photos that match all of a list of `url`s
@@ -4745,7 +4739,7 @@ const product = prisma.product.findMany({
photos: [{ url: '1.jpg' }, { url: '2.jpg' }],
},
},
-})
+});
```
### `is`
@@ -4765,7 +4759,7 @@ const orders = await prisma.order.findMany({
},
},
},
-})
+});
```
### `isNot`
@@ -4785,7 +4779,7 @@ const orders = await prisma.order.findMany({
},
},
},
-})
+});
```
### `isEmpty`
@@ -4803,7 +4797,7 @@ const product = prisma.product.findMany({
isEmpty: true,
},
},
-})
+});
```
### `every`
@@ -4922,7 +4916,7 @@ const updatePosts = await prisma.post.updateMany({
increment: 1,
},
},
-})
+});
```
#### Set all `views` fields of all `Post` records to `0`
@@ -4934,7 +4928,7 @@ const updatePosts = await prisma.post.updateMany({
set: 0,
},
},
-})
+});
```
Can also be written as:
@@ -4944,7 +4938,7 @@ const updatePosts = await prisma.post.updateMany({
data: {
views: 0,
},
-})
+});
```
## `Json` filters
@@ -4999,7 +4993,7 @@ const getUsers = await prisma.user.findMany({
equals: 'Rottweiler',
},
},
-})
+});
```
@@ -5013,7 +5007,7 @@ const getUsers = await prisma.user.findMany({
equals: 'Rottweiler',
},
},
-})
+});
```
@@ -5032,7 +5026,7 @@ const getUsers = await prisma.user.findMany({
array_contains: ['Elliott'],
},
},
-})
+});
```
@@ -5046,7 +5040,7 @@ const getUsers = await prisma.user.findMany({
array_contains: 'Elliott',
},
},
-})
+});
```
@@ -5068,7 +5062,7 @@ const getUsers = await prisma.user.findMany({
array_contains: 'Dreamies',
},
},
-})
+});
```
### `string_contains`
@@ -5086,7 +5080,7 @@ const getUsers = await prisma.user.findMany({
string_contains: 'Van',
},
},
-})
+});
```
@@ -5100,7 +5094,7 @@ const getUsers = await prisma.user.findMany({
string_contains: 'Van',
},
},
-})
+});
```
@@ -5122,7 +5116,7 @@ const getUsers = await prisma.user.findMany({
string_starts_with: 'Turkish',
},
},
-})
+});
```
@@ -5137,7 +5131,7 @@ const getUsers = await prisma.user.findMany({
string_starts_with: 'Turkish',
},
},
-})
+});
```
@@ -5159,7 +5153,7 @@ const getUsers = await prisma.user.findMany({
string_ends_with: 'Van',
},
},
-})
+});
```
@@ -5174,7 +5168,7 @@ const getUsers = await prisma.user.findMany({
string_ends_with: 'Van',
},
},
-})
+});
```
@@ -5196,7 +5190,7 @@ const getUsers = await prisma.user.findMany({
array_contains: ['RSPCA'],
},
},
-})
+});
```
@@ -5216,7 +5210,7 @@ const getUsers = await prisma.user.findMany({
array_contains: 'RSPCA',
},
},
-})
+});
```
@@ -5235,7 +5229,7 @@ const getUsers = await prisma.user.findMany({
array_contains: ['RSPCA', 'Alley Cat Allies'],
},
},
-})
+});
```
@@ -5249,7 +5243,7 @@ const getUsers = await prisma.user.findMany({
array_contains: ['RSPCA', 'Alley Cat Allies'],
},
},
-})
+});
```
@@ -5270,7 +5264,7 @@ const getUsers = await prisma.user.findMany({
array_starts_with: 'RSPCA',
},
},
-})
+});
```
@@ -5285,7 +5279,7 @@ const getUsers = await prisma.user.findMany({
array_starts_with: 'RSPCA',
},
},
-})
+});
```
@@ -5306,7 +5300,7 @@ const getUsers = await prisma.user.findMany({
array_ends_with: 'Alley Cat Allies',
},
},
-})
+});
```
@@ -5320,7 +5314,7 @@ const getUsers = await prisma.user.findMany({
array_ends_with: 'Alley Cat Allies',
},
},
-})
+});
```
@@ -5380,11 +5374,11 @@ The `$use()` method adds [middleware](/orm/prisma-client/client-extensions/middl
```ts
prisma.$use(async (params, next) => {
- console.log('This is middleware!')
+ console.log('This is middleware!');
// Modify or interrogate params here
- return next(params)
-})
+ return next(params);
+});
```
#### `next`
@@ -5488,7 +5482,7 @@ There are two ways you can use the `validator`:
Using types provides a type-level approach to validate data:
```ts
-Prisma.validator({ args })
+Prisma.validator({ args });
```
#### Using a "selector"
@@ -5498,12 +5492,7 @@ When using the selector pattern, you use an existing Prisma Client instance to c
You can also use an instance of Prisma Client that has been extended using a [Prisma Client extension](/orm/prisma-client/client-extensions).
```ts
-Prisma.validator(
- PrismaClientInstance,
- '',
- '',
- ''
-)({ args })
+Prisma.validator(PrismaClientInstance, '', '', '')({ args });
```
#### Examples
@@ -5511,7 +5500,7 @@ Prisma.validator(
The following example shows how you can extract and validate the input for the `create` operation you can reuse within your app:
```ts
-import { Prisma } from '@prisma/client'
+import { Prisma } from '@prisma/client';
const validateUserAndPostInput = (name, email, postTitle) => {
return Prisma.validator()({
@@ -5522,15 +5511,15 @@ const validateUserAndPostInput = (name, email, postTitle) => {
title: postTitle,
},
},
- })
-}
+ });
+};
```
Here is an alternative syntax for the same operation:
```ts
-import { Prisma } from '@prisma/client'
-import prisma from './prisma'
+import { Prisma } from '@prisma/client';
+import prisma from './prisma';
const validateUserAndPostInput = (name, email, postTitle) => {
return Prisma.validator(
@@ -5546,8 +5535,8 @@ const validateUserAndPostInput = (name, email, postTitle) => {
title: postTitle,
},
},
- })
-}
+ });
+};
```
## Compare columns in the same table
@@ -5572,7 +5561,7 @@ To compare columns in the same table, use the `.fields` property. In the
```ts
prisma.product.findMany({
where: { quantity: { lte: prisma.product.fields.warnQuantity } },
-})
+});
```
@@ -5594,7 +5583,7 @@ await prisma.order.findMany({
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Type error: id is a string, while amountDue is an integer
},
-})
+});
```
#### Fields must be in the same model
@@ -5608,7 +5597,7 @@ await prisma.order.findMany({
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Type error: name is a field on the User model, not Order
},
-})
+});
```
However, you can compare fields in separate models with [standard queries](#model-queries).
@@ -5623,7 +5612,7 @@ The following example works:
prisma.user.groupBy({
by: ['id', 'name'],
having: { id: { equals: prisma.user.fields.name } },
-})
+});
```
The following example does not work, because `name` is not in the `by` argument:
@@ -5634,7 +5623,7 @@ prisma.user.groupBy({
having: { id: { equals: prisma.user.fields.name } },
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// name is not in the 'by' argument
-})
+});
```
#### Search for fields in scalar lists
@@ -5647,7 +5636,7 @@ await prisma.user.findMany({
// find all users where 'name' is in a list of tags
name: { in: prisma.user.fields.tags },
},
-})
+});
```
## Filter on non-unique fields with `UserWhereUniqueInput`
@@ -5682,25 +5671,25 @@ model User {
```ts
function updateOne() {
- const user = await prisma.user.findUnique({ id: 1 })
+ const user = await prisma.user.findUnique({ id: 1 });
await prisma.user.update({
where: { id: user.id, version: user.version },
data: { city: 'Berlin', version: { increment: 1 } },
- })
+ });
}
function updateTwo() {
- const user = await prisma.user.findUnique({ id: 1 })
+ const user = await prisma.user.findUnique({ id: 1 });
await prisma.user.update({
where: { id: user.id, version: user.version },
data: { city: 'New York', version: { increment: 1 } },
- })
+ });
}
function main() {
- await Promise.allSettled([updateOne(), updateTwo()])
+ await Promise.allSettled([updateOne(), updateTwo()]);
}
```
@@ -5714,7 +5703,7 @@ In the following example, a user wants to update a post title. The `where` state
await prisma.post.update({
where: { id: 1, authorId: 1 },
data: { title: 'Updated post title' },
-})
+});
```
### Soft deletes
@@ -5724,7 +5713,7 @@ You can filter on non-unique fields to handle soft deletes.
In the following example, we do not want to return a post if it is soft-deleted. The operation only returns the post if the value in `isDeleted` is `false`.
```ts
-prisma.Post.findUnique({ where: { id: postId, isDeleted: false } })
+prisma.Post.findUnique({ where: { id: postId, isDeleted: false } });
```
### `UserWhereUniqueInput` considerations
@@ -5839,11 +5828,11 @@ All Prisma Client queries return an instance of `PrismaPromise`. This is a ["the
For example:
```ts
-const findPostOperation = prisma.post.findMany({}) // Query not yet executed
+const findPostOperation = prisma.post.findMany({}); // Query not yet executed
-findPostOperation.then() // Prisma Client now executes the query
+findPostOperation.then(); // Prisma Client now executes the query
// or
-await findPostOperation // Prisma Client now executes the query
+await findPostOperation; // Prisma Client now executes the query
```
When using the [`$transaction` API](/orm/prisma-client/queries/transactions#the-transaction-api), this behavior makes it possible for Prisma Client to pass all the queries on to the query engine as a single transaction.