fix: defer MongoDB connection until runtime
CI Pipeline with Test Management / 🧪 Run Tests & Generate Reports (push) Waiting to run
CI Pipeline with Test Management / 🐳 Docker Integration Tests (push) Blocked by required conditions
CI Pipeline with Test Management / 🏗️ Build Docker Image (push) Blocked by required conditions
CI Pipeline with Test Management / 📊 Generate Test Summary (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / 🧪 Run Tests & Generate Reports (push) Waiting to run
Test Reporting & Gherkin Analysis / 📊 Analyze Test Coverage (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / 🔄 Sync with Linear (push) Blocked by required conditions
Test Reporting & Gherkin Analysis / ⚡ Performance Monitoring (push) Blocked by required conditions

This commit is contained in:
Do Siki
2026-08-17 17:00:41 +02:00
parent 82124f3d2b
commit 2c9d3d01f1
2 changed files with 33 additions and 31 deletions
+24 -22
View File
@@ -1,39 +1,41 @@
import { MongoClient, Db, Collection, Document } from 'mongodb'
if (!process.env.MONGODB_URI) {
throw new Error('Please add MONGODB_URI to your environment variables')
}
const uri = process.env.MONGODB_URI
const options = {
maxPoolSize: 10,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
}
let client: MongoClient
let clientPromise: Promise<MongoClient>
let clientPromise: Promise<MongoClient> | undefined
// In development mode, use a global variable so that the client is not recreated between hot reloads
if (process.env.NODE_ENV === 'development') {
// @ts-expect-error - Global variable for development hot reload
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options)
// @ts-expect-error - Global variable for development hot reload
global._mongoClientPromise = client.connect()
function createClientPromise(): Promise<MongoClient> {
const uri = process.env.MONGODB_URI
if (!uri) {
throw new Error('Please add MONGODB_URI to your environment variables')
}
// @ts-expect-error - Global variable for development hot reload
clientPromise = global._mongoClientPromise
} else {
// In production mode, it's best to not use a global variable
client = new MongoClient(uri, options)
clientPromise = client.connect()
return new MongoClient(uri, options).connect()
}
export default clientPromise
export function getClientPromise(): Promise<MongoClient> {
// In development mode, use a global variable so that the client is not recreated between hot reloads.
// The connection is intentionally created lazily: `next build` must not require runtime secrets.
if (process.env.NODE_ENV === 'development') {
// @ts-expect-error - Global variable for development hot reload
if (!global._mongoClientPromise) {
// @ts-expect-error - Global variable for development hot reload
global._mongoClientPromise = createClientPromise()
}
// @ts-expect-error - Global variable for development hot reload
return global._mongoClientPromise
}
clientPromise ??= createClientPromise()
return clientPromise
}
export async function getDb(): Promise<Db> {
const client = await clientPromise
const client = await getClientPromise()
return client.db(process.env.MONGODB_DB || 'mozdit')
}