import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from "./schema";
import * as relations from "./relations";

// Global cache to prevent connection exhaustion in development
const globalForDb = globalThis as unknown as {
  conn: mysql.Pool | undefined;
};

const poolConnection = globalForDb.conn ?? mysql.createPool({
  uri: process.env.DATABASE_URL,
  waitForConnections: true,
  connectionLimit: 10, // 根据数据库规格调整
  queueLimit: 0
});

if (process.env.NODE_ENV !== "production") globalForDb.conn = poolConnection;

export const db = drizzle(poolConnection, { 
  schema: { ...schema, ...relations }, 
  mode: "default" 
});
