From 780c808cfcb43cf5678c42afb75ba85f955a417e Mon Sep 17 00:00:00 2001 From: ruheni Date: Wed, 25 Oct 2023 14:13:10 +0200 Subject: [PATCH] add serverless driver section --- content/300-guides/050-database/890-neon.mdx | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/content/300-guides/050-database/890-neon.mdx b/content/300-guides/050-database/890-neon.mdx index eb8800f4fc..93f8dfdbbe 100644 --- a/content/300-guides/050-database/890-neon.mdx +++ b/content/300-guides/050-database/890-neon.mdx @@ -99,3 +99,56 @@ Another possible cause of connection timeouts is [Prisma's connection pool](http ```text DATABASE_URL=postgres://daniel:@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) . A driver adapter allows you to use a different database driver, from the default Prisma provides, to communicate with your database. + + + +This feature is available from Prisma versions 5.4.2 and later. + + + +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.