Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ To start using TypedSQL in your Prisma project, follow these steps:

```prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["typedSql"]
provider = "prisma-client" // or "prisma-client-js"
previewFeatures = ["typedSql"]
output = "../src/generated/prisma"
}
```

:::tip[Using driver adapters with TypedSQL]

If you are deploying Prisma in serverless or edge environments, you can use [driver adapters](/orm/overview/databases/database-drivers#driver-adapters) to connect through JavaScript database drivers. Driver adapters are compatible with TypedSQL, with the exception of `@prisma/adapter-better-sqlite3`. For SQLite support, use [`@prisma/adapter-libsql`](https://www.npmjs.com/package/@prisma/adapter-libsql) instead. All other driver adapters are supported.
Expand All @@ -37,6 +37,23 @@ To start using TypedSQL in your Prisma project, follow these steps:
mkdir -p prisma/sql
```

:::note[Custom SQL folder location]

Starting with Prisma 6.12.0, you can configure a custom location for your SQL files using the Prisma config file. Create a `prisma.config.ts` file in your project root and specify the `typedSql.path` option:

```typescript title="prisma.config.ts"
import { defineConfig } from 'prisma/config'

export default defineConfig({
schema: './prisma/schema.prisma',
typedSql: {
path: './prisma/sql',
},
})
```

:::

1. Create a new `.sql` file in your `prisma/sql` directory. For example, `getUsersWithPosts.sql`. Note that the file name must be a valid JS identifier and cannot start with a `$`.

1. Write your SQL queries in your new `.sql` file. For example:
Expand Down Expand Up @@ -68,16 +85,22 @@ To start using TypedSQL in your Prisma project, follow these steps:

1. Now you can import and use your SQL queries in your TypeScript code:

```typescript title="/src/index.ts"
import { PrismaClient } from '@prisma/client'
import { getUsersWithPosts } from '@prisma/client/sql'
```typescript title="/src/index.ts"
import { PrismaClient } from './generated/prisma/client'
import { getUsersWithPosts } from './generated/prisma/sql'

const prisma = new PrismaClient()
const prisma = new PrismaClient()

const usersWithPostCounts = await prisma.$queryRawTyped(getUsersWithPosts())
console.log(usersWithPostCounts)
```

:::note

If you do not customize the generator `output`, you can import from `@prisma/client` and `@prisma/client/sql` instead.

:::

## Passing Arguments to TypedSQL Queries

To pass arguments to your TypedSQL queries, you can use parameterized queries. This allows you to write flexible and reusable SQL statements while maintaining type safety. Here's how to do it:
Expand Down Expand Up @@ -122,9 +145,9 @@ To pass arguments to your TypedSQL queries, you can use parameterized queries. T

1. When using the generated function in your TypeScript code, pass the arguments as additional parameters to `$queryRawTyped`:

```typescript title="/src/index.ts"
import { PrismaClient } from '@prisma/client'
import { getUsersByAge } from '@prisma/client/sql'
```typescript title="/src/index.ts"
import { PrismaClient } from './generated/prisma/client'
import { getUsersByAge } from './generated/prisma/sql'

const prisma = new PrismaClient()

Expand All @@ -147,8 +170,8 @@ WHERE id = ANY($1)
```

```typescript title="/src/index.ts"
import { PrismaClient } from '@prisma/client'
import { getUsersByIds } from '@prisma/client/sql'
import { PrismaClient } from './generated/prisma/client'
import { getUsersByIds } from './generated/prisma/sql'

const prisma = new PrismaClient()

Expand Down Expand Up @@ -208,7 +231,7 @@ Manual argument type definitions are not supported for array arguments. For thes

## Examples

For practical examples of how to use TypedSQL in various scenarios, please refer to the [Prisma Examples repo](https://github.com/prisma/prisma-examples). This repo contains a collection of ready-to-run Prisma example projects that demonstrate best practices and common use cases, including TypedSQL implementations.
For practical examples of how to use TypedSQL, please refer to the [TypedSQL example in the Prisma Examples repo](https://github.com/prisma/prisma-examples/tree/latest/generator-prisma-client/basic-typedsql).

## Limitations of TypedSQL

Expand Down
Loading