All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 27s
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { MongoClient } from "mongodb";
|
|
|
|
// MongoDB connection from environment or fallback
|
|
const uri =
|
|
process.env.MONGODB_URI ||
|
|
"mongodb://mo1124_ai_web:~4nfR5EA)3%C2%A3%40@mongo76.mydevil.net:27017/mo1124_ai_web?authSource=mo1124_ai_web";
|
|
|
|
console.log("[MongoDB] 🔧 Initializing connection...");
|
|
console.log(
|
|
"[MongoDB] 📍 URI:",
|
|
uri.replace(/:\/\/([^:]+):([^@]+)@/, "://<username>:<password>@"),
|
|
);
|
|
console.log("[MongoDB] 🌍 Environment:", process.env.NODE_ENV);
|
|
|
|
// Use global variable to preserve connection across HMR in development
|
|
let globalWithMongo = global as typeof globalThis & {
|
|
_mongoClientPromise?: Promise<MongoClient>;
|
|
};
|
|
|
|
let clientPromise: Promise<MongoClient>;
|
|
|
|
if (!globalWithMongo._mongoClientPromise) {
|
|
console.log("[MongoDB] 🔌 Creating new connection...");
|
|
const client = new MongoClient(uri);
|
|
globalWithMongo._mongoClientPromise = client
|
|
.connect()
|
|
.then((client) => {
|
|
console.log("[MongoDB] ✅ Connected successfully");
|
|
return client;
|
|
})
|
|
.catch((error) => {
|
|
console.error("[MongoDB] ❌ Connection failed:", error.message);
|
|
throw error;
|
|
});
|
|
} else {
|
|
console.log("[MongoDB] ♻️ Reusing existing connection");
|
|
}
|
|
|
|
clientPromise = globalWithMongo._mongoClientPromise;
|
|
|
|
export default clientPromise;
|