import { PrismaClient } from "@prisma/client"; declare global { var prisma: PrismaClient | undefined; } export const prisma = global.prisma || new PrismaClient({ log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"], }); if (process.env.NODE_ENV !== "production") global.prisma = prisma; /** * caloriesBurned is in the DB schema but NOT in the generated Prisma client. * These helpers use raw SQL to read/write it until Prisma client can be regenerated. */ export async function getCaloriesBurned(workoutId: string): Promise { const rows = await prisma.$queryRawUnsafe>( `SELECT caloriesBurned FROM Workout WHERE id = ?`, workoutId ); return rows[0]?.caloriesBurned ?? null; } export async function setCaloriesBurned(workoutId: string, calories: number | null): Promise { await prisma.$executeRawUnsafe( `UPDATE Workout SET caloriesBurned = ? WHERE id = ?`, calories, workoutId ); } export async function getCaloriesBurnedBulk(workoutIds: string[]): Promise> { if (workoutIds.length === 0) return {}; const placeholders = workoutIds.map(() => "?").join(","); const rows = await prisma.$queryRawUnsafe>( `SELECT id, caloriesBurned FROM Workout WHERE id IN (${placeholders})`, ...workoutIds ); const map: Record = {}; for (const r of rows) { map[r.id] = r.caloriesBurned; } return map; }