Created
December 10, 2025 00:14
-
-
Save tuffs/0272351bb98bcaaf9a6e42caee3dc6f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To run: `node ./neonPrismaSetup.js` | |
| * This script assumes TypeScript is being used, adjust accordingly | |
| */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Check if .env exists and contains DATABASE_URL | |
| const envPath = path.join(__dirname, '.env'); | |
| if (!fs.existsSync(envPath)) { | |
| console.error('Error: .env file not found. Please create it with DATABASE_URL.'); | |
| process.exit(1); | |
| } | |
| const envContent = fs.readFileSync(envPath, 'utf-8'); | |
| if (!envContent.includes('DATABASE_URL=')) { | |
| console.error('Error: DATABASE_URL not found in .env.'); | |
| process.exit(1); | |
| } | |
| // Template for prisma.ts | |
| const prismaTsTemplate = `import { PrismaClient } from "@prisma/client"; | |
| import { PrismaNeon } from "@prisma/adapter-neon"; | |
| import { neonConfig } from "@neondatabase/serverless"; | |
| neonConfig.webSocketConstructor = WebSocket; | |
| const connectionString = process.env.DATABASE_URL!; | |
| // PrismaNeon accepts a connection configuration object; pass the | |
| // connectionString directly to avoid incompatible Pool types. | |
| const adapter = new PrismaNeon({ connectionString }); | |
| const globalForPrisma = globalThis as unknown as { | |
| prisma: PrismaClient | undefined; | |
| }; | |
| export const prisma = | |
| globalForPrisma.prisma ?? | |
| new PrismaClient({ | |
| adapter, | |
| log: ["query"], | |
| }); | |
| if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma; | |
| `; | |
| // Ensure prisma directory exists | |
| const prismaDir = path.join(__dirname, 'prisma'); | |
| if (!fs.existsSync(prismaDir)) { | |
| fs.mkdirSync(prismaDir); | |
| } | |
| // Write prisma.ts | |
| const prismaTsPath = path.join(prismaDir, 'prisma.ts'); | |
| fs.writeFileSync(prismaTsPath, prismaTsTemplate); | |
| console.log('Prisma Neon setup complete. Run npx prisma generate next.'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment