Skip to content

Commit

Permalink
Merge branch 'main' into add-neon-to-prisma-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni authored Nov 3, 2023
2 parents a43cac7 + 4ccc901 commit 8cec49f
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"backoff",
"Replibyte",
"Snaplet",
"Kysely"
"Kysely",
"Turso"
],
"ignoreWords": [
"Ania",
Expand Down
54 changes: 54 additions & 0 deletions content/200-concepts/100-components/database-drivers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: 'Database drivers'
metaTitle: 'Database drivers'
metaDescription: 'Learn how Prisma connects to your database using the built-in drivers and how you can use Prisma along with other JavaScript database drivers using driver adapters (Preview)'
tocDepth: 4
---

## Default built-in drivers

One of Prisma Client's components is the [Query Engine](./prisma-engines/query-engine) <span class="concept"></span>. The Query Engine is responsible for transforming Prisma Client queries to SQL statements. The Query Engine connects to your database using the included drivers that don't require additional setup. The built-in drivers use TCP connections to connect to the database.

![Query flow from the user application to the database with Prisma Client](./images/drivers/qe-query-execution-flow.png)

## Driver adapters

Prisma Client can connect and run queries against your database using JavaScript database drivers using **driver adapters**. Adapters act as _translators_ between Prisma Client and the JavaScript database driver.

Prisma will use the Query Engine to transform the Prisma Client query to SQL and run the generated SQL queries via the JavaScript database driver.

![Query flow from the user application to the database using Prisma Client and driver adapters](./images//drivers/qe-query-engine-adapter.png)

### Serverless driver adapters

- [Neon](/guides/database/neon#how-to-use-the-neon-serverless-driver-with-prisma-preview)
- [PlanetScale](/guides/database/planetscale#how-to-use-the-planetscale-serverless-driver-with-prisma-preview)

### Database driver adapters

- [Turso](/guides/database/turso#connect-and-query-your-primary-database)

### How to use driver adapters

To use this feature:

1. Update the `previewFeatures` block in your schema to include the the `driverAdapters` preview feature:

```prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
```

2. Generate Prisma Client:

```sh
npx prisma generate
```

3. Refer to the following pages to learn more how to use the specific driver adapters with the specific database providers:

- [Neon](/guides/database/neon#how-to-use-the-neon-serverless-driver-with-prisma-preview)
- [PlanetScale](/guides/database/planetscale#how-to-use-the-planetscale-serverless-driver-with-prisma-preview)
- [Turso](/guides/database/turso#connect-and-query-your-primary-database)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions content/300-guides/050-database/850-planetscale.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,74 @@ model User {

For a more detailed example, see the [Getting Started guide for PlanetScale](/getting-started/setup-prisma/add-to-existing-project/relational-databases/introspection-typescript-planetscale).

## How to use the PlanetScale serverless driver with Prisma (Preview)

The [PlanetScale serverless driver](https://planetscale.com/docs/tutorials/planetscale-serverless-driver) provides a way of communicating with your database and executing queries over HTTP.

You can use Prisma along with the PlanetScale serverless driver using the [`@prisma/adapter-planetscale`](https://www.npmjs.com/package/@prisma/adapter-planetscale) driver adapter. The driver adapter allows you to communicate with your database over HTTP.

<Admonition>

This feature is available in Preview from Prisma versions 5.4.2 and later.

</Admonition>

To get started, enable the `driverAdapters` Preview feature flag:

```prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
```

Generate Prisma Client:

```sh
npx prisma generate
```

<Admonition>

Ensure you update the host value in your connection string to `aws.connect.psdb.cloud`. You can learn more about this [here](https://planetscale.com/docs/tutorials/planetscale-serverless-driver#add-and-use-the-planetscale-serverless-driver-for-javascript-to-your-project).

```bash
DATABASE_URL='mysql://johndoe:[email protected]/clear_nightsky?sslaccept=strict'
```

</Admonition>

Install the Prisma adapter for PlanetScale, PlanetScale serverless driver and `undici` packages:

```sh
npm install @prisma/adapter-planetscale @planetscale/database undici
```

<Admonition>

When using a Node.js version below 18, you must provide a custom fetch function implementation. We recommend the `undici` package on which Node's built-in fetch is based. Node.js versions 18 and later include a built-in global `fetch` function, so you don't have to install an extra package.

</Admonition>

Update your Prisma Client instance to use the PlanetScale serverless driver:

```ts
import { connect } from '@planetscale/database'
import { PrismaPlanetScale } from '@prisma/adapter-planetscale'
import { PrismaClient } from '@prisma/client'
import dotenv from 'dotenv'
import { fetch as undiciFetch } from 'undici'

dotenv.config()
const connectionString = `${process.env.DATABASE_URL}`

const connection = connect({ url: connectionString, fetch: undiciFetch })
const adapter = new PrismaPlanetScale(connection)
const prisma = new PrismaClient({ adapter })
```

You can then use Prisma Client as you normally would with full type-safety. Prisma Migrate, introspection, and Prisma Studio will continue working as before using the connection string defined in the Prisma schema.

## More on using PlanetScale with Prisma

The fastest way to start using PlanetScale with Prisma is to refer to our Getting Started documentation:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,36 @@ const prisma = new PrismaClient({
})
```

### <inlinecode>adapter</inlinecode>

Defines an instance of a [driver adapter](/concepts/components/drivers#driver-adapters). See also [Database drivers](/concepts/components/drivers) <span class="concept"></span>.

<Admonition>

This is available from version 5.4.0 and newer behind the `driverAdapters` feature flag.

</Admonition>

#### Example

The example below uses the [Neon driver adapter](/guides/database/neon#how-to-use-the-neon-serverless-driver-with-prisma-preview)

```ts
import { Pool, neonConfig } from '@neondatabase/serverless'
import { PrismaNeon } from '@prisma/adapter-neon'
import { PrismaClient } from '@prisma/client'
import dotenv from 'dotenv'
import ws from 'ws'

dotenv.config()
neonConfig.webSocketConstructor = ws
const connectionString = `${process.env.DATABASE_URL}`

const pool = new Pool({ connectionString })
const adapter = new PrismaNeon(pool)
const prisma = new PrismaClient({ adapter })
```

### <inlinecode>rejectOnNotFound</inlinecode>

<Admonition type="info">
Expand Down

0 comments on commit 8cec49f

Please sign in to comment.