PrismaP
Prisma3w ago
1 reply
real chupacabra 😈

Using Prisma with NeonDB in serverless env

I am trying to use prisma v7 with neon (will later host on vercel) and I'm wondering if I'm doing things the right way.
here's my current prisma.ts file:
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "../app/generated/prisma/client";

const globalForPrisma = global as unknown as {
    prisma: PrismaClient;
};

const adapter = new PrismaNeon({
    connectionString: process.env.DATABASE_URL,
});

const prisma =
    globalForPrisma.prisma ||
    new PrismaClient({
        adapter,
    });

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export default prisma;

I have also come across this code snippet from https://neon.com/docs/guides/prisma#use-the-neon-serverless-driver-with-prisma

but I have no idea if I need this or not
import 'dotenv/config';
import { PrismaClient } from '@prisma/client';
import { PrismaNeon } from '@prisma/adapter-neon';
import { neonConfig } from '@neondatabase/serverless';

import ws from 'ws';
neonConfig.webSocketConstructor = ws;

// To work in edge environments (Cloudflare Workers, Vercel Edge, etc.), enable querying over fetch
// neonConfig.poolQueryViaFetch = true

// Type definitions
// declare global {
//   var prisma: PrismaClient | undefined
// }

const connectionString = `${process.env.DATABASE_URL}`;

const adapter = new PrismaNeon({ connectionString });
const prisma = global.prisma || new PrismaClient({ adapter });

if (process.env.NODE_ENV === 'development') global.prisma = prisma;

export default prisma;

they also used previewFeatures = ["driverAdapters"] here but idk if that's a thing in primsa v7 becuse the neon prisma docs are pre v7
Neon
Prisma is an open source, next generation ORM that lets you to manage and interact with your database. This guide covers the following topics Connect to Neon from Prisma Use connection pooling with Pr...
Connect from Prisma to Neon - Neon Docs
Was this page helpful?