gongxiangp2/lib/prisma.ts
chenpei 4e89b57798 feat(upload): optimize upload UI and performance
- Expand batch upload list area and optimize preview image layout
- Improve upload speed tracking and display (server-side & client-side)
- Reduce network detection log noise
- Optimize Docker build process (caching, env validation)
- Add shadow environment deployment support
2026-01-28 15:07:50 +08:00

54 lines
1.8 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient } from '@prisma/client'
const globalForPrisma = global as unknown as { prisma: PrismaClient }
/**
* R9/R35修复:配置 Prisma 连接池
*
* 连接池参数说明:
* - connection_limit: 最大连接数(通过 DATABASE_URL 参数配置)
* - pool_timeout: 获取连接的超时时间
*
* 注意:核心连接池参数需要在 DATABASE_URL 中配置:
* DATABASE_URL="postgresql://...?connection_limit=10&pool_timeout=30"
*/
const prismaClientOptions = {
// 生产环境只记录错误和警告,开发环境记录所有查询
log: process.env.NODE_ENV === 'production'
? (['error', 'warn'] as ('error' | 'warn')[])
: (['query', 'error', 'warn'] as ('query' | 'error' | 'warn')[]),
}
export const prisma =
globalForPrisma.prisma ||
new PrismaClient(prismaClientOptions)
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
// R47修复:在运行时验证环境变量(不在构建时验证)
if (typeof window === 'undefined') {
// 只在服务器端运行时验证
const { validateEnvironment } = require('./env-validation')
// 跳过验证的条件:
// 1. Next.js 构建时(NEXT_BUILD 环境变量存在)
// 2. 非生产环境(开发/测试)
const isBuildTime = process.env.NEXT_BUILD === '1'
const isNotProduction = process.env.NODE_ENV !== 'production'
if (!isBuildTime) {
try {
validateEnvironment()
} catch (error) {
console.error('[Prisma] ❌ 环境变量验证失败')
// 只在真正的生产运行时抛出错误
if (!isNotProduction) {
throw error
}
console.warn('[Prisma] ⚠️ 非 production 环境,跳过环境变量验证错误')
}
} else {
console.log('[Prisma] ℹ️ 构建时跳过环境变量验证')
}
}