Исправление: Mock Prisma Client во время сборки Next.js

This commit is contained in:
DeepAgent
2025-10-28 09:22:59 +00:00
parent e1c9abc796
commit e0abec6215
3 changed files with 43 additions and 13 deletions

View File

@@ -1,22 +1,51 @@
import { PrismaClient } from '@prisma/client'
// Singleton pattern для Prisma Client
// Использует lazy initialization - клиент создается только при первом использовании
// НАСТОЯЩАЯ 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'],
})
let prismaInstance: PrismaClient | undefined
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
export function getPrisma(): PrismaClient {
// Проверяем, находимся ли мы в процессе сборки
const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build'
if (isBuildTime) {
// Во время сборки возвращаем mock объект, который не делает реальных запросов
console.log('⚠️ Build time detected, returning mock Prisma client')
return {
$connect: async () => {},
$disconnect: async () => {},
contactSubmission: {
create: async () => ({ id: 1, name: 'mock', phone: 'mock', email: null, serviceType: null, message: null, createdAt: new Date() }),
findMany: async () => [],
}
} as any
}
// Graceful shutdown
if (typeof window === 'undefined') {
process.on('beforeExit', async () => {
await prisma.$disconnect()
})
// Runtime: используем настоящий клиент
if (!prismaInstance) {
if (globalForPrisma.prisma) {
prismaInstance = globalForPrisma.prisma
} else {
prismaInstance = new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
})
if (process.env.NODE_ENV !== 'production') {
globalForPrisma.prisma = prismaInstance
}
}
}
return prismaInstance
}
// Для обратной совместимости экспортируем через Proxy
export const prisma = new Proxy({} as PrismaClient, {
get(target, prop) {
return (getPrisma() as any)[prop]
}
})