mirror_match/lib/prisma.ts
ilia 9640627972
Some checks failed
CI / skip-ci-check (pull_request) Successful in 1m19s
CI / lint-and-type-check (pull_request) Failing after 1m37s
CI / test (pull_request) Successful in 2m16s
CI / build (pull_request) Failing after 1m46s
CI / secret-scanning (pull_request) Successful in 1m20s
CI / dependency-scan (pull_request) Successful in 1m27s
CI / sast-scan (pull_request) Successful in 2m29s
CI / workflow-summary (pull_request) Successful in 1m18s
feat: Add photo management features, duplicate detection, attempt limits, and admin deletion
- Add duplicate photo detection (file hash and URL checking)
- Add max attempts per photo with UI counter
- Simplify penalty system (auto-enable when points > 0)
- Prevent scores from going below 0
- Add admin photo deletion functionality
- Improve navigation with always-visible logout
- Prevent users from guessing their own photos
2026-01-02 14:57:30 -05:00

47 lines
1.7 KiB
TypeScript

import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import { Pool } from 'pg'
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined
}
const connectionString = process.env.DATABASE_URL
if (!connectionString) {
throw new Error('DATABASE_URL environment variable is not set')
}
// Handle Prisma Postgres URLs (prisma+postgres://) vs standard PostgreSQL URLs
let pool: Pool
if (connectionString.startsWith('prisma+postgres://')) {
// For Prisma managed Postgres, extract the actual postgres URL from the API key
try {
const urlMatch = connectionString.match(/api_key=([^"&]+)/)
if (urlMatch) {
const apiKey = decodeURIComponent(urlMatch[1])
const decoded = JSON.parse(Buffer.from(apiKey, 'base64').toString())
if (decoded.databaseUrl) {
pool = new Pool({ connectionString: decoded.databaseUrl })
} else if (decoded.shadowDatabaseUrl) {
pool = new Pool({ connectionString: decoded.shadowDatabaseUrl })
} else {
throw new Error('Could not extract database URL from Prisma Postgres connection string')
}
} else {
throw new Error('Invalid Prisma Postgres connection string format')
}
} catch (error) {
console.error('Error parsing Prisma Postgres URL:', error)
throw new Error('Failed to parse Prisma Postgres connection string. Consider using a standard PostgreSQL connection string.')
}
} else {
// Standard PostgreSQL connection
pool = new Pool({ connectionString })
}
const adapter = new PrismaPg(pool)
export const prisma = globalForPrisma.prisma ?? new PrismaClient({ adapter })
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma