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
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:
@@ -32,6 +32,7 @@ const originalEnv = process.env
|
|||||||
|
|
||||||
describe('MongoDB Connection (Unit Tests)', () => {
|
describe('MongoDB Connection (Unit Tests)', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
jest.resetModules()
|
||||||
process.env = {
|
process.env = {
|
||||||
...originalEnv,
|
...originalEnv,
|
||||||
MONGODB_URI: 'mongodb://localhost:27017/test',
|
MONGODB_URI: 'mongodb://localhost:27017/test',
|
||||||
@@ -58,9 +59,8 @@ describe('MongoDB Connection (Unit Tests)', () => {
|
|||||||
// Clear module cache to force re-import
|
// Clear module cache to force re-import
|
||||||
jest.resetModules()
|
jest.resetModules()
|
||||||
|
|
||||||
expect(() => {
|
const { getClientPromise } = require('./mongodb')
|
||||||
require('./mongodb')
|
expect(() => getClientPromise()).toThrow('Please add MONGODB_URI to your environment variables')
|
||||||
}).toThrow('Please add MONGODB_URI to your environment variables')
|
|
||||||
|
|
||||||
// Restore environment
|
// Restore environment
|
||||||
process.env.MONGODB_URI = originalMongodbUri
|
process.env.MONGODB_URI = originalMongodbUri
|
||||||
@@ -70,9 +70,9 @@ describe('MongoDB Connection (Unit Tests)', () => {
|
|||||||
describe('Database connection', () => {
|
describe('Database connection', () => {
|
||||||
it('should create MongoClient with correct URI and options', async () => {
|
it('should create MongoClient with correct URI and options', async () => {
|
||||||
// Import after setting up the mock
|
// Import after setting up the mock
|
||||||
const { clientPromise } = await import('./mongodb')
|
const { getClientPromise } = await import('./mongodb')
|
||||||
|
|
||||||
await clientPromise
|
await getClientPromise()
|
||||||
|
|
||||||
expect(MockedMongoClient).toHaveBeenCalledWith(
|
expect(MockedMongoClient).toHaveBeenCalledWith(
|
||||||
process.env.MONGODB_URI,
|
process.env.MONGODB_URI,
|
||||||
@@ -131,10 +131,10 @@ describe('MongoDB Connection (Unit Tests)', () => {
|
|||||||
|
|
||||||
describe('Connection reuse and caching', () => {
|
describe('Connection reuse and caching', () => {
|
||||||
it('should reuse the same MongoClient instance for multiple calls', async () => {
|
it('should reuse the same MongoClient instance for multiple calls', async () => {
|
||||||
const { clientPromise } = await import('./mongodb')
|
const { getClientPromise } = await import('./mongodb')
|
||||||
|
|
||||||
const client1 = await clientPromise
|
const client1 = await getClientPromise()
|
||||||
const client2 = await clientPromise
|
const client2 = await getClientPromise()
|
||||||
|
|
||||||
// Should be the same client instance
|
// Should be the same client instance
|
||||||
expect(client1).toBe(client2)
|
expect(client1).toBe(client2)
|
||||||
|
|||||||
+24
-22
@@ -1,39 +1,41 @@
|
|||||||
import { MongoClient, Db, Collection, Document } from 'mongodb'
|
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 = {
|
const options = {
|
||||||
maxPoolSize: 10,
|
maxPoolSize: 10,
|
||||||
serverSelectionTimeoutMS: 5000,
|
serverSelectionTimeoutMS: 5000,
|
||||||
socketTimeoutMS: 45000,
|
socketTimeoutMS: 45000,
|
||||||
}
|
}
|
||||||
|
|
||||||
let client: MongoClient
|
let clientPromise: Promise<MongoClient> | undefined
|
||||||
let clientPromise: Promise<MongoClient>
|
|
||||||
|
|
||||||
// In development mode, use a global variable so that the client is not recreated between hot reloads
|
function createClientPromise(): Promise<MongoClient> {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
const uri = process.env.MONGODB_URI
|
||||||
// @ts-expect-error - Global variable for development hot reload
|
if (!uri) {
|
||||||
if (!global._mongoClientPromise) {
|
throw new Error('Please add MONGODB_URI to your environment variables')
|
||||||
client = new MongoClient(uri, options)
|
|
||||||
// @ts-expect-error - Global variable for development hot reload
|
|
||||||
global._mongoClientPromise = client.connect()
|
|
||||||
}
|
}
|
||||||
// @ts-expect-error - Global variable for development hot reload
|
|
||||||
clientPromise = global._mongoClientPromise
|
return new MongoClient(uri, options).connect()
|
||||||
} else {
|
|
||||||
// In production mode, it's best to not use a global variable
|
|
||||||
client = new MongoClient(uri, options)
|
|
||||||
clientPromise = client.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> {
|
export async function getDb(): Promise<Db> {
|
||||||
const client = await clientPromise
|
const client = await getClientPromise()
|
||||||
return client.db(process.env.MONGODB_DB || 'mozdit')
|
return client.db(process.env.MONGODB_DB || 'mozdit')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user