diff --git a/content/200-orm/100-prisma-schema/10-overview/index.mdx b/content/200-orm/100-prisma-schema/10-overview/index.mdx index 970bce8f80..ea948299d6 100644 --- a/content/200-orm/100-prisma-schema/10-overview/index.mdx +++ b/content/200-orm/100-prisma-schema/10-overview/index.mdx @@ -117,7 +117,7 @@ enum Role { ## Syntax -Prisma Schema files are written in Prisma Schema Language (PSL). See the [data sources](/orm/prisma-schema/overview/data-sources), [generators](/orm/prisma-schema/overview/generators), [data model definition**](/orm/prisma-schema/data-model) and of course [Prisma Schema API reference](/orm/reference/prisma-schema-reference) pages for details and examples. +Prisma Schema files are written in Prisma Schema Language (PSL). See the [data sources](/orm/prisma-schema/overview/data-sources), [generators](/orm/prisma-schema/overview/generators), [data model definition](/orm/prisma-schema/data-model) and of course [Prisma Schema API reference](/orm/reference/prisma-schema-reference) pages for details and examples. ### VS Code diff --git a/content/200-orm/200-prisma-client/100-queries/063-excluding-fields.mdx b/content/200-orm/200-prisma-client/100-queries/063-excluding-fields.mdx index c3762162b1..71a586ad84 100644 --- a/content/200-orm/200-prisma-client/100-queries/063-excluding-fields.mdx +++ b/content/200-orm/200-prisma-client/100-queries/063-excluding-fields.mdx @@ -4,15 +4,185 @@ metaTitle: 'Excluding fields' metaDescription: 'This page explains how to exclude sensitive fields from Prisma Client' --- - - By default Prisma Client returns all fields from a model. You can use `select` to narrow the result set, but that can be unwieldy if you have a large model and you only want to exclude one or two fields. -Prisma Client doesn't have a native way of excluding fields yet, but it's easy to create a function that you can use to exclude certain fields in a type-safe way. +:::info + +As of Prisma ORM 5.16.0, excluding fields globally and locally is supported via the `omitApi` Preview feature. + +::: + +## Excluding a field globally using `omit` + +The following is a type-safe way to exclude a field globally using the [`omitApi` Preview feature](/orm/reference/preview-features): + + + + + +```prisma +generator client { + provider = "prisma-client-js" + previewFeatures = ["omitApi"] +} + +model User { + id Int @id @default(autoincrement()) + firstName String + lastName String + email String @unique + password String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} +``` + + + + + +```tsx +const prisma = new PrismaClient({ + omit: { + user: { + password: true + } + } +}) + +// The password field is excluded in all queries, including this one +const user = await prisma.user.findUnique({ where: { id: 1 } }) +``` + + + + + +## Excluding a field locally using `omit` - +The following is a type-safe way to exclude a field locally using the `omitApi` Preview feature: -## Excluding the password field + + + + +```prisma +generator client { + provider = "prisma-client-js" + previewFeatures = ["omitApi"] +} + +model User { + id Int @id @default(autoincrement()) + firstName String + lastName String + email String @unique + password String + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} +``` + + + + + +```tsx +const prisma = new PrismaClient() + +// The password field is excluded only in this query +const user = await prisma.user.findUnique({ + omit: { + password: true + }, + where: { + id: 1 + } +}) +``` + + + + + +## How to omit multiple fields + +Omitting multiple fields works the same as selecting multiple fields: add multiple key-value pairs to the omit option. +Using the same schema as before, you could omit password and email with the following: + +```tsx +const prisma = new PrismaClient() + +// password and email are excluded +const user = await prisma.user.findUnique({ + omit: { + email: true, + password: true, + }, + where: { + id: 1, + }, +}) +``` + +Multiple fields can be omitted locally and globally. + +## How to select a previously omitted field + +If you [omit a field globally](#excluding-a-field-globally-using-omit), you can "override" by either selecting the field specifically or by setting `omit` to `false` in a query. + + + + + +```tsx +const user = await prisma.user.findUnique({ + select: { + firstName: true, + lastName: true, + password: true // The password field is now selected. + }, + where: { + id: 1 + } +}) +``` + + + + + +```tsx +const user = await prisma.user.findUnique({ + omit: { + password: false // The password field is now selected. + }, + where: { + id: 1 + } +}) +``` + + + + + +## When to use `omit` globally or locally + +It's important to understand when to omit a field globally or locally: + +- If you are omitting a field in order to prevent it from accidentally being included in a query, it's best to omit it globally. For example: Globally omitting the password field from a User model so that sensitive information doesn't accidentally get exposed. +- If you are omitting a field because it's not needed in a query, it's best to omit it locally. + +Local omit (when an `omit` option is provided in a query) only applies to the query it is defined in, while a global omit applies to every query made with the same Prisma Client instance, [unless a specific select is used or the omit is overridden](#how-to-select-a-previously-omitted-field). + +## Excluding the password field without using `omit` + +:::note + +The `omitApi` Preview feature, released in Prisma ORM 5.13.0, is the preferred way of omitting fields from a query result. The ability to globally omit fields was added to the `omitApi` Preview feature in Prisma ORM 5.16.0. This documentation is still relevant for versions of Prisma ORM prior to 5.13.0. + +::: The following is a type-safe `exclude` function returns a user without the `password` field. @@ -62,9 +232,3 @@ function main() { In the TypeScript example, we've provided two generics: `User` and `Key`. The `Key` generic is defined as the keys of a `User` (e.g. `email`, `password`, `firstName`, etc.). These generics flow through the logic, returning a `User` that omits the list of `Key`s provided. - -## Going further - -- Learn how you can move the `exclude` function into [a custom model](/orm/prisma-client/queries/custom-models). -- Instead of excluding fields, another option is to [obfuscate the field](https://github.com/prisma/prisma-client-extensions/tree/main/obfuscated-fields). -- There's an [outstanding feature request](https://github.com/prisma/prisma/issues/5042) to add exclude support natively in Prisma Client. If you'd like to see that happen, make sure to upvote that issue and share your use case! diff --git a/content/200-orm/200-prisma-client/500-deployment/550-deploy-database-changes-with-prisma-migrate.mdx b/content/200-orm/200-prisma-client/500-deployment/550-deploy-database-changes-with-prisma-migrate.mdx index fe736c8989..1a5faa315c 100644 --- a/content/200-orm/200-prisma-client/500-deployment/550-deploy-database-changes-with-prisma-migrate.mdx +++ b/content/200-orm/200-prisma-client/500-deployment/550-deploy-database-changes-with-prisma-migrate.mdx @@ -46,7 +46,7 @@ name: Deploy on: push: paths: - - ./prisma/migrations/** # Only run this workflow when migrations are updated + - prisma/migrations/** # Only run this workflow when migrations are updated branches: - main diff --git a/content/200-orm/500-reference/200-prisma-cli-reference.mdx b/content/200-orm/500-reference/200-prisma-cli-reference.mdx index 6dcad87fc3..7fd4523086 100644 --- a/content/200-orm/500-reference/200-prisma-cli-reference.mdx +++ b/content/200-orm/500-reference/200-prisma-cli-reference.mdx @@ -336,12 +336,14 @@ generator client { #### Options -| Option | Required | Description | Default | -| -------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | -| `--data-proxy` | No | The `generate` command will generate Prisma Client for use with [Prisma Accelerate](/accelerate) prior to Prisma 5.0.0. Mutually exclusive with `--accelerate` and `--no-engine`. | -| `--accelerate` | No | The `generate` command will generate Prisma Client for use with [Prisma Accelerate](/accelerate). Mutually exclusive with `--data-proxy` and `--no-engine`. Available in Prisma 5.1.0 and later. | -| `--no-engine` | No | The `generate` command will generate Prisma Client without an accompanied engine for use with [Prisma Accelerate](/accelerate). Mutually exclusive with `--data-proxy` and `--accelerate`. Available in Prisma ORM 5.2.0 and later. | -| `--watch` | No | The `generate` command will continue to watch the `schema.prisma` file and re-generate Prisma Client on file changes. | +| Option | Required | Description | Default | +| --------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | +| `--data-proxy` | No | The `generate` command will generate Prisma Client for use with [Prisma Accelerate](/accelerate) prior to Prisma 5.0.0. Mutually exclusive with `--accelerate` and `--no-engine`. | +| `--accelerate` | No | The `generate` command will generate Prisma Client for use with [Prisma Accelerate](/accelerate). Mutually exclusive with `--data-proxy` and `--no-engine`. Available in Prisma 5.1.0 and later. | +| `--no-engine` | No | The `generate` command will generate Prisma Client without an accompanied engine for use with [Prisma Accelerate](/accelerate). Mutually exclusive with `--data-proxy` and `--accelerate`. Available in Prisma ORM 5.2.0 and later. | +| `--no-hints` | No | The `generate` command will generate Prisma Client without usage hints being printed to the terminal. Available in Prisma ORM 5.16.0 and later. | +| `--allow-no-models` | No | The `generate` command will generate Prisma Client without generating any models. | +| `--watch` | No | The `generate` command will continue to watch the `schema.prisma` file and re-generate Prisma Client on file changes. |