Исправление: Prisma singleton pattern + повторная генерация перед сборкой
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { sendEmailNotification } from '@/lib/email'
|
||||
import { sendTelegramNotification } from '@/lib/telegram'
|
||||
|
||||
export const dynamic = "force-dynamic"
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
|
||||
22
nextjs_space/lib/prisma.ts
Normal file
22
nextjs_space/lib/prisma.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
// Singleton pattern для Prisma Client
|
||||
// Использует lazy initialization - клиент создается только при первом использовании
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined
|
||||
}
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ??
|
||||
new PrismaClient({
|
||||
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
||||
})
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
|
||||
|
||||
// Graceful shutdown
|
||||
if (typeof window === 'undefined') {
|
||||
process.on('beforeExit', async () => {
|
||||
await prisma.$disconnect()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user