refactor: replace Prisma ORM runtime with Drizzle ORM facade
- Replace Prisma client runtime with Drizzle ORM (zero Prisma engine/query engine in production) - Add Prisma-compatible facade (@/lib/prisma-facade.ts) backed by Drizzle for backwards compatibility - Runtime queries route through Drizzle ORM; @prisma/client is now devDependency (types only) - Remove @prisma/adapter-mariadb dependency; delete prisma-pool.ts and types/prisma.ts - New Drizzle schema layer: src/db/schema.ts (176 tables) and src/lib/db.ts (connection) - Update README documenting the dual-layer ORM architecture - Restore src/generated/ gitignore (build artifact for local type generation) - 0 TypeScript errors, 583 tests passing The facade intentionally uses `any` types to match the Prisma Client API surface, allowing existing code to run unmodified while routing queries through Drizzle at runtime.
This commit is contained in:
1 parent
9d1c71d926
commit
56061e41d4
52 files changed
+5825
-748
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { drizzle, type MySql2Database } from "drizzle-orm/mysql2";
|
||||
import mysql from "mysql2/promise";
|
||||
import * as relations from "@/db/relations";
|
||||
import * as schema from "@/db/schema";
|
||||
import { env } from "@/env";
|
||||
import { resolveConnectionLimit } from "@/lib/db-pool";
|
||||
|
||||
const fullSchema = { ...schema, ...relations };
|
||||
|
||||
const globalForDb = globalThis as unknown as {
|
||||
db?: MySql2Database<typeof fullSchema>;
|
||||
};
|
||||
|
||||
function createDb(): MySql2Database<typeof fullSchema> {
|
||||
const url = new URL(env.DATABASE_URL);
|
||||
const isBuild =
|
||||
process.env.NEXT_PHASE === "phase-production-build" ||
|
||||
process.env.NEXT_PHASE === "phase-production-compile";
|
||||
const connectionLimit = resolveConnectionLimit(env.DATABASE_POOL_SIZE);
|
||||
// Fail fast during SSG so Next can retry/fallback instead of hanging 30s+.
|
||||
const connectTimeout = isBuild
|
||||
? Math.min(env.DATABASE_CONNECT_TIMEOUT_MS, 5_000)
|
||||
: env.DATABASE_CONNECT_TIMEOUT_MS;
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: url.hostname,
|
||||
port: Number(url.port) || 3306,
|
||||
user: decodeURIComponent(url.username),
|
||||
password: decodeURIComponent(url.password),
|
||||
database: url.pathname.replace(/^\//, ""),
|
||||
charset: "utf8mb4",
|
||||
waitForConnections: true,
|
||||
connectionLimit,
|
||||
queueLimit: 0,
|
||||
connectTimeout,
|
||||
idleTimeout: env.DATABASE_IDLE_TIMEOUT_MS,
|
||||
// Keep BIGINT precision; safe values come back as numbers, huge ones as
|
||||
// strings (drizzle bigint mode maps both to JS bigint).
|
||||
supportBigNumbers: true,
|
||||
});
|
||||
|
||||
return drizzle(pool, {
|
||||
schema: fullSchema,
|
||||
mode: "default",
|
||||
logger:
|
||||
env.NODE_ENV === "development"
|
||||
? {
|
||||
logQuery(query, params) {
|
||||
console.log("[db]", query, params);
|
||||
},
|
||||
}
|
||||
: false,
|
||||
});
|
||||
}
|
||||
|
||||
export const db = globalForDb.db ?? createDb();
|
||||
|
||||
if (env.NODE_ENV !== "production") globalForDb.db = db;
|
||||
|
||||
export type Db = MySql2Database<typeof fullSchema>;
|
||||
|
||||
export * from "@/db/schema";
|
||||
Reference in new issue
Block a user