Skip to content

Commit

Permalink
s/writing-custom-sql/using-raw-sql/g
Browse files Browse the repository at this point in the history
  • Loading branch information
jharrell committed Aug 1, 2024
1 parent 59a5195 commit 9da06a2
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ _Alternatives_: [Hasura](https://hasura.io/), [Postgraphile](https://www.graphil

### ... you want to use raw, type-safe SQL for querying your database

While Prisma ORM does allow you to [send plain SQL queries](/orm/prisma-client/writing-custom-sql/raw-queries) to your database, it might not be the best fit if you prefer to work with a SQL-based abstraction that you want to be type-safe. Prisma ORM's main benefit is to provide an abstraction layer that makes you more productive compared to writing SQL.
While Prisma ORM does allow you to [send plain SQL queries](/orm/prisma-client/using-raw-sql/raw-queries) to your database, it might not be the best fit if you prefer to work with a SQL-based abstraction that you want to be type-safe. Prisma ORM's main benefit is to provide an abstraction layer that makes you more productive compared to writing SQL.

If you're a solo developer that is very comfortable with SQL, and you just want to be sure that your database layer is type-safe, a lower-level TypeScript database library might be better for you.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ location Unsupported("POLYGON")?

The `Unsupported` type allows you to define fields in the Prisma schema for database types that are not yet supported by Prisma ORM. For example, MySQL's `POLYGON` type is not currently supported by Prisma ORM, but can now be added to the Prisma schema using the `Unsupported("POLYGON")` type.

Fields of type `Unsupported` are not available in the generated Prisma Client API, but you can still use Prisma ORM's [raw database access](/orm/prisma-client/writing-custom-sql/raw-queries) feature to query these fields.
Fields of type `Unsupported` are not available in the generated Prisma Client API, but you can still use Prisma ORM's [raw database access](/orm/prisma-client/using-raw-sql/raw-queries) feature to query these fields.

> **Note**: If a model has **mandatory `Unsupported` fields**, the generated client will not include `create` or `update` methods for that model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,6 @@ Some databases support materialized views, e.g. [PostgreSQL](https://www.postgre

Materialized views persist the result of the view query for faster access and only update it on demand.

Currently Prisma ORM has no understanding of materialized views, but when you [manually create a view](#create-a-view-in-the-underlying-database) you can also create a materialized view by using the corresponding command in the underlying database. You can then use Prisma Client's [TypedSQL functionality](/orm/prisma-client/writing-custom-sql) to execute the command to refresh the view manually.
Currently Prisma ORM has no understanding of materialized views, but when you [manually create a view](#create-a-view-in-the-underlying-database) you can also create a materialized view by using the corresponding command in the underlying database. You can then use Prisma Client's [TypedSQL functionality](/orm/prisma-client/using-raw-sql) to execute the command to refresh the view manually.

In the future Prisma Client might support marking individual views as materialized and add a Prisma Client method to refresh the materialized view. Please comment on our [`views` feedback issue](https://github.com/prisma/prisma/issues/17335) with your use case.
2 changes: 1 addition & 1 deletion content/200-orm/200-prisma-client/100-queries/030-crud.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ await prisma.$transaction([deleteProfile, deletePosts, deleteUsers])
#### Deleting all data with raw SQL / `TRUNCATE`

If you are comfortable working with raw SQL you can perform a `TRUNCATE` on a table by utilizing [`$executeRawUnsafe`](/orm/prisma-client/writing-custom-sql/raw-queries#executerawunsafe).
If you are comfortable working with raw SQL you can perform a `TRUNCATE` on a table by utilizing [`$executeRawUnsafe`](/orm/prisma-client/using-raw-sql/raw-queries#executerawunsafe).

In the following examples, the first tab shows how to perform a `TRUNCATE` on a Postgres database by using a `$queryRaw` look up that maps over the table and `TRUNCATES` all tables in a single query.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ However, if you try to search on `title` alone, the search will fail with the er

## Full-text search with raw SQL

Full-text search is currently in Preview and due to a [known issue](https://github.com/prisma/prisma/issues/23627), you may be seeing slow search queries. If that's the case, you can optimize your query using [TypedSQL](/orm/prisma-client/writing-custom-sql).
Full-text search is currently in Preview and due to a [known issue](https://github.com/prisma/prisma/issues/23627), you may be seeing slow search queries. If that's the case, you can optimize your query using [TypedSQL](/orm/prisma-client/using-raw-sql).

### PostgreSQL

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tocDepth: 3

:::warning

With Prisma ORM 5.18.0, we have released [TypedSQL](/orm/prisma-client/writing-custom-sql). TypedSQL is a new way to write type-safe SQL queries that are type-safe and even easier to add to your workflow. We strongly recommend using TypedSQL queries over the legacy raw queries described in this document whenever possible. The following information is maintained for backward compatibility and specific use cases where TypedSQL may not be applicable.
With Prisma ORM 5.18.0, we have released [TypedSQL](/orm/prisma-client/using-raw-sql). TypedSQL is a new way to write type-safe SQL queries that are type-safe and even easier to add to your workflow. We strongly recommend using TypedSQL queries over the legacy raw queries described in this document whenever possible. The following information is maintained for backward compatibility and specific use cases where TypedSQL may not be applicable.

:::

Expand Down Expand Up @@ -702,7 +702,7 @@ console.log(result)
#### Using `$queryRawUnsafe` and `$executeRawUnsafe` unsafely
If you cannot use tagged templates, you can instead use [`$queryRawUnsafe`](/orm/prisma-client/writing-custom-sql/raw-queries#queryrawunsafe) or [`$executeRawUnsafe`](/orm/prisma-client/writing-custom-sql/raw-queries#executerawunsafe) but **be aware that your these functions make it much more likely that your code will be vulnerable to SQL injection**.
If you cannot use tagged templates, you can instead use [`$queryRawUnsafe`](/orm/prisma-client/using-raw-sql/raw-queries#queryrawunsafe) or [`$executeRawUnsafe`](/orm/prisma-client/using-raw-sql/raw-queries#executerawunsafe) but **be aware that your these functions make it much more likely that your code will be vulnerable to SQL injection**.
The following example concatenates `query` and `inputString`. Prisma Client ❌ **cannot** escape `inputString` in this example, which makes it vulnerable to SQL injection:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ This may happen for several reasons, e.g., because you need to optimize the perf

In these cases, you can use one of Prisma Client's advanced querying features via `$queryRaw` or `$executeRaw`, to send raw queries to the database.

In most cases, [TypedSQL](#writing-type-safe-queries-with-prisma-client-and-typedsql) will allow you to express your query in SQL while still leveraging Prisma Client's excellent user experience. However, since TypedSQL is statically typed, there are some cases (such as dynamically generated `WHERE` clauses) that it can't represent. For these cases, you will need to use [`$queryRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#queryraw) or [`$executeRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#executeraw) or their unsafe counterparts.
In most cases, [TypedSQL](#writing-type-safe-queries-with-prisma-client-and-typedsql) will allow you to express your query in SQL while still leveraging Prisma Client's excellent user experience. However, since TypedSQL is statically typed, there are some cases (such as dynamically generated `WHERE` clauses) that it can't represent. For these cases, you will need to use [`$queryRaw`](/orm/prisma-client/using-raw-sql/raw-queries#queryraw) or [`$executeRaw`](/orm/prisma-client/using-raw-sql/raw-queries#executeraw) or their unsafe counterparts.

## Writing type-safe queries with Prisma Client and TypedSQL

:::info

TypedSQL is available in Prisma ORM 5.18.0 and later. For raw database access in previous versions, see [our raw queries documentation](/orm/prisma-client/writing-custom-sql/raw-queries).
TypedSQL is available in Prisma ORM 5.18.0 and later. For raw database access in previous versions, see [our raw queries documentation](/orm/prisma-client/using-raw-sql/raw-queries).

:::

Expand All @@ -40,7 +40,7 @@ TypedSQL is particularly useful for:

By using TypedSQL, you can write efficient, type-safe database queries without sacrificing the power and flexibility of raw SQL. This feature allows you to seamlessly integrate custom SQL queries into your Prisma-powered applications, ensuring type safety and improving developer productivity.

For a detailed guide on how to get started with TypedSQL, including setup instructions and usage examples, please refer to our [TypedSQL documentation](/orm/prisma-client/writing-custom-sql/typedsql).
For a detailed guide on how to get started with TypedSQL, including setup instructions and usage examples, please refer to our [TypedSQL documentation](/orm/prisma-client/using-raw-sql/typedsql).

## Raw queries

Expand All @@ -52,20 +52,20 @@ While not as ergonomic as [TypedSQL](#writing-type-safe-queries-with-prisma-clie

Prisma ORM supports four methods to execute raw SQL queries in relational databases:

- [`$queryRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#queryraw)
- [`$executeRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#executeraw)
- [`$queryRawUnsafe`](/orm/prisma-client/writing-custom-sql/raw-queries#queryrawunsafe)
- [`$executeRawUnsafe`](/orm/prisma-client/writing-custom-sql/raw-queries#executerawunsafe)
- [`$queryRaw`](/orm/prisma-client/using-raw-sql/raw-queries#queryraw)
- [`$executeRaw`](/orm/prisma-client/using-raw-sql/raw-queries#executeraw)
- [`$queryRawUnsafe`](/orm/prisma-client/using-raw-sql/raw-queries#queryrawunsafe)
- [`$executeRawUnsafe`](/orm/prisma-client/using-raw-sql/raw-queries#executerawunsafe)

These commands are similar to using TypedSQL, but they are not type-safe and are written as strings in your code rather than in dedicated `.sql` files.

### Legacy raw queries in document databases

For MongoDB, Prisma ORM supports three methods to execute raw queries:

- [`$runCommandRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#runcommandraw)
- [`<model>.findRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#findraw)
- [`<model>.aggregateRaw`](/orm/prisma-client/writing-custom-sql/raw-queries#aggregateraw)
- [`$runCommandRaw`](/orm/prisma-client/using-raw-sql/raw-queries#runcommandraw)
- [`<model>.findRaw`](/orm/prisma-client/using-raw-sql/raw-queries#findraw)
- [`<model>.aggregateRaw`](/orm/prisma-client/using-raw-sql/raw-queries#aggregateraw)

These methods allow you to execute raw MongoDB commands and queries, providing flexibility when you need to use MongoDB-specific features or optimizations.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const prisma = new PrismaClient().$extends({
})
```

In the event a [raw query](/orm/prisma-client/writing-custom-sql/raw-queries) is invoked, the `model` argument passed to the callback will be `undefined`.
In the event a [raw query](/orm/prisma-client/using-raw-sql/raw-queries) is invoked, the `model` argument passed to the callback will be `undefined`.

For example, you can use the `$allOperations` method to log queries as follows:

Expand Down
14 changes: 7 additions & 7 deletions content/200-orm/500-reference/050-prisma-client-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1641,11 +1641,11 @@ const groupUsers = await prisma.user.groupBy({

### `findRaw()`

See: [Raw database access (`findRaw()`)](/orm/prisma-client/writing-custom-sql/raw-queries#findraw).
See: [Raw database access (`findRaw()`)](/orm/prisma-client/using-raw-sql/raw-queries#findraw).

### `aggreagateRaw()`

See: [Raw database access (`aggreagateRaw()`)](/orm/prisma-client/writing-custom-sql/raw-queries#aggregateraw).
See: [Raw database access (`aggreagateRaw()`)](/orm/prisma-client/using-raw-sql/raw-queries#aggregateraw).

## Model query options

Expand Down Expand Up @@ -5411,23 +5411,23 @@ See [middleware examples](/orm/prisma-client/client-extensions/middleware#sample

### `$queryRaw`

See: [Raw database access (`$queryRaw`)](/orm/prisma-client/writing-custom-sql/raw-queries#queryraw).
See: [Raw database access (`$queryRaw`)](/orm/prisma-client/using-raw-sql/raw-queries#queryraw).

### `$queryRawUnsafe()`

See: [Raw database access (`$queryRawUnsafe()`)](/orm/prisma-client/writing-custom-sql/raw-queries#queryrawunsafe).
See: [Raw database access (`$queryRawUnsafe()`)](/orm/prisma-client/using-raw-sql/raw-queries#queryrawunsafe).

### `$executeRaw`

See: [Raw database access (`$executeRaw`)](/orm/prisma-client/writing-custom-sql/raw-queries#executeraw).
See: [Raw database access (`$executeRaw`)](/orm/prisma-client/using-raw-sql/raw-queries#executeraw).

### `$executeRawUnsafe()`

See: [Raw database access (`$executeRawUnsafe()`)](/orm/prisma-client/writing-custom-sql/raw-queries#executerawunsafe).
See: [Raw database access (`$executeRawUnsafe()`)](/orm/prisma-client/using-raw-sql/raw-queries#executerawunsafe).

### `$runCommandRaw()`

See: [Raw database access (`$runCommandRaw()`)](/orm/prisma-client/writing-custom-sql/raw-queries#runcommandraw).
See: [Raw database access (`$runCommandRaw()`)](/orm/prisma-client/using-raw-sql/raw-queries#runcommandraw).

### `$transaction()`

Expand Down
Loading

0 comments on commit 9da06a2

Please sign in to comment.