Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { PrismaClient } from '@prisma/client';
import { PrismaPg } from '@prisma/adapter-pg';

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};

export const prisma = globalForPrisma.prisma ?? new PrismaClient({
log: ['error'],
errorFormat: 'pretty',
});
const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
throw new Error('DATABASE_URL is not set');
}

// Prisma 7 connects through a driver adapter instead of a schema-level `url`.
// PrismaPg manages its own pg connection pool from the connection string.
const adapter = new PrismaPg({ connectionString });

export const prisma =
globalForPrisma.prisma ??
new PrismaClient({
adapter,
log: ['error'],
errorFormat: 'pretty',
});

if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prisma;
}
}
Loading