Skip to content

Commit

Permalink
add serverless driver section
Browse files Browse the repository at this point in the history
  • Loading branch information
ruheni committed Oct 25, 2023
1 parent d82757b commit 780c808
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions content/300-guides/050-database/890-neon.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,56 @@ Another possible cause of connection timeouts is [Prisma's connection pool](http
```text
DATABASE_URL=postgres://daniel:<password>@ep-mute-rain-952417.us-east-2.aws.neon.tech/neondb?connect_timeout=15&pool_timeout=15`
```

## How to use Neon's serverless driver with Prisma (Preview)

The [Neon serverless driver](https://github.com/neondatabase/serverless) is a low-latency Postgres driver for JavaScript and TypeScript that allows you to query data from serverless and edge environments over HTTP or WebSockets in place of TCP. Prisma by default uses TCP.

You can use Prisma along with the Neon serverless 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 communicate with 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
```

Install the Prisma adapter for Neon, Neon serverless driver and ws packages:

```sh
npm install @prisma/adapter-neon @neondatabase/serverless ws
```

Update your Prisma Client instance:

```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 })
```

You can then use Prisma Client as you normally would with full-type-safety.

0 comments on commit 780c808

Please sign in to comment.