# ORM client reference (/docs/orm/reference/orm-client) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Reference for the Prisma ORM client's query, mutation, filter, and aggregate methods. Location: ORM > Reference > ORM client reference The ORM client gives you model-level methods for reading and writing data across PostgreSQL and MongoDB. This page documents every method, its availability on each database, and the behavior that differs between the two. Each method's Remarks say which databases it works on, how it behaves differently on one of them, and where TypeScript rejects something the database itself would accept. This page assumes your tables already exist. [Coming from Prisma ORM 7](https://www.prisma.io/docs/orm/coming-from-prisma-orm-7) has the packages to install and the `prisma orm init` command. It also covers `prisma db update`, which replaces `prisma migrate dev`, and `prisma contract emit`, which replaces `prisma generate`. [How migrations work](https://www.prisma.io/docs/orm/migrations/how-migrations-work) shows how the tables get created. For task-oriented walkthroughs, see the Fundamentals guides: [Reading data](https://www.prisma.io/docs/orm/fundamentals/reading-data), [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data), [Relations and joins](https://www.prisma.io/docs/orm/fundamentals/relations-and-joins), and [Transactions](https://www.prisma.io/docs/orm/fundamentals/transactions). ## Example schema [#example-schema] All examples on this page run against the schema below, except the grouped-aggregate ones, which use a separate `Customer` / `Order` schema shown under [Grouped aggregates](#grouped-aggregates), and the row-locking example, which uses a `Job` model shown beside it. `contract.prisma` replaces `schema.prisma`, and this page calls it the contract. Running `npx prisma contract emit` compiles it into two generated files: `contract.json`, read at run time, and `contract.d.ts`, read by TypeScript. You import both, as the examples below do. Run `npx prisma contract emit` again after every change to `contract.prisma`. A new project keeps all three files in `src/prisma/`. **Expand for the example schema** #### PostgreSQL ```prisma types { Embedding1536 = pgvector.Vector(1536) } type Address { street String city String zip String? country String } enum user_type { @@type("pg/text@1") admin user } enum Priority { @@type("pg/text@1") Low = "low" High = "high" Urgent = "urgent" } model User { id Uuid @id @onCreate(uuid()) email String @unique displayName String createdAt DateTime @default(now()) kind user_type address Address? posts Post[] tasks Task[] @@map("user") } model Post { id Uuid @id @onCreate(uuid()) title String userId Uuid priority Priority @default(Low) createdAt DateTime @default(now()) embedding Embedding1536? user User @relation(fields: [userId], references: [id]) tags Tag[] @@map("post") } model Tag { id Uuid @id @onCreate(uuid()) label String @unique posts Post[] @@map("tag") } model PostTag { postId Uuid tagId Uuid post Post @relation(fields: [postId], references: [id]) tag Tag @relation(fields: [tagId], references: [id]) @@id([postId, tagId]) @@map("post_tag") } model Task { id Uuid @id @onCreate(uuid()) title String description String? status String @default("open") type String userId Uuid createdAt DateTime @default(now()) user User @relation(fields: [userId], references: [id]) @@discriminator(type) @@map("task") } model Bug { severity String stepsToRepro String? @@base(Task, "bug") @@map("bug") } model Feature { priority String targetRelease String? @@base(Task, "feature") @@map("feature") } ``` #### MongoDB ```prisma enum UserRole { @@type("mongo/string@1") Admin = "admin" Author = "author" Reader = "reader" } type Address { street String city String zip String? country String } model User { id ObjectId @id @map("_id") name String email String bio String? role UserRole address Address? posts Post[] @@map("users") } model Post { id ObjectId @id @map("_id") title String content String kind String authorId ObjectId createdAt DateTime author User @relation(fields: [authorId], references: [id]) @@discriminator(kind) @@index([authorId]) @@index([sort(createdAt, Desc), authorId]) @@map("posts") } model Article { summary String @@base(Post, "article") @@unique([summary]) } model Tutorial { difficulty String duration Int32 @@base(Post, "tutorial") } ``` Some parts of the schema the examples rely on: - The `types` block at the top gives a name to a type from an extension package, so your models can use that name. `Embedding1536` comes from the pgvector extension, and `pgvector.` is that package's prefix. See [PostgreSQL](#postgresql) for the extra line it needs when you create the client. - `@@map("user")` sets the table or collection name. Without it, the table name is the model name exactly as written, `User`. - On MongoDB, `id ObjectId @id @map("_id")` stores the field as `_id` in the database. The ORM names it `id`, its name in the schema: you filter on `id`, write `id`, and the returned document has an `id` key. The same holds for every field with `@map`. The [pipeline builder](https://www.prisma.io/docs/orm/reference/pipeline-builder) and [raw commands](https://www.prisma.io/docs/orm/reference/raw-queries) work on stored documents, so they name the field `_id`. - `Uuid` is a built-in type for a PostgreSQL `uuid` column. You do not import it. `@onCreate(uuid())` makes Prisma ORM fill the id in when you insert a row. - `@@type("pg/text@1")` on an `enum` block stores the enum as a text column. The `@1` is the version of that storage format, and only version 1 exists today, so write it exactly like that. The MongoDB form of the same attribute is `@@type("mongo/string@1")`. This page calls an enum written this way a text-backed enum. - A `native_enum` block stores the members as a PostgreSQL enum type, created with `CREATE TYPE`. Prisma ORM 7 stored every `enum` this way, so a database that Prisma ORM 7 created already has these types, and you keep them by declaring each one with `native_enum`, as [Coming from Prisma ORM 7](https://www.prisma.io/docs/orm/coming-from-prisma-orm-7) shows. A plain `enum` block in Prisma ORM 8 is stored as text. Give every `native_enum` member a value, because a member with no value is rejected. A field that uses it is typed `pg.enum(Priority)`. This page calls an enum written this way a native enum. The two sort differently; see [`orderBy()`](#orderby). ```prisma native_enum Priority { Low = "low" High = "high" Urgent = "urgent" } model Post { id Uuid @id @onCreate(uuid()) priority pg.enum(Priority) } ``` - An enum member's stored value is the string after `=`, or the member name when there is no `=`. That string is what you pass to `.eq()`. So `Urgent = "urgent"` is matched by `.eq('urgent')`, and a bare `admin` member is matched by `.eq('admin')`. - A variant is a model that reuses another model's fields and rows. `@@base(Task, "bug")` makes `Bug` a variant of `Task`. Its second argument, `"bug"`, is the value stored in the discriminator column for a `Bug` row. `@@discriminator(type)` names that column. `db.orm.public.Task.variant('bug')` returns only the `Bug` rows. Pass the string in `@@base`, `'bug'`, not the model name. - `PostTag` is required. To join `Post.tags Tag[]` and `Tag.posts Post[]`, write a third model with one foreign key to each side and an `@@id` of exactly those two columns. Prisma ORM finds it for you, and neither `Post` nor `Tag` names it. A pair of list fields with no such model is rejected. See [Relations and joins](https://www.prisma.io/docs/orm/fundamentals/relations-and-joins#many-to-many). - On PostgreSQL, a `DateTime` field comes back as a JavaScript `Date`, and a field written `DateTime(as: temporal)` as a `Temporal.Instant`. A filter on a field takes the same type it returns, so you can pass a value straight back in. ## Setting up the client [#setting-up-the-client] Create a client with `postgres(...)` or `mongo(...)`, then read and write your models through `db.orm`. The same `db` also has a query builder, `db.sql` on PostgreSQL and `db.query` on MongoDB, plus `db.raw` for raw queries. This page covers none of those three: see [Advanced queries](https://www.prisma.io/docs/orm/fundamentals/advanced-queries). Create `db` once per process, and close it with `await db.close()` when the process shuts down. The two databases have different entry points, and they name the models differently. ### PostgreSQL [#postgresql] Create a PostgreSQL client with `postgres(...)`. `db.orm` holds your models by model name, grouped by database schema: `db.orm.public.User`, `db.orm.public.Post`. `public` is the PostgreSQL schema your tables are in unless you put them in another one. Import types from `./contract.d`, which resolves to `contract.d.ts`. ```typescript import postgres from '@prisma/orm-postgres/runtime'; import { contractFromJson } from '@prisma/orm-postgres/static'; import type { Contract } from './contract.d'; import contractJson from './contract.json' with { type: 'json' }; export const contract = contractFromJson(contractJson); export const db = postgres({ contract, url: process.env.DATABASE_URL! }); const users = await db.orm.public.User.all(); ``` If your contract uses a type from an extension package, pass that package to `extensions` when you create the client. The example schema's `Embedding1536` type comes from pgvector, so it needs this: ```typescript import pgvector from '@prisma/orm-extension-pgvector/runtime'; const db = postgres({ contract, url: process.env.DATABASE_URL!, extensions: [pgvector], }); ``` To give a model your own methods on top of the built-in ones, import `Collection` from `@prisma/orm-postgres/orm-client`, subclass it, and pass the subclass to `postgres(...)` in the `collections` option. The code is under [Custom `Collection` subclass](#custom-collection-subclass). ### MongoDB [#mongodb] Create a MongoDB client with `mongo(...)`. `db.orm` holds your models by collection name, with no schema in between. The collection name is the one you set with `@@map`, or the model name exactly as written when you did not set one. The example schema maps `User` to `users`, so it is `db.orm.users`, and `Post` is `db.orm.posts`. `dbName` is the MongoDB database name. ```typescript import mongo from '@prisma/orm-mongo/runtime'; import { contractFromJson } from '@prisma/orm-mongo/static'; import type { Contract } from './contract.d'; import contractJson from './contract.json' with { type: 'json' }; export const contract = contractFromJson(contractJson); export const db = mongo({ contract, url: process.env.DATABASE_URL!, dbName: 'app' }); const users = await db.orm.users.all(); ``` ## Query-building methods [#query-building-methods] These methods narrow a query, and each returns a collection. On this page, a collection is the query object you chain more methods on, such as `db.orm.public.User` or `db.orm.public.User.where(...)`. When this page says "a MongoDB collection", it means that query object. MongoDB itself calls its store of documents a collection too, and the name is the same one. You run the query with a [read method](#read-terminals) such as `all()` or `first()`. MongoDB supports fewer of these methods than PostgreSQL, and each method's Remarks say which databases it works on. Some examples import a filter helper you can use on its own. On PostgreSQL those come from `@prisma/orm-postgres/orm-client`, on MongoDB from `@prisma/orm-mongo/query-ast/execution`. See [Filter conditions and operators](#filter-conditions-and-operators). The examples below use these ids, which stand for rows inserted before the query runs: ```typescript const aliceId = '00000000-0000-4000-8000-000000000001'; const carolId = '00000000-0000-4000-8000-000000000003'; const postId = '00000000-0000-4000-8000-000000000010'; const acmeId = '00000000-0000-4000-8000-000000000020'; ``` ### `where()` [#where] Restrict a query to rows matching a filter. #### Remarks [#remarks] - Available for PostgreSQL and MongoDB, but the accepted filter shapes differ. - On PostgreSQL, `where()` takes either a callback that calls an operator on a column (`u.email.eq(...)`) or an object of field-and-value pairs that must match exactly. - On MongoDB, `where()` takes either an object of field-and-value pairs that must match exactly or a `MongoFieldFilter`. The object form can only test for equality. For anything else, such as greater-than, use `MongoFieldFilter` (see [MongoFieldFilter](#mongofieldfilter)). - Calling `where()` more than once on the same query requires a row to match all of the filters. - A filter value has the type you write to the field. A field whose type has parameters, such as `String(storage: varchar, length: 50)`, reads as `Varchar<50>` but takes a plain `string` in `where()`, `first()`, `create()`, `update()`, and `upsert()`, so you need no cast. The same holds for `Char<36>` and `Numeric<10, 2>`. - `.eq()` is one operator of many. [Filter conditions and operators](#filter-conditions-and-operators), further down this page, lists them all for both databases. #### Options [#options] | Argument | Type | Required | Description | | -------- | ---------------------------------------------------------------------------------------------------- | -------- | -------------------------------- | | `filter` | A callback that calls an operator on a column, a shorthand object, or (MongoDB) a `MongoFieldFilter` | Yes | The condition rows must satisfy. | #### Return type [#return-type] | Return type | Example | Description | | ------------ | ------------------------------- | ------------------------------------------------------------------------------------------------------- | | `Collection` | `db.orm.public.User.where(...)` | A collection narrowed by the filter. Chain more methods on it, then run it with a read or write method. | #### Examples [#examples] ##### Callback form with a column operator (PostgreSQL) [#callback-form-with-a-column-operator-postgresql] ```typescript const admins = await db.orm.public.User.where((u) => u.kind.eq('admin')).all(); ``` ##### Shorthand object form [#shorthand-object-form] #### PostgreSQL ```typescript const bob = await db.orm.public.User.where({ email: 'bob@example.com' }).first(); ``` #### MongoDB ```typescript const authors = await db.orm.users.where({ role: 'author' }).all(); ``` ##### Chaining `where()` calls (ANDed) [#chaining-where-calls-anded] ```typescript const carolUrgentPosts = await db.orm.public.Post.where({ userId: carolId }) .where((p) => p.priority.eq('urgent')) .all(); ``` ##### `MongoFieldFilter` expression (MongoDB) [#mongofieldfilter-expression-mongodb] ```typescript import { MongoFieldFilter } from '@prisma/orm-mongo/query-ast/execution'; const alice = await db.orm.users.where(MongoFieldFilter.eq('email', 'alice@example.com')).first(); const recentPosts = await db.orm.posts .where(MongoFieldFilter.gte('createdAt', new Date('2024-01-02T00:00:00.000Z'))) .all(); ``` For a task-oriented guide to filtering, see [Reading data](https://www.prisma.io/docs/orm/fundamentals/reading-data#filter-records). ### `whereUnique()` [#whereunique] Restrict a query to the one row a primary key or a unique constraint names. #### Remarks [#remarks-1] - Available on PostgreSQL, SQLite, and MySQL. Not available on MongoDB yet. - The criterion is an object with a value for every field of the primary key or of one `@@unique` constraint, such as `{ id }`, `{ email }` or `{ tenantId, userId }`. A field that is not part of such a key, or a key with a field missing, is a type error. When the call runs, a criterion without a value for every field of one key throws an error whose `code` is `ORM.ARGUMENT_INVALID`. - It filters exactly as `where()` with the same object does. A value cannot be `null`, because a unique constraint allows any number of rows whose column is `NULL`; to read those, use `where({ field: null })`. - After it, the calls that address many rows are compile errors: `orderBy()`, `limit()`, `offset()`, `cursor()`, `distinct()`, `distinctOn()`, `all()`, `aggregate()`, `groupBy()`, `updateAll()`, `updateAndCount()`, `deleteAll()`, `deleteAndCount()`, and `prepared.all()` and `prepared.aggregate()`. `first()`, `firstOrThrow()`, `update()`, `delete()`, `select()`, `include()` and a further `where()` still compile. - Inside an `include()` refinement it throws an error whose `code` is `ORM.INCLUDE_INVALID`. #### Examples [#examples-1] ```typescript const post = await db.orm.public.Post.whereUnique({ id }).first(); const membership = await db.orm.public.Membership.whereUnique({ tenantId, userId }).firstOrThrow(); const removed = await db.orm.public.User.whereUnique({ email }).delete(); ``` ### `select()` [#select] Return only the scalar fields you name. #### Remarks [#remarks-2] - Available for PostgreSQL and MongoDB. - On PostgreSQL, `select()` narrows the returned row shape at the type level: fields you didn't select are absent from the result type. - On MongoDB, `select()` changes what the database returns but not the TypeScript type. Only the fields you named come back. The type still lists every field on the model. Read a field you did not name and you get `undefined`, because the returned document does not carry it. #### Options [#options-1] | Argument | Type | Required | Description | | ----------- | ---------------------- | -------- | --------------------------------------- | | `...fields` | Field names (`string`) | Yes | One or more scalar field names to keep. | #### Return type [#return-type-1] | Return type | Example | Description | | ------------ | ------------------------------------------ | ------------------------------------------- | | `Collection` | `db.orm.public.User.select('id', 'email')` | A collection projected to the named fields. | #### Examples [#examples-2] ##### Project to a subset of fields [#project-to-a-subset-of-fields] #### PostgreSQL ```typescript const summaries = await db.orm.public.User.select('id', 'email').orderBy((u) => u.email.asc()).all(); // summaries[0] is { id, email }, with no displayName ``` #### MongoDB ```typescript const summaries = await db.orm.users.select('name', 'email').all(); ``` ### `include()` [#include] Eagerly load a relation onto the returned rows. #### Remarks [#remarks-3] - Available for PostgreSQL and MongoDB, with differences noted below. - On PostgreSQL, `include(relationName, refineFn?)` loads both to-one and to-many relations. The optional callback receives the related rows as a collection, so you can filter, order, limit, and aggregate them. See [Refinements, aggregates, and combine](#refinements-aggregates-and-combine). - On MongoDB, a related document loaded by `include()` comes back with the same field names and value types as when you read it on its own, so its `id` is a hex string. - On MongoDB, `include()` loads a relation that is stored as a reference. It takes the relation name and nothing else, and TypeScript rejects a second argument. #### Options [#options-2] | Argument | Type | Required | Description | | -------------- | ------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------- | | `relationName` | `string` | Yes | The relation to load. | | `refineFn` | A callback that returns the narrowed relation or an aggregate of it | No | PostgreSQL only. Narrows the loaded relation, or reduces it to one value. | #### Return type [#return-type-2] | Return type | Example | Description | | ------------ | ------------------------------------- | -------------------------------------------------- | | `Collection` | `db.orm.public.User.include('posts')` | A collection whose rows carry the loaded relation. | #### Examples [#examples-3] ##### To-one relation (PostgreSQL) [#to-one-relation-postgresql] ```typescript const posts = await db.orm.public.Post.include('user').where({ id: postId }).all(); // posts[0].user is the related User ``` ##### To-many relation (PostgreSQL) [#to-many-relation-postgresql] ```typescript const users = await db.orm.public.User.include('posts').where({ id: aliceId }).all(); // users[0].posts is an array of the user's posts ``` ##### Reference relation (MongoDB) [#reference-relation-mongodb] ```typescript const posts = await db.orm.posts.include('author').where({ title: 'Hello world' }).all(); // posts[0].author.id is a hex string, like posts[0].id ``` For a task-oriented guide to loading related records, see [Relations and joins](https://www.prisma.io/docs/orm/fundamentals/relations-and-joins). ### Refinements, aggregates, and combine [#refinements-aggregates-and-combine] On PostgreSQL, the callback you pass to `include()` receives the related rows as a collection. This page calls that callback a refinement, which is the word in the heading above. You can filter, order, and paginate the related rows in it, and you can also reduce them to a single value, or use `combine()` to return several results at once. To count or sum a whole model instead of a relation, see [Grouped aggregates](#grouped-aggregates), where `aggregate((agg) => ({ n: agg.count() }))` counts every row. #### Remarks [#remarks-4] - PostgreSQL only. On MongoDB, `include()` takes no callback, and `count`, `sum`, `avg`, `min`, `max`, and `combine` do not exist on a collection. - On PostgreSQL, those six are only callable **inside** an `include()` callback. Called anywhere else, they throw an error whose `code` is `ORM.INCLUDE_INVALID`. - `sum()` and `avg()` take a numeric column: an integer, a floating-point number, or a decimal. They also take an interval or a `Time` column, the time-of-day type. They do not take a date or a timestamp, and TypeScript rejects a date or timestamp field name here. - `min()` and `max()` take more: numeric and text columns, dates, times, timestamps, intervals, IP addresses, and text arrays. They do not take a boolean, a `uuid`, binary data, a bit string, or JSON. - `count()` returns a `number`. With no argument it counts rows; with a field name it counts rows where that field is not null. `countBigInt()` returns a `bigint` instead, for a count too large for a JavaScript `number`. - `sum()` over an integer column returns a `number`. A total too large for a `number` throws an error whose `code` is `RUNTIME.DECODE_FAILED`. Use `sumBigInt(field)` for totals that large. It returns a `bigint`. - `avg()` over an integer column returns a JavaScript floating-point `number`. `avgDecimal(field)` returns the exact average as a decimal string. Pass that string to a decimal library, or call `Number(...)` on it when a floating-point value is fine. - When a to-many relation has no rows, `sum()`, `avg()`, `min()`, and `max()` come back as `null`, not `0`. - `combine(shape)` takes an object. You choose the keys. Call `posts.combine({ ... })`, and inside the object write `posts` again for each value. The `posts` inside the object is the same collection the callback received, with nothing chained on it yet. A value that chains more methods on `posts` comes back as an array of rows. A value that is an aggregate, such as `posts.count()`, comes back as that single value. #### Options [#options-3] | Argument | Type | Required | Description | | ------------------------------------------------ | ------------------------------------------ | -------- | -------------------------------------------------------------------------------- | | `where()` / `orderBy()` / `limit()` / `offset()` | Chained on the nested collection | No | Refine which related rows load. | | `count()` | Aggregate, no arguments | No | Reduces the relation to a row count. | | `sum(field)` / `avg(field)` | Aggregate over a numeric field | No | Reduces the relation to one number. | | `min(field)` / `max(field)` | Aggregate over a sortable field | No | Reduces the relation to its smallest or largest value, in the column's own type. | | `combine(shape)` | Object of named collections and aggregates | No | Returns several results under one relation key. | #### Return type [#return-type-3] | Return type | Example | Description | | ----------------------------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------- | | The narrowed relation, a single value, or an object of both | `include('posts', (p) => p.count())` | The relation key on each row holds the callback's result instead of the related rows. | #### Examples [#examples-4] ##### Filter, order, and limit within a relation (PostgreSQL) [#filter-order-and-limit-within-a-relation-postgresql] ```typescript const users = await db.orm.public.User.include('posts', (posts) => posts .where((p) => p.priority.eq('low')) .orderBy((p) => p.createdAt.desc()) .limit(1), ) .where({ id: aliceId }) .all(); ``` ##### Reduce a relation to a count (PostgreSQL) [#reduce-a-relation-to-a-count-postgresql] ```typescript const users = await db.orm.public.User.include('posts', (posts) => posts.count()) .where({ id: aliceId }) .all(); // users[0].posts is the number 2 ``` ##### `sum()` / `avg()` over a numeric relation (PostgreSQL) [#sum--avg-over-a-numeric-relation-postgresql] This example uses the `Customer` and `Order` models, which are in the separate schema shown under [Grouped aggregates](#grouped-aggregates). ```typescript const customers = await db.orm.public.Customer.include('orders', (orders) => orders.sum('amount')) .where({ id: acmeId }) .all(); // customers[0].orders is 1500 const avgCustomers = await db.orm.public.Customer.include('orders', (orders) => orders.avg('amount')) .where({ id: acmeId }) .all(); // avgCustomers[0].orders is 300 ``` ##### `combine()` multiple sub-views (PostgreSQL) [#combine-multiple-sub-views-postgresql] ```typescript const users = await db.orm.public.User.include('posts', (posts) => posts.combine({ recent: posts.orderBy((p) => p.createdAt.desc()).limit(1), total: posts.count(), }), ) .where({ id: aliceId }) .all(); // users[0].posts.total is 2; users[0].posts.recent is a one-element array ``` > [!NOTE] > `min()` > > and > > `max()` > > over a date column > > `min('createdAt')` over a `DateTime` column compiles and returns the field's type, a JavaScript `Date`. Over a field read as a `Temporal` value, it returns that value, which needs a global `Temporal`: check with `typeof Temporal === 'undefined'`, and if your engine has none, install `temporal-polyfill` and add `import 'temporal-polyfill/full/global'`. > > `sum('createdAt')` and `avg('createdAt')` do not compile. ### `orderBy()` [#orderby] Sort the result set. #### Remarks [#remarks-5] - Available for PostgreSQL and MongoDB, with different argument shapes. - On PostgreSQL, `orderBy()` takes a callback that returns `.asc()` or `.desc()` on one column. Pass an array of such callbacks to sort by more than one column. - On PostgreSQL, the callback can also sort by a field of a related record, for a relation to one record and one relation deep, as in `(p) => p.user.displayName.asc()`. - On PostgreSQL, the callback can also sort by the number of related records, for a relation to many records, as in `(u) => u.posts.count().desc()`. `count()` takes an optional filter, the same one `some()` takes. - On PostgreSQL, `.asc()` and `.desc()` take `{ nulls: "first" }` or `{ nulls: "last" }`. Without it, PostgreSQL puts nulls last for `.asc()` and first for `.desc()`. - On MongoDB, `orderBy()` takes an object instead: `{ field: 1 }` sorts ascending and `{ field: -1 }` sorts descending. - The example schema declares `Priority` text-backed, so on PostgreSQL it sorts by its stored text: `'high'`, then `'low'`, then `'urgent'`. A native enum column sorts in the order its members are declared instead, so the same members in a `native_enum Priority` block sort `Low`, `High`, `Urgent`. Use a native enum when the sort order matters. #### Options [#options-4] | Argument | Type | Required | Description | | -------- | ------------------------------------------------------------------------------------------------------------------------------------ | -------- | --------------------------------- | | `sort` | Callback `(fields) => f.field.asc() \| .desc()`, an array of such callbacks (PostgreSQL), or a `{ field: 1 \| -1 }` object (MongoDB) | Yes | The sort key(s) and direction(s). | #### Return type [#return-type-4] | Return type | Example | Description | | ------------ | --------------------------------- | -------------------------------------- | | `Collection` | `db.orm.public.Post.orderBy(...)` | A collection with an ordering applied. | #### Examples [#examples-5] ##### Ascending and descending [#ascending-and-descending] #### PostgreSQL ```typescript const newestFirst = await db.orm.public.Post.where({ userId: aliceId }) .orderBy((p) => p.createdAt.desc()) .all(); ``` #### MongoDB ```typescript const newestFirst = await db.orm.posts.orderBy({ createdAt: -1 }).all(); ``` ##### Multiple sort keys (PostgreSQL) [#multiple-sort-keys-postgresql] ```typescript const byPriorityThenDate = await db.orm.public.Post.orderBy([ (p) => p.priority.asc(), (p) => p.createdAt.asc(), ]).all(); // Priority is text-backed here, so it sorts as 'high', 'low', 'urgent' ``` ### `limit()` [#limit] Limit the number of returned rows. #### Remarks [#remarks-6] - Available for PostgreSQL and MongoDB. #### Options [#options-5] | Argument | Type | Required | Description | | -------- | -------- | -------- | --------------------------------- | | `count` | `number` | Yes | Maximum number of rows to return. | #### Return type [#return-type-5] | Return type | Example | Description | | ------------ | ----------------------------- | ------------------------------------- | | `Collection` | `db.orm.public.Post.limit(2)` | A collection limited to `count` rows. | #### Examples [#examples-6] ##### Limit the result set [#limit-the-result-set] #### PostgreSQL ```typescript const firstTwo = await db.orm.public.Post.orderBy((p) => p.createdAt.asc()).limit(2).all(); ``` #### MongoDB ```typescript const firstOne = await db.orm.posts.orderBy({ createdAt: 1 }).limit(1).all(); ``` ### `offset()` [#offset] Offset into the ordered result set. #### Remarks [#remarks-7] - Available for PostgreSQL and MongoDB. - Combine with `orderBy()` and `limit()` for pagination. #### Options [#options-6] | Argument | Type | Required | Description | | -------- | -------- | -------- | ----------------------- | | `count` | `number` | Yes | Number of rows to skip. | #### Return type [#return-type-6] | Return type | Example | Description | | ------------ | ------------------------------ | ------------------------------------ | | `Collection` | `db.orm.public.Post.offset(2)` | A collection offset by `count` rows. | #### Examples [#examples-7] ##### Offset into the result set [#offset-into-the-result-set] #### PostgreSQL ```typescript const page2 = await db.orm.public.Post.orderBy((p) => p.createdAt.asc()).offset(2).limit(2).all(); ``` #### MongoDB ```typescript const secondPost = await db.orm.posts.orderBy({ createdAt: 1 }).offset(1).limit(1).all(); ``` ##### Related records, counts, and nulls (PostgreSQL) [#related-records-counts-and-nulls-postgresql] ```typescript const byAuthor = await db.orm.public.Post.orderBy([ (p) => p.user.displayName.asc(), (p) => p.id.asc(), ]).all(); const mostPosts = await db.orm.public.User.orderBy((u) => u.posts.count().desc()).all(); const described = await db.orm.public.Task.orderBy((t) => t.description.desc({ nulls: "last" })).all(); ``` ### `cursor()` [#cursor] Resume pagination from a known position. #### Remarks [#remarks-8] - PostgreSQL only. `cursor()` does not exist on a MongoDB collection. - Every sort in the `orderBy()` must be a plain column of the model. A sort by a related record, a count, an extension operation such as a vector distance, or a sort with `nulls` makes `cursor()` throw an error whose `code` is `ORM.ARGUMENT_INVALID`. Page such a query with `limit()` and `offset()`. - Always call `orderBy()` before `cursor()`. TypeScript rejects the call if you do not. Nothing checks this while the query runs, so without the `orderBy()` the cursor is ignored and every row comes back. - The cursor object must name every column the `orderBy()` sorts on. Leave one out and running the query throws an error whose `code` is `ORM.CURSOR_VALUE_MISSING`. With two sort columns, give both keys. The cursor compares the sort columns in order: first by the first column, then by the second for rows that tie on the first. #### Options [#options-7] | Argument | Type | Required | Description | | -------- | ------------------------------------------------- | -------- | ----------------------------- | | `values` | Object of the `orderBy()` key(s) and their values | Yes | The position to resume after. | #### Return type [#return-type-7] | Return type | Example | Description | | ------------ | ------------------------------------------ | ------------------------------------------------ | | `Collection` | `db.orm.public.Post.cursor({ createdAt })` | A collection resuming after the cursor position. | #### Examples [#examples-8] ##### Resume pagination (PostgreSQL) [#resume-pagination-postgresql] ```typescript const page1 = await db.orm.public.Post.orderBy((p) => p.createdAt.asc()).limit(2).all(); const last = page1[page1.length - 1]; const page2 = await db.orm.public.Post.orderBy((p) => p.createdAt.asc()) .cursor({ createdAt: last.createdAt }) .limit(2) .all(); ``` ### `distinct()` [#distinct] Remove duplicate rows, comparing only the fields you name. #### Remarks [#remarks-9] - PostgreSQL only. `distinct()` does not exist on a MongoDB collection. - You do not need `select()`. `distinct('priority')` keeps one whole row per distinct `priority` value, whatever the query returns. - Which of the tied rows it keeps is not defined. Call `orderBy()` first to choose, as [`distinctOn()`](#distincton) does. #### Options [#options-8] | Argument | Type | Required | Description | | ----------- | ---------------------- | -------- | ----------------------------- | | `...fields` | Field names (`string`) | Yes | The fields to deduplicate on. | #### Return type [#return-type-8] | Return type | Example | Description | | ------------ | ----------------------------------------- | ------------------------------------------------------------- | | `Collection` | `db.orm.public.Post.distinct('priority')` | A collection with duplicate rows removed on the named fields. | #### Examples [#examples-9] ##### Deduplicate on a field (PostgreSQL) [#deduplicate-on-a-field-postgresql] ```typescript const priorities = await db.orm.public.Post.distinct('priority').all(); // priorities holds one whole Post row per distinct priority value ``` ### `distinctOn()` [#distincton] Keep the first row per key according to `orderBy()`. #### Remarks [#remarks-10] - PostgreSQL only. `distinctOn()` does not exist on a MongoDB collection. - Call `orderBy()` first, so that "the first row per key" means something. TypeScript rejects the call if you do not. - Start the `orderBy()` with the same columns you pass to `distinctOn()`, as the example does. Sort by anything else first and the query fails in the database. If one of those first sorts is not a plain column, the call throws an error whose `code` is `ORM.ARGUMENT_INVALID`; a sort by a related record or a count may follow them. #### Options [#options-9] | Argument | Type | Required | Description | | ----------- | ---------------------- | -------- | ------------------------------------------ | | `...fields` | Field names (`string`) | Yes | The key field(s) to keep the first row of. | #### Return type [#return-type-9] | Return type | Example | Description | | ------------ | ----------------------------------------- | ------------------------------------- | | `Collection` | `db.orm.public.Post.distinctOn('userId')` | A collection keeping one row per key. | #### Examples [#examples-10] ##### First row per key (PostgreSQL) [#first-row-per-key-postgresql] ```typescript const latestPerUser = await db.orm.public.Post.orderBy([(p) => p.userId.asc(), (p) => p.createdAt.desc()]) .distinctOn('userId') .all(); // latestPerUser holds one post per userId: the newest one, because of the orderBy ``` ### `variant()` [#variant] Narrow a model to one of the variants declared with `@@base`. #### Remarks [#remarks-11] - Available for PostgreSQL and MongoDB. - Pass the variant's discriminator value, the string in its `@@base` attribute, such as `'bug'` for `@@base(Task, "bug")`, not its model name. A value the model does not declare, or a call on a model with no `@@discriminator`, throws [`ORM.ARGUMENT_INVALID`](https://www.prisma.io/docs/orm/reference/error-reference#ORM.ARGUMENT_INVALID). - Call `variant()` once, on the base model. A second `variant()` call on a narrowed collection throws: select each variant from the base model instead. - On PostgreSQL, a variant that sets its own `@@map`, as `Bug` and `Feature` do, is stored in its own table. Read [`createAndCount()`](#createandcount) and [`upsert()`](#upsert) before you write to such a variant. - On MongoDB, every variant is a document in the one collection, told apart by the field named in `@@discriminator`. #### Options [#options-10] | Argument | Type | Required | Description | | -------- | -------- | -------- | --------------------------------------------------------- | | `value` | `string` | Yes | The discriminator value the variant declares in `@@base`. | #### Return type [#return-type-10] | Return type | Example | Description | | ------------------------------------- | ----------------------------------- | ----------------------------------------------- | | `Collection`, narrowed to the variant | `db.orm.public.Task.variant('bug')` | A collection holding only rows of that variant. | #### Examples [#examples-11] ##### Narrow to a variant [#narrow-to-a-variant] #### PostgreSQL ```typescript const bugs = await db.orm.public.Task.variant('bug').all(); ``` #### MongoDB ```typescript const tutorials = await db.orm.posts.variant('tutorial').all(); ``` ### Row locking: `forUpdate()`, `forNoKeyUpdate()`, `forShare()`, `forKeyShare()` [#row-locking] Lock the rows a read selects until the transaction ends. #### Remarks [#remarks-12] - PostgreSQL only. Each method exists only when the contract records that your packages support it, under these keys: `sql.forUpdate`, `postgres.forNoKeyUpdate`, `sql.forShare`, or `postgres.forKeyShare`, and also `sql.lockOf`, because the ORM always locks only the model's own table (`FOR UPDATE OF "job"`). A contract emitted before `8.0.0-rc.17` has none of them: run `prisma contract emit` again to get them. See [what your packages support](https://www.prisma.io/docs/orm/contract-authoring/capabilities). - A lock lasts until the transaction ends, so call these methods on `tx.orm` inside `db.transaction(...)`. - A lock cannot be combined with `include()`, `groupBy()`, `aggregate()`, `distinct()`, `distinctOn()`, or a write method, and cannot be called inside an `include()` refinement. Each of these throws [`ORM.LOCK_INCOMPATIBLE`](https://www.prisma.io/docs/orm/reference/error-reference#ORM.LOCK_INCOMPATIBLE). #### Options [#options-11] | Argument | Type | Required | Description | | ------------ | --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------ | | `nowait` | `boolean` | No | Fail at once when another transaction holds a lock on a selected row. Needs the `sql.lockNowait` key in the contract. | | `skipLocked` | `boolean` | No | Leave out rows another transaction has locked. Needs the `sql.lockSkipLocked` key in the contract. Cannot be combined with `nowait`. | #### Return type [#return-type-11] | Return type | Example | Description | | ------------ | ---------------------------------------------------------- | --------------------------------------------------- | | `Collection` | `tx.orm.public.Job.where({ state: 'queued' }).forUpdate()` | The same collection, which locks the rows it reads. | #### Examples [#examples-12] ##### Take the next queued job (PostgreSQL) [#take-the-next-queued-job-postgresql] This example uses a `Job` model: ```prisma title="src/prisma/contract.prisma (excerpt)" model Job { id Uuid @id @onCreate(uuid()) state String createdAt DateTime @default(now()) @@map("job") } ``` ```typescript await db.transaction(async (tx) => { const job = await tx.orm.public.Job.where({ state: 'queued' }) .orderBy((j) => j.createdAt.asc()) .forUpdate({ skipLocked: true }) .first(); if (job === null) return; await tx.orm.public.Job.where({ id: job.id }).update({ state: 'running' }); }); ``` ## Read methods [#read-terminals] A read method runs the query and gives you the rows. `all()` and `first()` are available on both databases, while `aggregate()` and `groupBy()` are PostgreSQL only and are documented under [Grouped aggregates](#grouped-aggregates). There is no `count()` method on either database, so on PostgreSQL, count with `aggregate((a) => ({ n: a.count() }))`. On MongoDB, counting takes two steps: `db.query`, the [pipeline builder](https://www.prisma.io/docs/orm/fundamentals/advanced-queries#mongodb-pipeline-builder), builds the pipeline, and `runtime.query(...)` runs it. ```typescript const built = db.query.from('posts').count('total').build(); const runtime = await db.runtime(); const [counted] = await runtime.query(built); // counted.total is the count ``` For a task-oriented walkthrough, see [Reading data](https://www.prisma.io/docs/orm/fundamentals/reading-data). ### `all()` [#all] Resolve the query to every matching row. #### Remarks [#remarks-13] - Available for PostgreSQL and MongoDB. - `all()` returns an [`AsyncIterableResult`](#asynciterableresult): you can `await` it to collect an array, or use `for await` to stream rows one at a time. - `await`ing a result you have already `await`ed is safe. You get the same array back, and the query does not run again. Each result can be used one way only, so awaiting it and then looping it with `for await`, or the reverse, throws an error whose `code` is `RUNTIME.ITERATOR_CONSUMED`. See [Single consumption and mode switching](#single-consumption-and-mode-switching). - `orderBy()` is written differently on each database. See [`orderBy()`](#orderby). #### Options [#options-12] `all()` takes no required arguments. #### Return type [#return-type-12] | Return type | Example | Description | | -------------------------- | -------------------------------- | ----------------------------------------------------------------- | | `AsyncIterableResult` | `await db.orm.public.User.all()` | Awaitable to `Row[]`, or iterable with `for await` for streaming. | `Row` is the model's row type, from the emitted `contract.d.ts`. #### Examples [#examples-13] ##### Await to collect an array [#await-to-collect-an-array] #### PostgreSQL ```typescript const users = await db.orm.public.User.all(); ``` #### MongoDB ```typescript const users = await db.orm.users.all(); ``` ##### Stream rows one at a time [#stream-rows-one-at-a-time] #### PostgreSQL ```typescript for await (const user of db.orm.public.User.orderBy((u) => u.email.asc()).all()) { console.log(user.email); } ``` #### MongoDB ```typescript for await (const post of db.orm.posts.orderBy({ createdAt: 1 }).all()) { // 1 ascending, -1 descending console.log(post.title); } ``` For Prisma ORM 7 users, `findMany` maps onto `all()`: ```diff - const users = await prisma.user.findMany({ where: { kind: 'admin' } }); + const users = await db.orm.public.User.where({ kind: 'admin' }).all(); ``` ### `first()` [#first] Resolve the query to the first matching row, or `null` if none matches. #### Remarks [#remarks-14] - Available for PostgreSQL and MongoDB. - On PostgreSQL, `first()` accepts an inline filter: a shorthand object or a callback (`first((p) => p.priority.eq('urgent'))`). An inline filter is added to any earlier `where()` call, and both have to match. - On MongoDB, `first()` takes no filter argument. Filter with `where(...)` first, then call `first()`. #### Options [#options-13] | Name | Type | Required | Description | | -------- | ---------------------------------------------- | -------- | ------------------------------------------ | | `filter` | Shorthand object or callback (PostgreSQL only) | No | An inline filter applied before resolving. | #### Return type [#return-type-13] | Return type | Example | Description | | ------------- | ------------------------------------- | ---------------------------------- | | `Row \| null` | `await db.orm.public.User.first(...)` | The first matching row, or `null`. | #### Examples [#examples-14] ##### Match by an inline filter (PostgreSQL) [#match-by-an-inline-filter-postgresql] ```typescript const alice = await db.orm.public.User.first({ email: 'alice@example.com' }); const urgentPost = await db.orm.public.Post.first((p) => p.priority.eq('urgent')); ``` ##### Match with a prior `where()` (MongoDB) [#match-with-a-prior-where-mongodb] ```typescript const bob = await db.orm.users.where({ name: 'Bob' }).first(); ``` For Prisma ORM 7 users, `findUnique` and `findFirst` map onto `first()`, and their `OrThrow` forms onto [`firstOrThrow()`](#firstorthrow): ```diff - const alice = await prisma.user.findUnique({ where: { email } }); + const alice = await db.orm.public.User.whereUnique({ email }).first(); - const alice = await prisma.user.findUniqueOrThrow({ where: { email } }); + const alice = await db.orm.public.User.whereUnique({ email }).firstOrThrow(); ``` `first()` returns `null` on no match and does not check that only one row matched. [`whereUnique()`](#whereunique) checks at compile time that the criterion names one row. ### `firstOrThrow()` [#firstorthrow] Resolve the query to the first matching row, or throw when none matches. #### Remarks [#remarks-15] - Available for PostgreSQL and MongoDB. - It runs the query `first()` runs, with `LIMIT 1`, and returns the row type without `null`. When nothing matches, it throws an error whose `code` is `RUNTIME.NO_ROWS`. - On PostgreSQL it takes the same inline filter as `first()`, and `.prepared.firstOrThrow()` is its prepared form. On MongoDB it takes no argument. - The result of `all()` also has a `firstOrThrow()` method. That one reads every matching row into memory first, so prefer the collection method. #### Examples [#examples-15] ```typescript const user = await db.orm.public.User.firstOrThrow({ id }); const doc = await db.orm.users.where({ email }).firstOrThrow(); ``` ### Custom `Collection` subclass [#custom-collection-subclass] On PostgreSQL, SQLite, and MySQL you can subclass `Collection` to add your own methods to a model, and pass the subclass to the client in the `collections` option. #### Remarks [#remarks-16] - PostgreSQL, SQLite, and MySQL. You cannot subclass a collection on MongoDB. - `collections` maps a model name to your class. You pass it to the function that creates your client: `postgres(...)`, `postgresServerless(...)` for serverless functions, `sqlite(...)` for SQLite, `mysql(...)` for MySQL, or `supabase(...)` for Supabase. Your class is then used wherever that client gives you the model: on `db.orm`, on every transaction's `tx.orm`, on each connection the serverless client opens, and on the Supabase clients that run queries as a database role, such as the one `asUser(jwt)` returns. Models you leave out keep the built-in class. - If the module that defines your classes needs the contract type, import only the type, `import type { Contract } from './contract.d'`, so that it does not import the module that creates the client. - `Collection` takes two type arguments: your contract type, and the name of the model this collection is for. - A method that returns `this.variant('bug')` gives back a collection narrowed to the `Bug` model. It is chainable, so you finish it with `all()`, `first()`, or any other method. - Your methods stay available after `where()`, `orderBy()`, `limit()`, `offset()`, `include()`, `distinct()`, `distinctOn()`, and `cursor()`, because each of them returns the collection's own class. `db.orm.public.Task.where({ title }).bugs()` compiles. - To override one of those chaining methods, or a method that returns rows, give the override a type parameter for the receiver and a `this` parameter of that type, and call the base method with `call`: `override limit(this: Self, n: number): Self { return super.limit.call(this, Math.min(n, 100)); }`. - `with`, `fragment`, and `using` are members of every collection. A member of your own named `with` or `fragment` with another signature does not compile, and neither does a member of your own named `using`. See [Query fragments](#query-fragments) and [Package methods with `using`](#package-methods-with-using). - The type `PostgresClient` describes a client without classes, and a client with classes fits it too. To name a client with classes, pass them as the second type argument: `PostgresClient`. #### Examples [#examples-16] ##### Register a subclass with a domain method (PostgreSQL) [#register-a-subclass-with-a-domain-method-postgresql] ```typescript import postgres from '@prisma/orm-postgres/runtime'; import { Collection } from '@prisma/orm-postgres/orm-client'; import { contractFromJson } from '@prisma/orm-postgres/static'; import type { Contract } from './contract.d'; import contractJson from './contract.json' with { type: 'json' }; class TaskCollection extends Collection { bugs() { return this.variant('bug'); } features() { return this.variant('feature'); } } const db = postgres({ contract: contractFromJson(contractJson), url: process.env.DATABASE_URL!, collections: { Task: TaskCollection }, }); const bugs = await db.orm.public.Task.bugs().all(); const features = await db.orm.public.Task.features().all(); const recentBugs = await db.orm.public.Task.orderBy((t) => t.id.desc()).limit(10).bugs().all(); const lockedBugs = await db.transaction((tx) => tx.orm.public.Task.bugs().forUpdate().all()); ``` The last line runs inside a transaction, so your method works on `tx.orm` too. `forUpdate()` locks the rows it reads with `FOR UPDATE` until the transaction ends. On SQLite and MySQL, `db.orm` holds the models directly, so the same method is `db.orm.Task.bugs()`. ### Package methods with `using` [#package-methods-with-using] Packages add methods to a model's collection under its `using` member. Which methods a model has depends on its fields and indexes. #### Remarks [#remarks-17] - PostgreSQL. Each method returns a collection, so you chain it with `where()`, `orderBy()`, `limit()`, and the other methods, and finish with `all()` or `first()`. - `@prisma/orm-postgres` adds one method for each [full-text index](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#full-text-indexes), named after the index. It takes a search query, the same kind that [`fullTextMatches()`](#full-text-search) takes, keeps the rows that match it, and orders them by relevance, best match first. - `@prisma/orm-extension-pgvector` adds one method for each vector field, named `_nearest`. It takes a vector and orders the rows by cosine distance to it, nearest first. Pass a maximum distance as the second argument to keep only the rows within it. Without one, rows whose field is null come back last. The method exists only when the client has pgvector in `extensions`. - `using` is a member of every collection, so your own [collection subclass](#custom-collection-subclass) cannot have a member named `using`. #### Examples [#examples-17] ##### Search through a full-text index [#search-through-a-full-text-index] ```prisma title="src/prisma/contract.prisma (excerpt)" model Post { id Uuid @id @onCreate(uuid()) title String @@fullTextIndex([title], name: "post_search") @@map("post") } ``` ```typescript import { websearchToTsquery } from '@prisma/orm-postgres/target/full-text'; const posts = await db.orm.public.Post.using.post_search(websearchToTsquery('prisma orm')).limit(10).all(); ``` ##### Find the nearest vectors [#find-the-nearest-vectors] ```typescript const similar = await db.orm.public.Post.using.embedding_nearest(queryEmbedding, 0.3).limit(5).all(); ``` ### Query fragments [#query-fragments] A query fragment is a function from a collection to a collection, a piece of a query you share between places. `with(fragment)` runs one on a collection and returns its result: `db.orm.public.Post.with((posts) => posts.where({ userId }))` has the same type as `db.orm.public.Post.where({ userId })`. A fragment that only filters, such as a soft-delete filter, is called a scope. Two helpers make the fragments TypeScript cannot type on its own, and a third turns a sort field from a request into an order. #### Remarks [#remarks-18] - PostgreSQL, SQLite, and MySQL. On SQLite and MySQL, `db.orm` holds the models directly, so a fragment for one model is `db.orm.Post.fragment(body)`. - `db.orm.fragment(fields, body)` makes a fragment for every model that has the given fields. Declare each field as `field.column(type.X(...))`, with `field` and `type` from `@prisma/orm-postgres/contract-builder`. The body sees only those fields and may call `where()`, `orderBy()`, `limit()`, and `offset()`. On a model that lacks one of the fields, or has it with another type or nullability, `with` does not compile. - `db.orm.public.Post.fragment(body)` makes a fragment for one model. The body receives the plain collection of that model and may call any query-building method, such as `select()` and `include()`. Call it on the model's collection itself: on a collection with chained calls, such as `db.orm.public.Post.where(...).fragment(body)`, it throws an error whose `code` is `ORM.ARGUMENT_INVALID`, and a `where()`, `orderBy()` or `include()` before it is a compile error. Put those calls in the body instead. - A fragment made by `db.orm.fragment(fields, body)` that filters returns `Filtered`, the receiver's type with its filter recorded, so `update()` and `delete()` compile after it. One that orders returns `Ordered`, so `cursor()` compiles after it. A fragment made by `collection.fragment(body)` returns the type its body returns. A filter or order on the collection before `with()` is not recorded in that type, so `update()`, `delete()` or `cursor()` after `with()` compiles only when the body itself filters or orders. - `CollectionRowOf` is the row type a collection returns, including the relations `include()` adds. `CollectionTypeStateOf` is what the collection's type records, such as `hasWhere` and `hasOrderBy`. The types of fragments are `QueryFragment`, for a fragment run with `collection.with(...)`, `DeclaredFieldsFragment`, for what `db.orm.fragment(fields, body)` returns, and `FragmentFacts`. Import these types, `Filtered`, and `Ordered` from `@prisma/orm-postgres/orm-client`. - `orderByField(collection, name, direction, allowed)` returns an `orderBy()` selector for a field named in a request. `direction` is `'asc'` or `'desc'`, and `undefined` means `'asc'`. `allowed` lists the fields a request may sort by. It is required, because sorting by any field lets a request learn the value of a secret one from the order of the results. Any other `name` or `direction`, including one that is not a string, throws an error whose `code` is `ORM.ARGUMENT_INVALID` before the query runs. - A fragment that needs a value is a function that returns a fragment, as `ownedBy` below shows. A scope for every model with a `deletedAt` field is how you share a soft-delete filter. - If your contract has a namespace named `fragment`, `db.orm.fragment` is that namespace, and the client has no method to make a fragment for every model with given fields. Use `collection.fragment(body)` on each model instead. #### Examples [#examples-18] ##### A filter for every model with a `userId` field (PostgreSQL) [#a-filter-for-every-model-with-a-userid-field-postgresql] ```typescript title="src/scopes.ts" import { field, type } from '@prisma/orm-postgres/contract-builder'; const ownedBy = (userId: string) => db.orm.fragment({ userId: field.column(type.Uuid()) }, (rows) => rows.where((r) => r.userId.eq(userId))); const posts = await db.orm.public.Post.with(ownedBy(userId)).all(); const tasks = await db.orm.public.Task.with(ownedBy(userId)).all(); await db.orm.public.Tag.with(ownedBy(userId)).all(); // type error: Tag has no userId ``` ##### A shared selection for one model (PostgreSQL) [#a-shared-selection-for-one-model-postgresql] ```typescript title="src/fragments.ts" import type { CollectionRowOf } from '@prisma/orm-postgres/orm-client'; const summary = db.orm.public.Post.fragment((posts) => posts.select('id', 'title').include('user')); type PostSummary = CollectionRowOf>; const mine = await db.orm.public.Post.where({ userId }).with(summary).all(); ``` ##### Sort by a field from a request (PostgreSQL) [#sort-by-a-field-from-a-request-postgresql] ```typescript title="src/routes/posts.ts" import { orderByField } from '@prisma/orm-postgres/orm-client'; const posts = await db.orm.public.Post .orderBy(orderByField(db.orm.public.Post, input.orderBy, input.direction, ['title', 'createdAt'])) .all(); ``` ## Write methods [#mutation-terminals] A write method changes the database. `create`, `createAll`, `createAndCount`, `update`, `updateAll`, `updateAndCount`, `delete`, `deleteAll`, `deleteAndCount`, and `upsert` are available on both databases, and the differences between the two are listed under each method. For Prisma ORM 7 users: `createMany` is `createAndCount()`, which returns the count as a number instead of `{ count }`, and `createManyAndReturn` is `createAll()`. In the same way, `updateMany` is `updateAndCount()` and `updateManyAndReturn` is `updateAll()`. `deleteMany` is `deleteAndCount()`, and `deleteAll()` returns the deleted rows, which no Prisma ORM 7 method did. `count` is [`aggregate()`](#grouped-aggregates) on PostgreSQL, or the two steps shown under [Read methods](#read-terminals) on MongoDB. `skipDuplicates: true` is now `{ onConflict: 'skip' }`, which you pass to [`createAll()`](#createall) or [`createAndCount()`](#createandcount) on PostgreSQL as a second argument. To catch one of the errors named below, wrap the call in `try` and `catch`, then compare `error.code` with the string given here. For a task-oriented walkthrough, see [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data). On MongoDB, a write method rejects a chain that already has `orderBy()`, `limit()`, or `offset()` on it, and every write except `updateAll()` and `deleteAll()` also rejects `include()`. Both throw an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. For grouping several writes into one unit, see [Transactions](https://www.prisma.io/docs/orm/fundamentals/transactions). > [!WARNING] > Filter before you update or delete > > Always call `where()` before an update or a delete. On MongoDB the six update and delete methods and `upsert()` check for a filter and throw an error whose `code` is `ORM.WHERE_MISSING`, with a message naming the method you called, such as `updateAll() requires a .where() filter. Call .where() before .updateAll()`. On PostgreSQL, these methods do not compile without a `where()`, and neither do they on a collection filtered on some code paths only, such as `search ? db.orm.public.Post.where({ title: search }) : db.orm.public.Post`. Nothing checks for the filter at run time. If a call without a filter does reach the database, `update()` and `delete()` change a single row and you cannot predict which one, and `updateAll()` and `deleteAll()` change every row. The examples below use `aliceId`, `bobId`, `carolId`, `postId`, `tagId`, `tutorialId`, and `userId` for the ids of rows that already exist. Substitute your own. ### `create()` [#create] Insert a single row and return it. #### Remarks [#remarks-19] - Available on every database. - On PostgreSQL, you may leave out any field your contract gives a default or generates a value for on create, including the id. Passing an explicit id is accepted. Nullable fields can be left out too. Every other field is required, including a field that generates a value only on update, such as one with `@onUpdate(...)` and no `@onCreate(...)`. The same rule applies to the `create` side of `upsert()`. - On MongoDB, you may leave out `id`, the field stored as `_id`, any field your contract generates a value for on create, such as `temporal.CreatedAt`, and any optional field, which then reads back as `null`. Passing `null` for an optional field, as the example below does, works too. Give every other field a value. - On MongoDB, `create()` returns the document as it is stored, with the `id` the server assigned, and converts its values the same way a read does. It does not query the database again to do this. On PostgreSQL the returned row is read from the database already. - On MongoDB, `create()` fails with an error whose `code` is `RUNTIME.ENCODE_FAILED`, before anything is written, when a value does not fit its field: a value of the wrong type, a fraction or an out-of-range number for an `Int32` field, a value that is not one of the field's enum values, or `null` for a field that is not optional. The error names the field. - On PostgreSQL, SQLite, MySQL and MariaDB, `create()` can create or link related rows in the same transaction. Nest `create()` on the side that does not hold the foreign key, and `connect()` on the side that does. - Since `8.0.0-rc.25`, a nested `create()` takes `{ onConflict, conflictOn? }` as its second argument, for a related row that may already exist: - `onConflict: 'connect'` inserts the related row when it collides with no existing row, and links the existing row when it does. It works on every kind of relation on PostgreSQL and SQLite. MySQL and MariaDB check every unique key of the table at once, so there `'connect'` works only when the key it checks is the related model's only unique key, apart from an auto-increment id the row leaves out. Otherwise it throws an error whose `code` is `ORM.CAPABILITY_MISSING`. On a many-to-many relation it does not work on MySQL or MariaDB yet: it throws an error whose `code` is `RUNTIME.AST_UNSUPPORTED`. - `onConflict: 'skip'` leaves a colliding row as it is. It works on one-to-many relations, on PostgreSQL and SQLite. On MySQL and MariaDB it throws an error whose `code` is `ORM.CAPABILITY_MISSING`. - `conflictOn` names the fields of the unique key to check, as in [`createAll()`](#createall). Without it, `'connect'` checks only the primary key, so to link an existing user by email, pass `{ onConflict: 'connect', conflictOn: ['email'] }`. `'skip'` without it checks every unique key. - Each related row must give a non-null value for every field of a key the option checks, in the row or generated by the ORM, such as `uuid()`. A value the database fills in, such as an auto-increment id, does not count, so a row without such a value throws an error whose `code` is `ORM.ARGUMENT_INVALID` before anything is inserted. With `'connect'` on a model whose primary key is an auto-increment id, give the id in the row or name another unique key in `conflictOn`. - `onConflict` does not work on a variant stored in its own table, such as `Bug`. Passing it there throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. Create such a row without `onConflict`, and a row that breaks a unique constraint then makes the call fail. - When the related model is a polymorphic base model, a nested `create()` names its variant: `tasks.variant('bug').create({ title: 'Crash on login' })`. Leave the discriminator field out of the data. `variant(value)` in a relation callback also has `connect`, `disconnect`, `where`, `updateAll` and `deleteAll`, each limited to rows of that variant. Before `8.0.0-rc.25`, you passed the discriminator in the data of `create()`, which no longer compiles. - Nested `create()` and `connect()` are not available on MongoDB. #### Options [#options-14] | Name | Type | Required | Description | | ---------------- | ---------------------- | -------- | ------------------------------------------------------------------------------------ | | The row's fields | Object of field values | Yes | The row to insert. Pass the object as the only argument. There is no `data` wrapper. | On PostgreSQL, SQLite, MySQL and MariaDB, that object can also carry a `create` or `connect` callback for a related row, as shown below. `create()` and `connect()` each take one object or an array of objects, on any relation. On a many-to-many relation, `connect()` does not work on MySQL or MariaDB yet: it throws an error whose `code` is `RUNTIME.AST_UNSUPPORTED`. `disconnect()` takes an array, and on a many-to-many relation the array is required. #### Return type [#return-type-14] | Return type | Example | Description | | ----------- | ------------------------------------- | ----------------- | | `Row` | `await db.orm.public.Tag.create(...)` | The inserted row. | #### Examples [#examples-19] ##### Insert a single row [#insert-a-single-row] #### PostgreSQL ```typescript const tag = await db.orm.public.Tag.create({ label: 'typescript-2' }); ``` #### MongoDB ```typescript const user = await db.orm.users.create({ name: 'Carol', email: 'carol@example.com', bio: null, role: 'reader', address: null, }); // user.id is the server-assigned id ``` ##### Nested `create()` on the side without the foreign key (PostgreSQL) [#nested-create-on-a-child-owned-relation-postgresql] ```typescript const author = await db.orm.public.User.create({ email: 'dana@example.com', displayName: 'Dana', kind: 'user', posts: (posts) => posts.create([{ title: 'Dana post one' }]), }); ``` ##### Nested `connect()` on the side with the foreign key (PostgreSQL) [#nested-connect-on-a-parent-owned-relation-postgresql] ```typescript const post = await db.orm.public.Post.create({ title: 'Connected to Bob', user: (user) => user.connect({ id: bobId }), }); ``` For Prisma ORM 7 users, `create()` drops the `data` wrapper: ```diff - const tag = await prisma.tag.create({ data: { label: 'typescript-2' } }); + const tag = await db.orm.public.Tag.create({ label: 'typescript-2' }); ``` ### `createAll()` [#createall] Insert multiple rows and return them. #### Remarks [#remarks-20] - Available for PostgreSQL and MongoDB. - Returns an [`AsyncIterableResult`](#asynciterableresult): `await` for an array, or `for await` to stream inserted rows. - On PostgreSQL every row is inserted by one statement before the first row reaches you. Stopping the loop early does not undo an insert. - To skip each row that would break a unique constraint, instead of failing the whole insert, pass `{ onConflict: 'skip' }` as the second argument. The result then holds only the rows the database wrote. This replaces `skipDuplicates: true` from Prisma ORM 7. - `onConflict` works on PostgreSQL, and on SQLite through the experimental `@prisma/orm-sqlite` package. On MongoDB, `createAll()` takes only the rows, so a second argument is a type error. - By default, a row is skipped when it breaks any unique constraint. To check only one constraint, list its fields in `conflictOn`. In the example schema, `Tag.label` is unique, so `conflictOn: ['label']` skips a tag whose label already exists. A row that breaks a different unique constraint is then not skipped: the database rejects it, and the whole insert fails, so no row is written. - `onConflict` does not work on a variant stored in its own table, such as `Bug`, which maps to `bug` while its base model `Task` maps to `task`. Passing it there throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. For such a variant, call `createAll()` with only the rows. A row that breaks a unique constraint then makes the call fail instead of being skipped. - If your contract was emitted before `8.0.0-rc.12`, run `npx prisma contract emit` again before you use `onConflict`. Otherwise the call throws an error whose `code` is `ORM.CAPABILITY_MISSING`. - With `onConflict: 'skip'`, every row must be able to collide with an existing row. A row can collide when it has a non-null value for every field of at least one key the option checks, given in the row or generated by the ORM, such as `uuid()`. A value the database fills in, such as an auto-increment id, does not count. A row that cannot collide throws an error whose `code` is `ORM.ARGUMENT_INVALID` before any statement runs, and so does a table with no primary key or unique key when you leave out `conflictOn`. Insert such rows with a separate call without `onConflict`. Before `8.0.0-rc.25`, they were inserted. `createAndCount()` behaves the same. #### Options [#options-15] | Name | Type | Required | Description | | --------------- | ------------------------------------- | -------- | ------------------------------------------------------------------- | | First argument | Array of row objects | Yes | The rows to insert. There is no `data` wrapper. | | Second argument | `{ onConflict: 'skip', conflictOn? }` | No | Skip rows that break a unique constraint. Not available on MongoDB. | `conflictOn` is an array of field names, such as `['label']`. [`upsert()`](#upsert) also takes a `conflictOn`, but there it is an object of field values, such as `{ label: 'typescript' }`. #### Return type [#return-type-15] | Return type | Example | Description | | -------------------------- | ------------------------------------------ | ------------------------------------ | | `AsyncIterableResult` | `await db.orm.public.Tag.createAll([...])` | Awaitable to `Row[]`, or streamable. | #### Examples [#examples-20] ##### Insert and collect [#insert-and-collect] #### PostgreSQL ```typescript const created = await db.orm.public.Tag.createAll([{ label: 'alpha' }, { label: 'beta' }]); // Skip rows whose label already exists instead of failing. const added = await db.orm.public.Tag.createAll([{ label: 'alpha' }, { label: 'gamma' }], { onConflict: 'skip', conflictOn: ['label'], }); // added holds only the gamma row ``` #### MongoDB ```typescript const created = await db.orm.users.createAll([ { name: 'Dana', email: 'dana@example.com', bio: null, role: 'author', address: null }, { name: 'Eve', email: 'eve@example.com', bio: null, role: 'reader', address: null }, ]); ``` ### `createAndCount()` [#createandcount] Insert rows and return how many were inserted, without reading them back. #### Remarks [#remarks-21] - Available for PostgreSQL and MongoDB. - On PostgreSQL, `createAndCount()` does not work on a variant that is stored in its own table, meaning its `@@map` names a different table than its base model does. `Bug` in the [example schema](#example-schema) is one: it maps to `bug` while `Task` maps to `task`. Use `createAll()` instead. - Calling it on such a variant throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`, with the message `createAndCount() is not supported for MTI variant "Bug" on model "Task". Use createAll() instead.` MTI in the message means multi-table inheritance, the variant stored in its own table described above. - On MongoDB, a variant is one document in one collection, so `createAndCount()` works on a variant as usual. - It returns the number of rows the database inserted. With `{ onConflict: 'skip' }`, the skipped rows are not counted. The option works as on [`createAll()`](#createall). #### Options [#options-16] | Name | Type | Required | Description | | --------------- | ------------------------------------- | -------- | ------------------------------------------------------------------- | | First argument | Array of row objects | Yes | The rows to insert. There is no `data` wrapper. | | Second argument | `{ onConflict: 'skip', conflictOn? }` | No | Skip rows that break a unique constraint. Not available on MongoDB. | `conflictOn` is an array of field names, as on [`createAll()`](#createall). #### Return type [#return-type-16] | Return type | Example | Description | | ----------- | ----------------------------------------------- | --------------------------- | | `number` | `await db.orm.public.Tag.createAndCount([...])` | The count of inserted rows. | #### Examples [#examples-21] ##### Insert and count [#insert-and-count] #### PostgreSQL ```typescript const inserted = await db.orm.public.Tag.createAndCount([{ label: 'epsilon' }, { label: 'zeta' }]); // 2 ``` #### MongoDB ```typescript const inserted = await db.orm.posts.variant('tutorial').createAndCount([ { title: 'Variant createAndCount', content: 'body', authorId: aliceId, createdAt: new Date('2024-02-01T00:00:00.000Z'), difficulty: 'beginner', duration: 10, }, ]); // 1 ``` ### `update()` [#update] Update the matched row and return it, or `null` if none matches. #### Remarks [#remarks-22] - Available on every database. Requires a prior `where()` (see the warning at the top of this section). - Relation callbacks, which change related rows in the same call, work on PostgreSQL, SQLite, MySQL and MariaDB, but not on MongoDB. [Related rows](#update-related-rows) lists them. - On MongoDB, `update()` takes an object of field values, or a callback that returns field operations, such as `(p) => [p.content.set('Rewritten')]`. See [Field update operations](#field-update-operations). - On MongoDB, the update input covers the base model's fields only. Putting a field declared on a variant in the update, such as `duration` on `Tutorial`, makes TypeScript complain even though the update runs correctly. Put `// @ts-expect-error` on the line directly above the line TypeScript flags. When the complaint goes away, TypeScript reports the comment itself as unused, so delete it then. The same is true of `updateAll()` and `updateAndCount()`. ```typescript // @ts-expect-error duration is a Tutorial field, not a Post field .update((t) => [t.duration.inc(10)]); ``` - Take care on PostgreSQL: `update()` takes an object of field values only, and passing a function instead writes nothing, reports nothing, and returns `null`. - `update()` and `delete()` find the row by its primary key, or else by a unique key whose columns are all required. A unique key with a nullable column does not count, because two rows can both hold `NULL` in it. On a table with no other key, `update()`, `delete()`, and a write followed by `.include(...)` throw an error whose `code` is `ORM.ROW_IDENTITY_MISSING`. Give the table a primary key, or make the key's fields required. This rule applies since `8.0.0-rc.25`. - The discriminator field of a polymorphic model cannot be updated: setting it is a type error, and throws an error whose `code` is `ORM.FIELD_IMMUTABLE`. - On PostgreSQL, `update()` changes the row `first()` would return, so `orderBy()`, `offset()`, `cursor()`, `distinct()`, and `distinctOn()` choose it. After `limit(0)` it changes nothing and returns `null`. - Returns `null` when no row matches. On PostgreSQL it also returns `null` when the object you pass is empty and has no keys at all. An object whose values already match the row is different: that update runs and returns the row. - On PostgreSQL, [`select()`](#select) and [`include()`](#include) before `.update()` choose which fields and relations come back on the returned row. They change nothing about what is written. #### Related rows [#update-related-rows] On PostgreSQL, SQLite, MySQL and MariaDB, the object you pass to `update()` can hold a callback for a relation field, such as `posts: (p) => ...`. The callback returns an operation on the related rows: | Operation | Relation | What it changes | | --------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------- | | `create({ ... })` | any | Creates a related row linked to this one. | | `connect(criteria)` | any | Points the foreign key at another row. On a many-to-many relation, adds a link. | | `disconnect()`, `disconnect([criteria])` | any | Sets the foreign key to `NULL`. On a many-to-many relation, see the note below the table. | | `where(...).updateAll({ ... })`, `where(...).deleteAll()` | to-many | Every related row the filter matches. | | `where(...).update({ ... })`, `where(...).delete()` | to-many | The first related row the filter matches. | | `update({ ... })`, `delete()` | to-one | The linked row, narrowed by `where(...)` if you call it. | On a many-to-many relation, `disconnect([criteria])` removes the links to the rows that match the criteria and keeps the rows themselves. A bare `disconnect()` there is a type error, and throws an error whose `code` is `ORM.ARGUMENT_INVALID`, because it does not say which links to remove. `updateAll()`, `deleteAll()`, `update()` and `delete()` change only rows that are already related to the row you are updating. `update()` and `delete()` of one related row are available since `8.0.0-rc.25`. `connect(criteria)` works differently: it links whichever rows match the criteria, so `posts: (p) => p.connect({ id: postId })` moves that post to this user even when it belongs to another user. `where(...)` takes everything the model's own `where()` takes, and chained `where` calls are combined with AND. `connect()` and `disconnect()`: - On a relation whose foreign key is required, `disconnect()` is a type error. At runtime it throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED` with `meta.reason` `'required-foreign-key'` before any statement runs. Use `connect()` to another row, or delete the row. - On a many-to-one relation, such as `Post.user`, `disconnect([criteria])` unlinks the row only when the linked row matches the criteria, and `disconnect([])` disconnects nothing. Before `8.0.0-rc.25`, the criteria were ignored on a many-to-one relation, and an empty list acted like `disconnect()`. - On a many-to-many relation, `connect()` does not work on MySQL or MariaDB yet: it throws an error whose `code` is `RUNTIME.AST_UNSUPPORTED`. - An `update()` that holds a relation callback cannot follow `orderBy()`, `limit()`, `offset()`, `cursor()`, `distinct()`, or `distinctOn()`. It throws an error whose `code` is `ORM.ARGUMENT_INVALID`. `updateAll()` and `deleteAll()`: - They need a to-many relation. On a to-one relation, such as `Post.user`, they throw an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. - On a many-to-many relation, the related rows are the ones linked to the row through the join table. There, `deleteAll()` deletes the related rows themselves, not only the links, so a tag that another post also uses is gone for that post too. - The rows in the join table itself, which hold the links, are not deleted by `deleteAll()`: the delete action of the join table's foreign key decides what happens to them. A key that cascades removes them. With a key that has no delete action or restricts the delete, the database rejects the delete and the whole update is rolled back. `update()` and `delete()` of one related row: - On a to-many relation, they need `where(...)` first. Without it they are a type error, and throw an error whose `code` is `ORM.ARGUMENT_INVALID`. - When no related row matches, nothing changes and nothing is thrown. - On a many-to-one relation, such as `Post.user`, `delete()` deletes the linked row. For `user: (u) => u.delete()` on a post, it sets the post's `userId` to `NULL` and then deletes that user. When that foreign key is required, `delete()` is a type error, and throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED` with `meta.reason` `'required-foreign-key'`. Rules for every relation callback: - In `update()`, set a foreign key of the row, such as `userId`, or pass a callback on its relation, such as `user`, but not both. `update()` data that does both throws an error whose `code` is `ORM.ARGUMENT_INVALID` before any statement runs. Before `8.0.0-rc.25` both ran, and a `create`, `connect` or `disconnect` callback overwrote the foreign key. - A callback in `create()` or `update()` can return an array of operations, which run in the order you list them. The example under "Update and delete filtered related rows" below returns two. - A relation callback in `create()` data takes only `create` and `connect`. The other operations in the table throw an error whose `code` is `ORM.OPERATION_UNSUPPORTED` there. - A to-one relation, such as `Post.user`, takes at most one operation. An array of two or more throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED` before any statement runs. - An empty array changes nothing. An array inside the array, or an element that is not an operation, throws an error whose `code` is `ORM.ARGUMENT_INVALID`. #### Options [#options-17] | Name | Type | Required | Description | | ----------- | ----------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------------------- | | The changes | Object of field values, or on MongoDB a field-operations callback | Yes | What to set on the matched row, passed as the only argument. There is no `data` wrapper. | On PostgreSQL, SQLite, MySQL and MariaDB, that object can also hold relation callbacks, as [Related rows](#update-related-rows) shows. #### Return type [#return-type-17] | Return type | Example | Description | | ------------- | ------------------------------------------------- | ------------------------------------------- | | `Row \| null` | `await db.orm.public.User.where(...).update(...)` | The updated row, or `null` if none matched. | #### Examples [#examples-22] ##### Update with a data object [#update-with-a-data-object] #### PostgreSQL ```typescript const updated = await db.orm.public.User.where({ id: bobId }).update({ displayName: 'Bob Updated' }); ``` #### MongoDB ```typescript const updated = await db.orm.users.where({ id: bobId }).update({ bio: 'Now with a bio' }); ``` ##### Field-operations callback (MongoDB) [#field-operations-callback-mongodb] ```typescript const updated = await db.orm.posts .where({ id: postId }) .update((p) => [p.title.set('Updated title'), p.content.set('Rewritten')]); ``` ##### Nested `connect()` relinks a foreign key (PostgreSQL) [#nested-connect-relinks-a-foreign-key-postgresql] ```typescript const relinked = await db.orm.public.Post.where({ id: postId }).update({ user: (user) => user.connect({ id: carolId }), }); ``` ##### Update and delete filtered related rows (PostgreSQL) [#update-and-delete-filtered-related-rows-postgresql] ```typescript const user = await db.orm.public.User.where({ id: userId }).update({ posts: (p) => [ p.where({ priority: 'low' }).updateAll({ priority: 'high' }), p.where((post) => post.title.like('Draft%')).deleteAll(), ], }); ``` ##### Update one related row (PostgreSQL) [#update-one-related-row-postgresql] ```typescript const user = await db.orm.public.User.where({ id: userId }).update({ posts: (p) => p.where({ id: postId }).update({ priority: 'high' }), }); ``` ##### Nested `disconnect()` unlinks a many-to-many row (PostgreSQL) [#nested-disconnect-unlinks-a-many-to-many-row-postgresql] ```typescript const updated = await db.orm.public.Post.where({ id: postId }) .select('id', 'title') .include('tags', (tag) => tag.select('id', 'label').orderBy((t) => t.label.asc())) .update({ tags: (tag) => tag.disconnect([{ id: tagId }]), }); // only the join-table row is removed. The Tag row itself still exists ``` For Prisma ORM 7 users, `update()` moves the filter into `where()` and drops the `data` wrapper: ```diff - const updated = await prisma.user.update({ where: { id: bobId }, data: { displayName: 'Bob Updated' } }); + const updated = await db.orm.public.User.where({ id: bobId }).update({ displayName: 'Bob Updated' }); ``` ### `updateAll()` [#updateall] Update every matching row and collect the results. #### Remarks [#remarks-23] - Available for PostgreSQL and MongoDB. Call `where()` first. Without a filter this changes every row. See the warning at the top of this section. - On PostgreSQL, `updateAll()` runs as one statement, so every matching row changes together. - On PostgreSQL, a `limit()`, `offset()`, `cursor()`, `distinct()`, or `distinctOn()` on the collection makes this method throw an error whose `code` is `ORM.ARGUMENT_INVALID`, because it changes every matching row and cannot apply them. An `orderBy()` is accepted. To change only some of the matching rows, read their ids first, then filter on them, as [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data#update-or-delete-a-limited-number-of-rows) shows. - On MongoDB, `updateAll()` is not a single operation, and two things follow. If a document begins matching your filter while the call is running, it is updated but not included in the returned rows. If someone else changes a matched document while the call is running, you get their version back, not yours. - Prisma ORM has no `db.transaction()` on MongoDB, so you cannot avoid this from the client. To control it, share one `MongoClient` with the MongoDB driver and group the writes in one of the driver's own sessions, shown under [Transactions on MongoDB](https://www.prisma.io/docs/orm/fundamentals/transactions#transactions-on-mongodb). #### Options [#options-18] | Name | Type | Required | Description | | ----------- | ----------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------ | | The changes | Object of field values, or on MongoDB a field-operations callback | Yes | What to set on every matched row, passed as the only argument. There is no `data` wrapper. | #### Return type [#return-type-18] | Return type | Example | Description | | -------------------------- | ---------------------------------------------------- | ----------------- | | `AsyncIterableResult` | `await db.orm.public.Post.where(...).updateAll(...)` | The updated rows. | #### Examples [#examples-23] ##### Update all matching rows [#update-all-matching-rows] #### PostgreSQL ```typescript const updated = await db.orm.public.Post.where({ userId: aliceId }).updateAll({ priority: 'urgent' }); ``` #### MongoDB ```typescript const updated = await db.orm.users.where({ role: 'author' }).updateAll({ role: 'admin' }); // see the remark above: this is not one operation ``` ### `updateAndCount()` [#updateandcount] Update every matching row and return the count. #### Remarks [#remarks-24] - Available for PostgreSQL and MongoDB. Call `where()` first. Without a filter this changes every row. See the warning at the top of this section. - On MongoDB the count leaves out documents that already held the new values. On PostgreSQL it counts every matched row. - On PostgreSQL, a `limit()`, `offset()`, `cursor()`, `distinct()`, or `distinctOn()` on the collection makes this method throw an error whose `code` is `ORM.ARGUMENT_INVALID`, because it changes every matching row and cannot apply them. An `orderBy()` is accepted. To change only some of the matching rows, read their ids first, then filter on them, as [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data#update-or-delete-a-limited-number-of-rows) shows. #### Options [#options-19] | Name | Type | Required | Description | | ----------- | ----------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------ | | The changes | Object of field values, or on MongoDB a field-operations callback | Yes | What to set on every matched row, passed as the only argument. There is no `data` wrapper. | #### Return type [#return-type-19] | Return type | Example | Description | | ----------- | --------------------------------------------------------- | -------------------------- | | `number` | `await db.orm.public.Post.where(...).updateAndCount(...)` | The count of updated rows. | #### Examples [#examples-24] ##### Update and count [#update-and-count] #### PostgreSQL ```typescript const count = await db.orm.public.Post.where({ userId: carolId }).updateAndCount({ priority: 'low' }); ``` #### MongoDB ```typescript const count = await db.orm.users.where({ role: 'author' }).updateAndCount({ role: 'admin' }); ``` ### `delete()` [#delete] Remove the matched row and return it, or `null` if none matches. #### Remarks [#remarks-25] - Available for PostgreSQL and MongoDB. Requires a prior `where()` (see the section warning above). Returns `null` when no row matches. - On PostgreSQL, `delete()` deletes the row `first()` would return, so `orderBy()`, `offset()`, `cursor()`, `distinct()`, and `distinctOn()` choose it. After `limit(0)` it deletes nothing and returns `null`. #### Options [#options-20] `delete()` takes no required arguments. Filter with `where()` first. #### Return type [#return-type-20] | Return type | Example | Description | | ------------- | --------------------------------------------- | ------------------------------------------- | | `Row \| null` | `await db.orm.public.Tag.where(...).delete()` | The deleted row, or `null` if none matched. | #### Examples [#examples-25] ##### Delete a row [#delete-a-row] #### PostgreSQL ```typescript const created = await db.orm.public.Tag.create({ label: 'throwaway' }); const deleted = await db.orm.public.Tag.where({ id: created.id }).delete(); ``` #### MongoDB ```typescript const deleted = await db.orm.users.where({ id: userId }).delete(); ``` For Prisma ORM 7 users, `delete()` moves the filter into `where()`: ```diff - const deleted = await prisma.tag.delete({ where: { id } }); + const deleted = await db.orm.public.Tag.where({ id }).delete(); ``` ### `deleteAll()` [#deleteall] Remove every matching row and collect the results. #### Remarks [#remarks-26] - Available for PostgreSQL and MongoDB. Call `where()` first. Without a filter this deletes every row. See the warning at the top of this section. - Every matching row is deleted before the first row reaches you. Stopping the loop early does not save any of them. - On PostgreSQL, a `limit()`, `offset()`, `cursor()`, `distinct()`, or `distinctOn()` on the collection makes this method throw an error whose `code` is `ORM.ARGUMENT_INVALID`, because it changes every matching row and cannot apply them. An `orderBy()` is accepted. To change only some of the matching rows, read their ids first, then filter on them, as [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data#update-or-delete-a-limited-number-of-rows) shows. #### Options [#options-21] `deleteAll()` takes no required arguments. Filter with `where()` first. #### Return type [#return-type-21] | Return type | Example | Description | | -------------------------- | ------------------------------------------------- | ----------------- | | `AsyncIterableResult` | `await db.orm.public.Post.where(...).deleteAll()` | The deleted rows. | #### Examples [#examples-26] ##### Delete all matching rows [#delete-all-matching-rows] #### PostgreSQL ```typescript const deleted = await db.orm.public.Post.where({ userId: carolId }).deleteAll(); ``` #### MongoDB ```typescript const deleted = await db.orm.users.where({ role: 'reader' }).deleteAll(); ``` ### `deleteAndCount()` [#deleteandcount] Remove every matching row and return the count. #### Remarks [#remarks-27] - Available for PostgreSQL and MongoDB. Call `where()` first. Without a filter this deletes every row. See the warning at the top of this section. Returns `0` when no row matches. - On PostgreSQL, a `limit()`, `offset()`, `cursor()`, `distinct()`, or `distinctOn()` on the collection makes this method throw an error whose `code` is `ORM.ARGUMENT_INVALID`, because it changes every matching row and cannot apply them. An `orderBy()` is accepted. To change only some of the matching rows, read their ids first, then filter on them, as [Writing data](https://www.prisma.io/docs/orm/fundamentals/writing-data#update-or-delete-a-limited-number-of-rows) shows. #### Options [#options-22] `deleteAndCount()` takes no required arguments. Filter with `where()` first. #### Return type [#return-type-22] | Return type | Example | Description | | ----------- | ------------------------------------------------------ | -------------------------- | | `number` | `await db.orm.public.Post.where(...).deleteAndCount()` | The count of deleted rows. | #### Examples [#examples-27] ##### Delete and count [#delete-and-count] #### PostgreSQL ```typescript const count = await db.orm.public.Post.where({ userId: carolId }).deleteAndCount(); ``` #### MongoDB ```typescript const count = await db.orm.users.where({ role: 'reader' }).deleteAndCount(); ``` ### `upsert()` [#upsert] Insert a row if none matches, otherwise update the existing row. #### Remarks [#remarks-28] - On MongoDB, call `where()` first. The filter decides which document is updated. - On PostgreSQL, `upsert()` never reads the filters. A `where()` before it is ignored, without an error. `conflictOn` decides which row counts as already existing. - `conflictOn` names the unique field that decides insert or update, such as `conflictOn: { label: 'typescript' }`. Only the field name is used. The value is required by the type and is then ignored, so pass any value of the right type other than `null`, which the type refuses. - For a unique constraint on several columns together, name every one of those columns, as in `conflictOn: { orgId: '', email: '' }`. The set you name has to match one `@@unique` constraint. - When a row is inserted on PostgreSQL, the `create` side is used as you wrote it. - When a row is inserted on MongoDB, a field that appears in both `create` and `update` takes the `update` value. - On MongoDB, a field that has an update default, such as `temporal.UpdatedAt`, keeps the value you give it in `create` when a document is inserted, and still gets the current time when a document is updated. An upsert whose `create` sets such a field cannot also `pull()` by a match document in its update. That throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. - On MongoDB, the `update` side takes an object of field values or a field-operations callback. - On PostgreSQL, `upsert()` does not work on a variant stored in its own table, the same restriction as [`createAndCount()`](#createandcount). It throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`, with the message `upsert() is not supported for MTI variant "Bug" on model "Task". Use createAll() instead.` - There is no upsert for such a variant. Read the row with `first()`, then call `create()` or `update()`. - On MongoDB, the `id` on the returned row is a hex string, not the driver's `ObjectId`. #### Options [#options-23] | Name | Type | Required | Description | | ------------ | ----------------------------------------------------------------- | -------- | ----------------------------------------------------------- | | `create` | Object of field values | Yes | The row to insert if none matches. | | `update` | Object of field values, or on MongoDB a field-operations callback | Yes | The changes to apply if a row matches. | | `conflictOn` | Object naming the unique column or columns, PostgreSQL only | No | What decides insert or update. Omit to use the primary key. | #### Return type [#return-type-23] | Return type | Example | Description | | ----------- | ------------------------------------- | ---------------------------- | | `Row` | `await db.orm.public.Tag.upsert(...)` | The inserted or updated row. | #### Examples [#examples-28] ##### Insert or update (PostgreSQL) [#insert-or-update-postgresql] ```typescript // Insert: no existing row has label 'brand-new', so the row is inserted // exactly as the create side describes it. const inserted = await db.orm.public.Tag.upsert({ create: { label: 'brand-new' }, update: { label: 'brand-new-updated' }, conflictOn: { label: 'brand-new' }, // only the column name `label` is used }); // Update: a row with label 'typescript' already exists, so the update runs against it. const updated = await db.orm.public.Tag.upsert({ create: { label: 'typescript' }, update: { label: 'typescript-renamed' }, conflictOn: { label: 'typescript' }, }); ``` ##### Insert or update (MongoDB) [#insert-or-update-mongodb] ```typescript const user = await db.orm.users.where({ email: 'newperson@example.com' }).upsert({ create: { name: 'New Person', email: 'newperson@example.com', bio: null, role: 'reader', address: null, }, update: { bio: 'set on upsert' }, }); // on insert, bio is 'set on upsert': the `update` value wins on MongoDB ``` ##### Field-operations callback on the update side (MongoDB) [#field-operations-callback-on-the-update-side-mongodb] ```typescript const post = await db.orm.posts .variant('tutorial') .where({ id: tutorialId }) .upsert({ create: { title: 'Should not be used', content: 'unused', authorId: bobId, createdAt: new Date('2024-01-01T00:00:00.000Z'), difficulty: 'beginner', duration: 0, }, update: (t) => [t.content.set('Updated content')], }); ``` For Prisma ORM 7 users, `upsert()` replaces the `where` argument with `conflictOn` (PostgreSQL): ```diff - const tag = await prisma.tag.upsert({ - where: { label: 'typescript' }, - update: { label: 'typescript-renamed' }, - create: { label: 'typescript' }, - }); + const tag = await db.orm.public.Tag.upsert({ + update: { label: 'typescript-renamed' }, + create: { label: 'typescript' }, + conflictOn: { label: 'typescript' }, + }); ``` ## Grouped aggregates [#grouped-aggregates] These methods exist only on PostgreSQL collections. `aggregate()` treats every row the query matches as one group, while `groupBy().aggregate()` aggregates per group and `having()` filters those groups. A MongoDB model's collection has none of them, so to aggregate on MongoDB, use the pipeline builder, which is Prisma ORM's API for MongoDB's aggregation pipeline and is described in [MongoDB: Pipeline builder](https://www.prisma.io/docs/orm/fundamentals/advanced-queries#mongodb-pipeline-builder). The examples below use a separate `Customer` / `Order` schema, where `Order.amount` is a numeric column: **Expand for the aggregate example schema** ```prisma model Customer { id Uuid @id @onCreate(uuid()) name String segment String orders Order[] @@map("customer") } model Order { id Uuid @id @onCreate(uuid()) customerId Uuid channel String amount Int quantity Int? placedAt DateTime @default(now()) customer Customer @relation(fields: [customerId], references: [id]) @@map("order") } ``` ### `aggregate()` [#aggregate] Compute aggregates over the rows the query matches. `aggregate()` runs the query, so you `await` it directly instead of calling `.all()`. #### Remarks [#remarks-29] - PostgreSQL only. Not available on MongoDB. - `count()` counts rows and returns a number, `0` when no rows match. - `count(field)` counts the rows where that field is not null. - To count or sum the related rows of each parent row, use the `include()` callback instead; see [Refinements, aggregates, and combine](#refinements-aggregates-and-combine). - `sum()`, `avg()`, `min()`, and `max()` return `null`, not `0`, when no rows match. Handle the `null`. - A `limit()` or `offset()` earlier in the chain is applied first. The aggregate then covers only the rows they kept. - What `sum()` and `avg()` give you back depends on the column type, as the table below shows. | Column type | `sum()` returns | `avg()` returns | Exact variant | | --------------- | ---------------- | ---------------- | ----------------------------- | | `Int`, `BigInt` | `number \| null` | `number \| null` | `sumBigInt()`, `avgDecimal()` | | `Decimal` | `string \| null` | `string \| null` | `avgDecimal()` | Use `sumBigInt()` when the total can pass `Number.MAX_SAFE_INTEGER`, because a `sum()` whose total passes it throws an error whose `code` is `RUNTIME.DECODE_FAILED`, thrown when the query result is read. Use `avgDecimal()` when you need the exact mean as a string, such as `'10.3333333333333333'`. Over a `Decimal` column `sum()` and `avg()` are already exact, and `sumBigInt()` is not available. #### Options [#options-24] | Argument | Type | Required | Description | | ---------- | -------------------------------------------------- | -------- | ---------------------------------------------------------------------------------- | | `selector` | Callback `(agg) => ({ total: agg.sum('amount') })` | Yes | The aggregates to compute. Each key you return becomes a key of the result object. | The callback's `agg` argument gives you `count()`, `count(field)`, `sum(field)`, `avg(field)`, `min(field)`, and `max(field)`, plus `countBigInt()`, `sumBigInt(field)`, and `avgDecimal(field)`. `agg.countBigInt()` counts the same rows as `agg.count()` and returns a `bigint`. #### Return type [#return-type-24] | Return type | Example | Description | | ----------- | --------------- | --------------------------------------------------------------------------------------------- | | Object | `{ total: 10 }` | One key per key you returned from the callback. `sum`, `avg`, `min`, and `max` can be `null`. | #### Examples [#examples-29] ##### Count (PostgreSQL) [#count-postgresql] ```typescript const stats = await db.orm.public.Order.aggregate((agg) => ({ total: agg.count(), withQuantity: agg.count('quantity'), // rows where quantity is not null })); // { total: 10, withQuantity: 8 } ``` ##### Sum and average (PostgreSQL) [#sum-and-average-postgresql] ```typescript const stats = await db.orm.public.Order.where({ customerId: '20000000-0000-0000-0000-000000000001', }).aggregate((agg) => ({ totalAmount: agg.sum('amount'), avgAmount: agg.avg('amount'), })); // { totalAmount: 900, avgAmount: 300 } ``` ##### Min and max (PostgreSQL) [#min-and-max-postgresql] ```typescript const stats = await db.orm.public.Order.aggregate((agg) => ({ cheapest: agg.min('amount'), priciest: agg.max('amount'), })); // { cheapest: 10, priciest: 500 } ``` ##### Counts and totals past JavaScript's precision limit (PostgreSQL) [#counts-and-totals-past-javascripts-precision-limit-postgresql] ```typescript const exact = await db.orm.public.Order.aggregate((agg) => ({ rows: agg.countBigInt(), total: agg.sumBigInt('amount'), average: agg.avgDecimal('amount'), })); // { rows: 10n, total: 1500n, average: '150.0000000000000000' } ``` ##### `null` over an empty set (PostgreSQL) [#null-over-an-empty-set-postgresql] ```typescript const stats = await db.orm.public.Order.where((o) => o.amount.gt(999_999)).aggregate((agg) => ({ total: agg.sum('amount'), average: agg.avg('amount'), count: agg.count(), })); // { total: null, average: null, count: 0 } ``` For Prisma ORM 7 users, `aggregate()` takes a selector callback instead of `_sum`/`_avg` keys: ```diff - const stats = await prisma.order.aggregate({ _sum: { amount: true }, _avg: { amount: true } }); + const stats = await db.orm.public.Order.aggregate((agg) => ({ total: agg.sum('amount'), average: agg.avg('amount') })); ``` ### `groupBy()` [#groupby] Group rows by one or more fields, then aggregate per group. #### Remarks [#remarks-30] - PostgreSQL only. Not available on MongoDB. - Pass field names as separate arguments: `groupBy('customerId', 'channel')`. - `groupBy(...)` returns a `GroupedCollection`. You cannot `await` it. Call `.aggregate(...)` on it, which runs the query and gives you one row per group. Each row holds the grouping fields, typed as they are on your model, plus the keys you returned from the aggregate callback. - Chain order: `where(...)` comes before `groupBy(...)` and filters the rows that get grouped. After `groupBy(...)` come `having(...)` and `orderBy(...)`, then `limit(...)` and `offset(...)`. `aggregate(...)` comes last. - `orderBy(...)` on a grouped collection sorts the groups by a field you grouped on. You cannot sort groups by an aggregate value. To do that, write the query with the SQL query builder instead. See [Advanced queries](https://www.prisma.io/docs/orm/fundamentals/advanced-queries). - `limit(n)` and `offset(n)` take a slice of the groups, and both require an `orderBy(...)` before them. Without one they are a TypeScript error. #### Options [#options-25] | Argument | Type | Required | Description | | ----------- | ---------------------------------- | -------- | ----------------------- | | `...fields` | One or more field names (`string`) | Yes | The fields to group by. | #### Return type [#return-type-25] | Return type | Example | Description | | ------------------- | ------------------------------------------- | -------------------------------------- | | `GroupedCollection` | `db.orm.public.Order.groupBy('customerId')` | Not a promise. Call `.aggregate(...)`. | #### Examples [#examples-30] ##### Group and aggregate (PostgreSQL) [#group-and-aggregate-postgresql] ```typescript const perCustomer = await db.orm.public.Order.groupBy('customerId').aggregate((agg) => ({ orderCount: agg.count(), totalAmount: agg.sum('amount'), })); // one row per customer, each { customerId, orderCount, totalAmount } ``` ##### Sort and page the groups (PostgreSQL) [#sort-and-page-the-groups-postgresql] ```typescript const firstTwoChannels = await db.orm.public.Order.groupBy('channel') .orderBy((group) => group.channel.desc()) // desc() sorts descending, asc() ascending .limit(2) .aggregate((agg) => ({ orderCount: agg.count() })); ``` For Prisma ORM 7 users, `groupBy()` chains into `aggregate()` instead of taking a `by` array with aggregate keys: ```diff - const perCustomer = await prisma.order.groupBy({ by: ['customerId'], _sum: { amount: true } }); + const perCustomer = await db.orm.public.Order.groupBy('customerId').aggregate((agg) => ({ totalAmount: agg.sum('amount') })); ``` ### `having()` [#having] Filter groups by an aggregate comparison. #### Remarks [#remarks-31] - PostgreSQL only. Not available on MongoDB. Call it on the result of `groupBy()`. - The `having()` callback gives you `count()`, `count(field)`, `sum(field)`, `avg(field)`, `min(field)`, and `max(field)`. Call a comparison on the result: `h.sum('amount').gt(1000)`. The comparisons are `eq`, `neq`, `gt`, `lt`, `gte`, and `lte`. - For more than one condition, call `.having(...)` again. The conditions are combined with AND. - You cannot refer to a key you returned from `aggregate()` here. Write the aggregate out again in `having()`. #### Options [#options-26] | Argument | Type | Required | Description | | ----------- | ------------------------------------------ | -------- | ------------------------------------ | | `predicate` | Callback `(h) => h.sum('amount').gt(1000)` | Yes | The condition each group must match. | #### Return type [#return-type-26] | Return type | Example | Description | | ------------------- | ------------------------------------------------------- | -------------------------------------- | | `GroupedCollection` | `db.orm.public.Order.groupBy('customerId').having(...)` | Not a promise. Call `.aggregate(...)`. | #### Examples [#examples-31] ##### Filter groups by a sum (PostgreSQL) [#filter-groups-by-a-sum-postgresql] ```typescript const bigSpenders = await db.orm.public.Order.groupBy('customerId') .having((h) => h.sum('amount').gt(1000)) .aggregate((agg) => ({ totalAmount: agg.sum('amount') })); ``` ## Filter conditions and operators [#filter-conditions-and-operators] A condition says which rows match. You write one inside `where()`, and inside a relation filter such as `u.posts.some(...)`. If you call `where()` more than once, the conditions are ANDed, and one call can take the object form and the next the callback form. PostgreSQL and MongoDB write conditions differently. - **PostgreSQL** compares a field with a method on that field, such as `u.email.eq(...)`, inside a callback. You name the callback's argument yourself, so the `u` and `p` below are only names. To combine conditions, import `and`, `or`, `not`, and `all` from `@prisma/orm-postgres/orm-client`. - **MongoDB** has no callback form. `where()` takes either the shorthand object or a filter such as `MongoFieldFilter.eq('name', 'Alice')`. The full list is under [`MongoFieldFilter`](#mongofieldfilter). Import `MongoFieldFilter`, `MongoAndExpr`, and `MongoOrExpr` from `@prisma/orm-mongo/query-ast/execution`. To combine conditions, use `.and()`, `.not()`, and `MongoOrExpr.of([...])`, described under [Combinators](#combinators). ### Scalar comparisons (PostgreSQL) [#scalar-comparisons-postgresql] Comparison methods on a field. #### Remarks [#remarks-32] - PostgreSQL. Which comparison methods a field has depends on its type. Autocomplete shows which ones apply. - `isNull()` and `isNotNull()` are on every field. - `eq`, `neq`, `in`, and `notIn` are on a field whose values can be compared for equality. - `gt`, `lt`, `gte`, and `lte` are on a field whose values can be ordered. Each takes a value of the field's type, or another field of the same type, as in `(t) => t.title.gt(t.description)` on `Task`. The other field may be nullable, as `description` is. A row where either side is `NULL` matches none of them. - `like` and `ilike` are on a text field. - `String` fields and text-backed enums have all of them. A [native enum](#example-schema) field, typed `pg.enum(...)` in the contract, has all but `like` and `ilike`, because PostgreSQL has no pattern match for an `enum` type. `Int`, `BigInt`, `Float`, `Decimal`, `Uuid`, and `DateTime` fields also have all but `like` and `ilike`. A `Boolean` field has only the equality methods, `isNull()`, and `isNotNull()`. A `Json` field has only `isNull()` and `isNotNull()`. - `like()` matches a SQL pattern case-sensitively. `ilike()` matches the same pattern case-insensitively. - `in([])` matches no rows. `notIn([])` matches every row. #### Options [#options-27] | Method | Type | Description | | ------------------------------------------------------- | ------------------ | ------------------------------------------------ | | `eq(value)` / `neq(value)` | Exact value | Equality / inequality. | | `gt(value)` / `lt(value)` / `gte(value)` / `lte(value)` | Ordered value | Ordered comparisons. | | `like(pattern)` / `ilike(pattern)` | SQL `LIKE` pattern | Case-sensitive / case-insensitive pattern match. | | `in(values)` / `notIn(values)` | Array of values | Membership / exclusion. | | `isNull()` / `isNotNull()` | No argument | NULL checks. | #### Examples [#examples-32] ##### Equality and inequality (PostgreSQL) [#equality-and-inequality-postgresql] ```typescript const alice = await db.orm.public.User.where((u) => u.email.eq('alice@example.com')).first(); const notAlice = await db.orm.public.User.where((u) => u.email.neq('alice@example.com')).all(); ``` ##### Ordered comparisons (PostgreSQL) [#ordered-comparisons-postgresql] ```typescript const after = await db.orm.public.Post.where((p) => p.createdAt.gt(new Date('2024-01-02T10:00:00.000Z')), ).all(); // gte() is the same comparison, including the boundary value. ``` ##### Pattern matching (PostgreSQL) [#pattern-matching-postgresql] ```typescript const matches = await db.orm.public.User.where((u) => u.email.like('%@example.com')).all(); // case-sensitive const caseInsensitive = await db.orm.public.User.where((u) => u.email.ilike('%@EXAMPLE.COM')).all(); ``` ##### Membership and NULL checks (PostgreSQL) [#membership-and-null-checks-postgresql] ```typescript const lowOrHigh = await db.orm.public.Post.where((p) => p.priority.in(['low', 'high'])).all(); const notLowOrHigh = await db.orm.public.Post.where((p) => p.priority.notIn(['low', 'high'])).all(); const withoutDescription = await db.orm.public.Task.where((t) => t.description.isNull()).all(); // Task.description is String? in the example schema ``` ### Full-text search (PostgreSQL) [#full-text-search] PostgreSQL full-text search on a text field, or over a full-text index that spans several fields. #### Remarks [#remarks-33] - PostgreSQL. A text field has `fullTextMatches(query, options?)` to filter and `fullTextRank(query, options?)` to sort by relevance. - To search a [`@@fullTextIndex`](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#full-text-indexes) by name, use the second argument of a `where` or `orderBy` callback, `{ fns, indexes }`: `fns.fullTextMatches(indexes.post_search, q)` and `fns.fullTextRank(indexes.post_search, q)`. `indexes` holds the model's indexes, keyed by the name the schema gave each, and the query reads the index's fields, weights and language from the contract, so PostgreSQL uses the index. This is the only way to search several fields at once in the ORM. - The [SQL query builder](https://www.prisma.io/docs/orm/reference/sql-query-builder) has both, as `fns.fullTextMatches(column, query)` and `fns.fullTextRank(column, query)`. It also has `fns.fullTextHeadline(column, query)`, which returns the matching text with each match marked. Use the SQL query builder when you want that text in your results, because the ORM's `select()` takes only field names. - The `query` argument is a parsed search query, which PostgreSQL calls a `tsquery`. Passing a plain string is a type error. Build the query with one of these functions from `@prisma/orm-postgres/target/full-text`: `websearchToTsquery(text)` reads text the way a web search box does: the user puts a phrase in quotes, writes `or` between choices, and puts `-` before a word to leave it out. `plaintoTsquery(text)` requires every word, and `phrasetoTsquery(text)` requires the words in order. - `toTsquery(text)` reads PostgreSQL's own search syntax, in which `&` means and, `|` means or, `!` means not, and `:*` after a word matches any word that starts with it. Text that breaks this syntax makes the query fail when it runs, so never pass user input to `toTsquery()`. - To add an operator to user text, such as `:*` for a prefix match while the user types, use the `tsquery` template tag from the same package: `` tsquery`${term}:*` ``. Text inside `${}` is searched as plain words, so the user's text cannot add operators of its own. When it holds several words, they must appear next to each other and in that order, and `:*` applies to each word, so `` tsquery`${'new y'}:*` `` finds `new` followed by a word that starts with `y`. Do not put quotes around the `${}` yourself. - `language` is the language whose rules PostgreSQL uses to match words, so that in `english` a search for `run` also finds `running`. It defaults to `english`. Pass the same `language` to `fullTextMatches()` or `fullTextRank()` and to the function that builds the query, as in the example below. The tag takes it as `` tsquery({ language: 'german' })`${input}:*` ``. ```typescript title="src/search-posts.ts (excerpt)" const q = websearchToTsquery(input, { language: 'german' }); const posts = await db.orm.public.Post.where((p) => p.title.fullTextMatches(q, { language: 'german' })).all(); ``` - Add [`@@fullTextIndex`](https://www.prisma.io/docs/orm/contract-authoring/psl-syntax#full-text-indexes) to the model, with the same `language`, so PostgreSQL can use an index for the search. Without it the query still works, but PostgreSQL reads every row. The attribute takes the fields to index and a `name:` for the index: ```prisma title="src/prisma/contract.prisma (excerpt)" model Post { id Uuid @id @onCreate(uuid()) title String @@fullTextIndex([title], name: "post_title_search", language: "german") @@map("post") } ``` Then run `npx prisma contract emit` and create the index the way you apply any other contract change: `npx prisma db update` while you are iterating, or `npx prisma migration plan` followed by `npx prisma db migrate`. See [How migrations work](https://www.prisma.io/docs/orm/migrations/how-migrations-work). - A native enum field, typed `pg.enum(...)` in the contract, has no full-text operations. #### Options [#options-28] | Method | Options | Description | | ---------------------------------- | ------------------------------------------- | -------------------------------------------- | | `fullTextMatches(query, options?)` | `language` | Matches rows whose field matches `query`. | | `fullTextRank(query, options?)` | `language`, `normalization`, `coverDensity` | The relevance of the match, for `orderBy()`. | - `normalization` sets how the length of the text changes the rank. It is a whole number from 0 to 63, made by adding PostgreSQL's flags together. The default, 0, ignores length. `2` divides the rank by the length of the text, so of two titles with the same matches, the shorter one ranks higher. The PostgreSQL manual lists every flag under [Ranking search results](https://www.postgresql.org/docs/current/textsearch-controls.html#TEXTSEARCH-RANKING). - `coverDensity: true` also counts how close together the matching words are, so a row where they appear near each other ranks higher. #### Examples [#examples-33] ##### Search and sort by relevance [#search-and-sort-by-relevance] ```typescript title="src/search-posts.ts" import { tsquery, websearchToTsquery } from '@prisma/orm-postgres/target/full-text'; const q = websearchToTsquery('"Prisma ORM" -draft'); const posts = await db.orm.public.Post.select('id', 'title') .where((p) => p.title.fullTextMatches(q)) .orderBy((p) => p.title.fullTextRank(q).desc()) .limit(10) .all(); const suggestions = await db.orm.public.Post.select('id', 'title') .where((p) => p.title.fullTextMatches(tsquery`${input}:*`)) .limit(5) .all(); ``` ##### Search a weighted index over several fields [#search-a-weighted-index-over-several-fields] With `@@fullTextIndex([[title, subtitle], body], name: "post_search")` on the model, a match in `title` or `subtitle` ranks above a match in `body`: ```typescript title="src/search-posts.ts" import { websearchToTsquery } from '@prisma/orm-postgres/target/full-text'; const q = websearchToTsquery(input); const posts = await db.orm.public.Post.select('id', 'title') .where((p, { fns, indexes }) => fns.fullTextMatches(indexes.post_search, q)) .orderBy((p, { fns, indexes }) => fns.fullTextRank(indexes.post_search, q).desc()) .limit(20) .all(); ``` ### Combinators [#combinators] Combine or negate conditions. #### Remarks [#remarks-34] - On PostgreSQL, import `and`, `or`, `not`, and `all` from `@prisma/orm-postgres/orm-client`. You call them as functions, as in `and(a, b)`. They are not methods on a field. - On PostgreSQL, `and(...)` and `or(...)` take any number of conditions, so `and(a, b, c)` is fine. `not(...)` takes one. - On PostgreSQL, `all()` takes no arguments and returns a condition that matches every row. This `all()` is not the `.all()` you call at the end of a chain to run the query. - On MongoDB, call `.and(other)` and `.not()` on a condition you already built, as in `MongoFieldFilter.eq('name', 'Alice').and(...)`. There is no `.or()` method. To OR conditions, build `MongoOrExpr.of([...])`. - On MongoDB, `.and(other)` takes exactly one condition. For three, chain it: `a.and(b).and(c)`. You can also write all three at once with `MongoAndExpr.of([a, b, c])`. `MongoAndExpr` and `MongoOrExpr` come from the same module as `MongoFieldFilter`. #### Examples [#examples-34] ##### `and` / `or` / `not` (PostgreSQL) [#and--or--not-postgresql] ```typescript import { and, or, not } from '@prisma/orm-postgres/orm-client'; const both = await db.orm.public.Post.where((p) => and(p.priority.eq('low'), p.userId.eq('00000000-0000-0000-0000-000000000003')), ).all(); const either = await db.orm.public.Post.where((p) => or(p.priority.eq('urgent'), p.priority.eq('high'))).all(); const negated = await db.orm.public.Post.where((p) => not(p.priority.eq('low'))).all(); ``` ##### `all` (PostgreSQL) [#all-postgresql] ```typescript import { all } from '@prisma/orm-postgres/orm-client'; const everyPost = await db.orm.public.Post.where(() => all()).all(); ``` ##### `and` / `or` / `not` (MongoDB) [#and--or--not-mongodb] ```typescript import { MongoAndExpr, MongoFieldFilter, MongoOrExpr } from '@prisma/orm-mongo/query-ast/execution'; const both = await db.orm.users .where(MongoFieldFilter.eq('role', 'author').and(MongoFieldFilter.eq('name', 'Alice'))) .all(); const either = await db.orm.users .where(MongoOrExpr.of([MongoFieldFilter.eq('name', 'Alice'), MongoFieldFilter.eq('name', 'Bob')])) .all(); const negated = await db.orm.users.where(MongoFieldFilter.eq('name', 'Alice').not()).all(); const bothAtOnce = await db.orm.users .where(MongoAndExpr.of([MongoFieldFilter.eq('role', 'author'), MongoFieldFilter.eq('name', 'Alice')])) .all(); ``` ##### AND of two ORs (MongoDB) [#and-of-two-ors-mongodb] ```typescript const authorsNamedAliceOrBob = await db.orm.users .where( MongoOrExpr.of([MongoFieldFilter.eq('name', 'Alice'), MongoFieldFilter.eq('name', 'Bob')]).and( MongoOrExpr.of([MongoFieldFilter.eq('role', 'author'), MongoFieldFilter.eq('role', 'editor')]), ), ) .all(); ``` ### Relation filters (PostgreSQL) [#relation-filters-postgresql] Filter parents by their related rows. #### Remarks [#remarks-35] - PostgreSQL. `some()`, `every()`, and `none()` are methods on a to-many relation, which is a relation that holds many related rows, such as `User.posts`. - Each of the three takes either a callback or a plain object of equality matches: `u.posts.some((p) => p.priority.eq('urgent'))` and `u.posts.some({ priority: 'urgent' })` mean the same thing. - `some()` with no argument matches parents with at least one related row. - A user with no posts matches `every()`. - `not(u.posts.some(...))` and `u.posts.none(...)` match the same rows. Prefer `none(...)`. - All three are also methods on a to-one relation, such as `Post.user`, which holds a single related row. There `some()` means the related row matches, and `none()` means it does not. `every()` matches when the related row matches, and also when there is no related row. #### Examples [#examples-35] ##### `some` / `every` / `none` (PostgreSQL) [#some--every--none-postgresql] ```typescript const withUrgentPost = await db.orm.public.User.where((u) => u.posts.some((p) => p.priority.eq('urgent'))).all(); const withAnyTag = await db.orm.public.Tag.where((t) => t.posts.some()).all(); const allLowPriority = await db.orm.public.User.where((u) => u.posts.every((p) => p.priority.eq('low'))).all(); const noUrgentPost = await db.orm.public.User.where((u) => u.posts.none((p) => p.priority.eq('urgent'))).all(); ``` ##### To-one relation predicate (PostgreSQL) [#to-one-relation-predicate-postgresql] ```typescript const postsByAlice = await db.orm.public.Post.where((p) => p.user.some({ email: 'alice@example.com' })).all(); const postsByAnyoneElse = await db.orm.public.Post.where((p) => p.user.none({ email: 'alice@example.com' })).all(); ``` For Prisma ORM 7 users, `some()` and `none()` on a to-one relation replace `is` and `isNot`: ```diff - const postsByAlice = await prisma.post.findMany({ where: { user: { is: { email: 'alice@example.com' } } } }); + const postsByAlice = await db.orm.public.Post.where((p) => p.user.some({ email: 'alice@example.com' })).all(); ``` ### Shorthand object filter [#shorthand-object-filter] A plain object of equality matches, ANDed together. #### Remarks [#remarks-36] - Available for PostgreSQL and MongoDB. - Multiple keys are combined with implicit AND. A key set to `undefined` is skipped. - On PostgreSQL, a key set to `null` becomes an IS NULL check. On MongoDB it matches documents where the field is null and documents where the field is missing. - On MongoDB, what a list field matches depends on the value you give it. A whole list, such as `{ tags: ['a', 'b'] }`, matches a stored list equal to it. `{ tags: [] }` matches an empty list. `{ tags: null }` matches a document whose list is null or missing, not a list that holds a `null` item. - On PostgreSQL, the shorthand object cannot filter a field whose type cannot be compared for equality, such as `Json`. Using that field as a key compiles, but fails at run time with an error whose `code` is `ORM.FILTER_UNSUPPORTED`. Use the callback form, which has whatever methods that type does support. On a `Json` field those are `isNull()` and `isNotNull()`. #### Examples [#examples-36] ##### Shorthand object [#shorthand-object] #### PostgreSQL ```typescript const row = await db.orm.public.Post.where({ userId: '00000000-0000-0000-0000-000000000001', priority: 'high', }).first(); ``` #### MongoDB ```typescript const alice = await db.orm.users.where({ name: 'Alice', role: 'author' }).first(); ``` ### `MongoFieldFilter` [#mongofieldfilter] Helpers that build one MongoDB filter condition. Use them for anything other than equality on a top-level field, which `.where({ name: 'Alice' })` already covers. Chaining two of them, as in `.where(a).where(b)`, joins them with AND. For OR, see [Combinators](#combinators). #### Remarks [#remarks-37] - MongoDB. Import from `@prisma/orm-mongo/query-ast/execution`. `MongoExistsExpr` is in the same module. - The helpers are `of`, `eq`, `neq`, `gt`, `lt`, `gte`, `lte`, `in`, `nin`, `isNull`, and `isNotNull`. Each returns one filter, which you can pass straight to `.where()` or hold in a variable. There is no `ne`; use `neq`. - `of(field, operator, value)`: pass the MongoDB operator as a string, including the `$`, for example `'$regex'`. Use it for operators the named helpers do not cover, such as `MongoFieldFilter.of('tags', '$size', 3)` on an array field `tags`. A misspelled operator such as `'$regexp'` is not caught until the query runs. - `isNull(field)` matches documents where the field is `null` and documents where the field is missing. `isNotNull(field)` matches everything else. To test only whether a field is present, use `MongoExistsExpr.exists(field)` or `MongoExistsExpr.notExists(field)`. Those two are all `MongoExistsExpr` offers. - There is no `regex`, `elemMatch`, `all`, or `size` helper. Write those operators through `of`. - A filter you pass to the ORM's `where()` converts each value through the field's type, the same way a write does. Pass the value your code reads from that field: a `bigint` for a `BigInt` field, decimal text for a `Decimal128` field, a hex string or an `ObjectId` for an `ObjectId` field, and an integer in the 32-bit range for an `Int32` field. A value the field's type refuses throws an error whose `code` is `RUNTIME.ENCODE_FAILED` before the query runs. - The helpers accept strings, numbers, booleans, `null`, `Date` values, and arrays and plain objects of these. Put any other value, such as a `bigint` or an `ObjectId`, in a `MongoParamRef` from `@prisma/orm-mongo/value`: `MongoFieldFilter.gt('views', new MongoParamRef(5n))`. - The pipeline builder's [`match()`](https://www.prisma.io/docs/orm/reference/pipeline-builder#match) does not convert values. It sends each value as you give it. #### Options [#options-29] | Method | Type | Description | | ----------------------------------------------------------------------------------- | --------------------------------------- | ---------------------------------------------------------------------------------------- | | `of(field, operator, value)` | Field + MongoDB operator string + value | Any operator, including the `$`, such as `'$regex'`. | | `eq(field, value)` / `neq(field, value)` | Field + value | Equality / inequality. | | `gt(field, value)` / `lt(field, value)` / `gte(field, value)` / `lte(field, value)` | Field + value | Greater than, less than, greater or equal, less or equal. | | `in(field, values)` / `nin(field, values)` | Field + array | Membership / exclusion. | | `isNull(field)` / `isNotNull(field)` | Field | `isNull` matches a `null` value or a missing field. `isNotNull` matches everything else. | #### Examples [#examples-37] ##### Comparison helpers (MongoDB) [#comparison-helpers-mongodb] ```typescript import { MongoFieldFilter } from '@prisma/orm-mongo/query-ast/execution'; const alice = await db.orm.users.where(MongoFieldFilter.eq('name', 'Alice')).first(); const notAlice = await db.orm.users.where(MongoFieldFilter.neq('name', 'Alice')).all(); const strictlyAfter = await db.orm.posts .where(MongoFieldFilter.gt('createdAt', new Date('2024-01-01T10:00:00.000Z'))) .all(); ``` ##### Membership and null checks (MongoDB) [#membership-and-null-checks-mongodb] ```typescript const articlesOrTutorials = await db.orm.posts .where(MongoFieldFilter.in('kind', ['article', 'tutorial'])) .all(); const notArticles = await db.orm.posts.where(MongoFieldFilter.nin('kind', ['article'])).all(); const noBio = await db.orm.users.where(MongoFieldFilter.isNull('bio')).all(); const hasBio = await db.orm.users.where(MongoFieldFilter.isNotNull('bio')).all(); ``` ##### An operator written as a string, and `$exists` (MongoDB) [#an-operator-written-as-a-string-and-exists-mongodb] ```typescript import { MongoExistsExpr, MongoFieldFilter } from '@prisma/orm-mongo/query-ast/execution'; const matching = await db.orm.posts.where(MongoFieldFilter.of('title', '$regex', '^Hello')).all(); const withBioField = await db.orm.users.where(MongoExistsExpr.exists('bio')).all(); ``` ### Dot-notation into an embedded object (MongoDB) [#dot-notation-into-an-embedded-object-mongodb] Filter into a field of an object stored inside the document, rather than in its own collection, using a dot path. #### Remarks [#remarks-38] - MongoDB. Use `MongoFieldFilter.eq('address.city', 'San Francisco')` for a dotted path. It takes the path as a plain `string` and needs no cast. - You can also write `.where({ 'address.city': 'San Francisco' })`. That object form runs correctly, but it does not type-check. `.where()` only knows your model's top-level field names, so you must cast. #### Examples [#examples-38] ##### Dot-notation path (MongoDB) [#dot-notation-path-mongodb] ```typescript import { MongoFieldFilter } from '@prisma/orm-mongo/query-ast/execution'; const usersInSf = await db.orm.users.where(MongoFieldFilter.eq('address.city', 'San Francisco')).all(); ``` `MongoFieldFilter.eq` is the recommended form, because the object form needs a cast, and the cast drops type checking for every key in that object: ```typescript const usersInSf = await db.orm.users .where({ 'address.city': 'San Francisco' } as unknown as Record) .all(); ``` ## Field update operations [#field-update-operations] On MongoDB, `update()`, `updateAll()`, `updateAndCount()`, and `upsert()` take a callback in place of a data object. The callback gets one argument, the update builder, written `u` in these examples, and returns an array of the operations you want. `updateAndCount()` takes the callback in the same place as `update()`, while `upsert()` takes it as its `update` key, beside `create`. PostgreSQL has no callback form: pass an object to [`update()`](#update) instead. - For a top-level field, read it off the update builder as a property: `u.bio.set('Hello')`. - For a field inside an embedded object, call the update builder as a function with the dot path: `u('address.city').set('San Francisco')`. The `upsert()` callback cannot use a dot path. Given one, `upsert()` throws an error whose `code` is `ORM.OPERATION_UNSUPPORTED`. Set the whole embedded object on its top-level field instead, as in `u.address.set({ street: '1 Market St', city: 'San Francisco', zip: '94105', country: 'US' })`. Give every field of the embedded object, including the optional ones. All four methods require `.where()`, and without one they throw an error whose `code` is `ORM.WHERE_MISSING`. `.where({})` does not satisfy that rule, because an empty object adds no condition. To change every document, pass a filter that matches them all, such as `MongoFieldFilter.isNotNull('id')`. An `id` filter takes the hex string that a read gives you back, as the examples below show. On `upsert()`, `.where()` picks the document to update, exactly as on `update()`, and the full example is under [`upsert()`](#upsert). `update()` returns the updated document, or `null` when nothing matched, and `updateAll()` returns an [`AsyncIterableResult`](#asynciterableresult) of the changed documents. [`updateAndCount()`](#updateandcount) exists on both databases, and it returns the number of changed documents. ### Available operations (MongoDB) [#available-operations-mongodb] #### Remarks [#remarks-39] - MongoDB only. These are the four operations the update builder gives you for single-value fields: `set()`, `unset()`, `inc()`, and `mul()`. The four array operations follow under [Array operations](#array-operations-mongodb-unverified). - `set(value)` assigns a field. - `unset()` removes a field. - `inc(n)` increments a numeric field. TypeScript offers `inc` and `mul` on numeric fields only. Applied to a missing field, `inc` sets the field to `n`. - `mul(n)` multiplies a numeric field. Applied to a missing field, it sets the field to `0` whatever `n` is. - After `variant(...)`, `u.duration.inc(10)` is a type error even though it runs correctly. Put `// @ts-expect-error` on the line directly above the line TypeScript flags. When the type error goes away, TypeScript reports the comment as unused, so delete the comment then. #### Examples [#examples-39] These examples use the MongoDB accessors from [Setting up the client](#setting-up-the-client). ##### `set()` (MongoDB) [#set-mongodb] Return as many operations as you want, and they are applied together, in one write. ```typescript const updated = await db.orm.users .where({ id: '6650f1c2a1b2c3d4e5f60002' }) .update((u) => [u.bio.set('Set via field op'), u.name.set('Bob R.')]); // A field inside the embedded `address` object, by dot path: const moved = await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60002' }).update((u) => [u('address.city').set('San Francisco')]); ``` ##### `inc()` and `mul()` (MongoDB) [#inc-and-mul-mongodb] `duration` is declared on `Tutorial`, not on the base `Post` model, so each `.update(...)` line that touches it needs its own `// @ts-expect-error` directly above it. ```typescript const incremented = await db.orm.posts .variant('tutorial') .where({ id: '6650f1c2a1b2c3d4e5f60003' }) // @ts-expect-error duration is a Tutorial field, not a Post field .update((u) => [u.duration.inc(10)]); const multiplied = await db.orm.posts .variant('tutorial') .where({ id: '6650f1c2a1b2c3d4e5f60003' }) // @ts-expect-error duration is a Tutorial field, not a Post field .update((u) => [u.duration.mul(3)]); ``` ##### `unset()` (MongoDB) [#unset-mongodb] ```typescript const updated = await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60001' }).update((u) => [u.bio.unset()]); const changed = await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60002' }).updateAndCount((u) => [u.bio.unset()]); ``` ### Operations that do not exist (MongoDB) [#operations-that-do-not-exist-mongodb] > [!WARNING] > No > > `min` > > , > > `max` > > , > > `rename` > > , or > > `currentDate` > > field operations > > The update builder implements eight operations, and only these eight: `set`, `unset`, `inc`, `mul`, `push`, `pull`, `addToSet`, and `pop`. `min`, `max`, `rename`, and `currentDate` are absent from the API, and TypeScript rejects them: `rename` is not a property of `u.bio`. ### Array operations (MongoDB) [#array-operations-mongodb-unverified] #### Remarks [#remarks-40] - MongoDB. `push()`, `pull()`, `addToSet()`, and `pop()` are available on the update builder. `push(value)` appends one element. `addToSet(value)` appends it only if it is not already there. `pop(1)` removes the last element, and `pop(-1)` removes the first. - `pull(match)` removes every element equal to the argument. If the array holds objects, pass part of an object to remove every element that matches it, as in `u.tags.pull({ kind: 'draft' })`. - These operations need an array field, declared with `[]` in your contract, as in `tags String[]`. Applied to any other field, the write fails. There is no `error.code` to match, because the error is MongoDB's own. - The four lines below are separate calls because one update cannot apply two operations to the same field. One update can still change two different fields, such as pushing to `tags` and setting `bio`. The example schema has no array field, so these four lines assume `tags String[]` on `User` and will not run against the schema on this page. ```typescript await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60001' }).update((u) => [u.tags.push('admin')]); await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60001' }).update((u) => [u.tags.addToSet('admin')]); await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60001' }).update((u) => [u.tags.pull('draft')]); await db.orm.users.where({ id: '6650f1c2a1b2c3d4e5f60001' }).update((u) => [u.tags.pop(1)]); ``` ## Result types [#result-types] ### Model and result types [#model-and-result-types] `contract.d.ts` exports a type for every model, under `Models`. On PostgreSQL the schema is part of the name: `Models.public_User`. The same file exports a `models` constant, so `typeof models.public.User` is the same type. A model type lists every scalar field and every relation, and no query returns exactly that, so three types derive what a query does return: - `Scalars` is the model without its relations. It is what `all()` and `first()` return when you do not call `select()` or `include()`. - `Shape` is a row with the fields and relations you choose. In `Spec`, `"+"` names the scalar fields and relations to keep, `"-"` names scalar fields to drop, and any other key is a relation with its own nested spec. If `"+"` names only relations, every scalar field stays. A relation named in `"+"` comes with all of its scalar fields and none of its own relations. - `ResultType` is one row of a query you have already written. Import `Scalars` and `Shape` from `@prisma/orm-postgres/family-contract/types`, or `@prisma/orm-mongo/family-contract` on MongoDB. Import `ResultType` from `@prisma/orm-postgres/components/runtime`. `MutationUpdateInput`, the type `update()` takes, comes from `@prisma/orm-postgres/orm-client`, since `8.0.0-rc.25`. A contract emitted before `8.0.0-rc.10` has no `Models`; run `npx prisma contract emit` again after upgrading. ```typescript import type { Models } from './contract.d'; import type { Scalars, Shape } from '@prisma/orm-postgres/family-contract/types'; import type { ResultType } from '@prisma/orm-postgres/components/runtime'; type UserRow = Scalars; // every scalar field of User, no posts and no tasks type UserResponse = Shape; // every scalar field of User except email, plus posts as an array of { id, title } const usersWithPosts = db.orm.public.User.include('posts'); type UserWithPosts = ResultType; // the same type as Shape ``` Use `Shape` to declare a function's return type once, and TypeScript checks the body at the `return`. A wrong field name, a relation under `"-"`, or a scalar field in `"+"` next to a `"-"` is a compile error that names that key. For Prisma ORM 7 users, these replace `User` and `Prisma.UserGetPayload<...>`. [Types for your code](https://www.prisma.io/docs/orm/fundamentals/types) shows them in functions, with the input and filter types and the Prisma ORM 7 name of each. ### `AsyncIterableResult` [#asynciterableresult] The methods `all()` / `createAll()` / `updateAll()` / `deleteAll()` return an `AsyncIterableResult`, which you can read all at once or one row at a time: - `await` the result (or call `.toArray()`) to get every row in one array. - Loop over it with `for await ... of` to take rows one at a time. `createAll()`, `updateAll()`, and `deleteAll()` exist on PostgreSQL as well as MongoDB, and [Write methods](#mutation-terminals) documents what each one takes, with examples. If you need the type by name, import it from `@prisma/orm-postgres/components/runtime` on PostgreSQL, or from `@prisma/orm-mongo/components/runtime` on MongoDB. #### Single consumption and mode switching [#single-consumption-and-mode-switching] Each result is read once, one way: - Re-`await`ing (or calling `.toArray()` on) a result you already awaited is **safe**. It returns the same array. - Looping a result a second time with `for await` throws an error whose `code` is `RUNTIME.ITERATOR_CONSUMED`. So does switching from one way to the other, such as awaiting a result and then looping it. The message contains `already been consumed`. To recognise the error, import `isRuntimeError` from `@prisma/orm-postgres/components/runtime` on PostgreSQL, or from `@prisma/orm-mongo/components/runtime` on MongoDB, and check `error.code`. PostgreSQL and MongoDB behave the same here, and the example below uses the PostgreSQL accessor. On MongoDB the same methods are on `db.orm.users`, see [Setting up the client](#setting-up-the-client). `all()` returns the result object straight away, and the `await` is what runs the query, so the example holds the result in a variable. ```typescript import { isRuntimeError } from '@prisma/orm-postgres/components/runtime'; const result = db.orm.public.User.all(); const first = await result; const again = await result.toArray(); // safe: same array try { for await (const user of result) { } } catch (error) { if (isRuntimeError(error) && error.code === 'RUNTIME.ITERATOR_CONSUMED') { // the result was already read as an array } } ``` ### Aggregate result shapes [#aggregate-result-shapes] - `aggregate((agg) => ({ ... }))` resolves to a **single object** keyed by your aliases. `agg` is the callback's argument, and it carries the aggregate functions. `db.orm.public.Order.aggregate((agg) => ({ total: agg.count() }))` resolves to one object, such as `{ total: 10 }`. - [Grouped aggregates](#grouped-aggregates) documents `aggregate()`, `groupBy()` with more than one field, and every function `agg` gives you. The bullets here cover only the shape of what you get back. - `groupBy(...).aggregate((agg) => ({ ... }))` resolves to an **array**, one object per group. Each object carries the fields you grouped by plus your aliases. `db.orm.public.Order.groupBy('customerId').aggregate((agg) => ({ orderCount: agg.count(), totalAmount: agg.sum('amount') }))` resolves to `[{ customerId, orderCount, totalAmount }, ...]`. - `count()` is always a `number` (`0` over an empty set). `sum()`, `avg()`, `min()`, and `max()` resolve to `null`, not `0`, over an empty result set. ## Related pages - [Error reference](https://www.prisma.io/docs/orm/reference/error-reference): The structured error codes of the Prisma ORM commands, runtime, and extensions, by namespace, with the condition that raises each one. - [Migration API](https://www.prisma.io/docs/orm/reference/migration-api): Every method and helper a migration.ts can use: the Migration class, the PostgreSQL operations, the column and constraint helpers, rawSql, and the MongoDB operations. - [Pipeline builder reference](https://www.prisma.io/docs/orm/reference/pipeline-builder): Reference for the Prisma ORM MongoDB pipeline builder's stages, accumulators, expression helpers, and write methods. - [Raw queries reference](https://www.prisma.io/docs/orm/reference/raw-queries): Reference for Prisma ORM raw queries: PostgreSQL raw SQL and MongoDB raw commands. - [SQL query builder reference](https://www.prisma.io/docs/orm/reference/sql-query-builder): Reference for the Prisma ORM SQL query builder's select, mutation, and grouped query methods.