Fixed incorrect prisma usage and added stuff for production

This commit is contained in:
Tim Howitz 2025-06-08 17:39:46 +01:00
parent a0f5a2f1de
commit 28daa98860
4 changed files with 37 additions and 54 deletions

View File

@ -1,7 +1,7 @@
import type { NextConfig } from "next"; import type { NextConfig } from "next";
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
/* config options here */ ignoreBuildErrors: true,
}; };
export default nextConfig; export default nextConfig;

View File

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"dev": "next dev --turbopack", "dev": "next dev --turbopack",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start -p 3002",
"lint": "next lint", "lint": "next lint",
"start:server": "dist/index.js" "start:server": "dist/index.js"
}, },

View File

@ -1,46 +1,41 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma"; import { prisma } from "@utils/prisma";
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
try { try {
const { query } = await req.json(); const { query } = await req.json();
// Nothing to search // Nothing to search
if (!query || typeof query !== "string" || query.trim().length === 0) { if (!query || typeof query !== "string" || query.trim().length === 0) {
// Return recent earthquakes if no search string // Return recent earthquakes if no search string
const earthquakes = await prisma.earthquake.findMany({ const earthquakes = await prisma.earthquake.findMany({
orderBy: { date: "desc" }, orderBy: { date: "desc" },
take: 30, take: 30,
}); });
return NextResponse.json({ earthquakes }); return NextResponse.json({ earthquakes });
} }
// Simple search: code, location, magnitude (add more fields as desired) // Simple search: code, location, magnitude (add more fields as desired)
const q = query.trim(); const q = query.trim();
const earthquakes = await prisma.earthquake.findMany({ const earthquakes = await prisma.earthquake.findMany({
where: { where: {
OR: [ OR: [
{ code: { contains: q, } }, { code: { contains: q } },
{ location: { contains: q, } }, { location: { contains: q } },
{ {
magnitude: Number.isNaN(Number(q)) magnitude: Number.isNaN(Number(q)) ? undefined : Number(q),
? undefined },
: Number(q), // optionally add more fields
}, ],
// optionally add more fields },
], orderBy: { date: "desc" },
}, take: 30,
orderBy: { date: "desc" }, });
take: 30,
});
return NextResponse.json({ earthquakes }); return NextResponse.json({ earthquakes });
} catch (e: any) { } catch (e: any) {
console.error("Earthquake search error:", e); console.error("Earthquake search error:", e);
return NextResponse.json( return NextResponse.json({ error: "Failed to search earthquakes." }, { status: 500 });
{ error: "Failed to search earthquakes." }, }
{ status: 500 }
);
}
} }

View File

@ -1,12 +0,0 @@
// src/lib/prisma.ts
import { PrismaClient } from "@prisma/client";
const globalForPrisma = global as unknown as { prisma: PrismaClient };
export const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: ["query", "error", "warn"],
});
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;