Vollstaendige Next.js/Prisma-E-Commerce-Anwendung fuer eine Imkerei: Shop, Warenkorb, Stripe-Checkout, Kundenkonto mit 2FA, Admin-Bereich (Produkte, Ernten, Chargen, Bestellungen, Gutscheine, admin-editierbare Website-Inhalte), i18n (DE/EN), Rate-Limiting/CSP/Audit-Log-Haertung, PostgreSQL- und MySQL-Unterstuetzung ueber austauschbare Prisma- Treiber-Adapter, Unit-/Komponenten-/E2E-Tests.
38 lines
1.6 KiB
TypeScript
38 lines
1.6 KiB
TypeScript
import { PrismaClient } from "@/generated/prisma/client";
|
|
import { PrismaClient as MySqlPrismaClient } from "@/generated/prisma-mysql/client";
|
|
import { createDbAdapter } from "@/lib/db-adapter";
|
|
|
|
// Verhindert im Next.js Dev-Modus (Hot Reload) das mehrfache Erzeugen von
|
|
// PrismaClient-Instanzen und damit zu viele offene DB-Verbindungen.
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
// Waehlt den Treiber-Adapter (Postgres oder MySQL) anhand von
|
|
// DATABASE_PROVIDER — siehe lib/db-adapter.ts.
|
|
const adapter = createDbAdapter();
|
|
const logLevels = process.env.NODE_ENV === "development" ? (["warn", "error"] as const) : (["error"] as const);
|
|
|
|
/**
|
|
* Ein einzelner generierter Prisma-Client ist fest an EINEN Provider
|
|
* gebunden — deshalb generiert scripts/generate-prisma-clients.mjs beide
|
|
* Varianten ins Image, und hier wird zur Laufzeit anhand von
|
|
* DATABASE_PROVIDER zwischen ihnen gewaehlt (ein Docker-Image, umschaltbar
|
|
* per Env-Variable statt Neubau). Der MySQL-Client hat dieselbe oeffentliche
|
|
* Form wie der Postgres-Client (identisches schema.prisma), der Cast ist nur
|
|
* noetig, weil beide als separate, nicht strukturell identische
|
|
* TS-Klassen generiert werden.
|
|
*/
|
|
function instantiateClient(): PrismaClient {
|
|
if ((process.env.DATABASE_PROVIDER ?? "postgresql").trim() === "mysql") {
|
|
return new MySqlPrismaClient({ adapter, log: [...logLevels] }) as unknown as PrismaClient;
|
|
}
|
|
return new PrismaClient({ adapter, log: [...logLevels] });
|
|
}
|
|
|
|
export const prisma: PrismaClient = globalForPrisma.prisma ?? instantiateClient();
|
|
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalForPrisma.prisma = prisma;
|
|
}
|