@prisma/adapter-neon
Prisma driver adapter for Neon Serverless Driver.
See https://github.com/prisma/prisma/releases/tag/5.4.0 and https://www.prisma.io/blog/serverless-database-drivers-KML1ehXORxZV for details.
The following usage tutorial is valid for Prisma 5.4.2 and later versions.
How to install
After creating your database on Neon, you'll need to install the @prisma/adapter-neon
driver adapter, Neon’s serverless database driver @neondatabase/serverless
, and ws
to set up a WebSocket connection for use by Neon.
npm install @prisma/adapter-neon
npm install @neondatabase/serverless
npm install ws
Make sure your Neon database connection string is copied over to your .env
file. The connection string will start with postgres://
.
# .env
DATABASE_URL="postgres://..."
Make sure you also include the driverAdapters
Preview feature in your schema.prisma
.
// schema.prisma
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Now run npx prisma generate
to re-generate Prisma Client.
How to use
In TypeScript, you will need to:
- Import packages
- Set up the Neon serverless database driver
- Instantiate the Prisma Neon adapter with the Neon serverless database driver
- Pass the driver adapter to the Prisma Client instance
import { Pool, neonConfig } from '@neondatabase/serverless';
import { PrismaNeon } from '@prisma/adapter-neon';
import { PrismaClient } from '@prisma/client';
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
const connectionString = `${process.env.DATABASE_URL}`;
const pool = new Pool({ connectionString });
const adapter = new PrismaNeon(pool);
const prisma = new PrismaClient({ adapter });
Now your code has built-in benefits of the Neon serverless driver, such as WebSocket connections and message pipelining, while Prisma covers connection creation and destruction, error handling, and type safety. If you have any feedback about our Neon Serverless Driver support, please leave a comment on our dedicated GitHub issue and we'll use it as we continue development.