Skip to content

Commit

Permalink
update planetscale docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni committed Oct 24, 2023
1 parent 28c2f5f commit 2327a61
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion content/200-concepts/100-components/drivers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To use this feature:

## Serverless drivers

By default, JavaScript database drivers use TCP connections. Several database providers enable use of different protocols such as HTTP and WebSocket to connect to your database such as Neon, PlanetScale and Turso.
By default, JavaScript database drivers use TCP connections. Several database providers enable use of different protocols, such as HTTP and WebSocket, to connect to your database. Example database providers include Neon, PlanetScale and Turso.

Prisma officially adapters for the following serverless database providers:

Expand Down
58 changes: 58 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,64 @@ 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 means of accessing your database and executing queries over HTTP. Prisma by default uses TCP to access your database and execute queries. You can use Prisma along with the PlanetScale driver using a [driver adapter](/concepts/components/drivers) <span class="concept"></span>. A driver adapter allows you to use a different database driver, from the default Prisma provides, to access your database.

<Admonition>

This feature is available 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
```

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'
```

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

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

> Prisma ORM supports Node 16 and up. In Node 18 and up, `undici` is not needed.
Update your Prisma Client instance to use the PlanetScale database 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 as you normally would.

## 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

0 comments on commit 2327a61

Please sign in to comment.