45 lines
1.8 KiB
TypeScript
45 lines
1.8 KiB
TypeScript
import { PrismaClient } from "@/generated/prisma/client";
|
|
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
|
import { buildMysqlPoolConfig } from "./mysql-pool-config";
|
|
|
|
/**
|
|
* Single Prisma Client instance for the whole app.
|
|
*
|
|
* Prisma 7 requires a driver adapter instead of a `datasource.url` in the
|
|
* schema (see prisma/schema.prisma). This is the ONLY file that imports a
|
|
* database driver directly — every other file goes through `prisma`.
|
|
*
|
|
* Migrated from PostgreSQL (`@prisma/adapter-pg`) to MySQL in Phase 7 — see
|
|
* docs/phase-7-summary.md for the full record. `@prisma/adapter-mariadb` is
|
|
* Prisma's one official driver adapter for BOTH MySQL and MariaDB (there is
|
|
* no separate "adapter-mysql" package): the underlying `mariadb` npm driver
|
|
* speaks the MySQL wire protocol and is Prisma's documented, supported way
|
|
* to connect to a genuine MySQL server. The package name is not a sign
|
|
* we're accidentally targeting MariaDB — `datasource.provider` in
|
|
* schema.prisma is `"mysql"`, and every environment this app runs against
|
|
* (local Docker, CI, and the documented production target) is real MySQL.
|
|
*
|
|
* See mysql-pool-config.ts for why the pool is built from a parsed object
|
|
* rather than passed the raw connection string — the short version is that
|
|
* the UTC-timezone handling it sets is load-bearing, not cosmetic.
|
|
*/
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
const adapter = new PrismaMariaDb(buildMysqlPoolConfig());
|
|
|
|
export const prisma =
|
|
globalForPrisma.prisma ??
|
|
new PrismaClient({
|
|
adapter,
|
|
log: process.env.NODE_ENV === "development" ? ["warn", "error"] : ["error"],
|
|
});
|
|
|
|
// Reuse the client across hot-reloads in dev so we don't exhaust the
|
|
// connection pool every time a file changes.
|
|
if (process.env.NODE_ENV !== "production") {
|
|
globalForPrisma.prisma = prisma;
|
|
}
|