From c324d67d206f3458f03cf110d9a7ba450df69776 Mon Sep 17 00:00:00 2001 From: Foxix Date: Tue, 8 Jul 2025 16:56:03 +0300 Subject: [PATCH] impove db code --- src/db.js | 47 ++++++++++++----------------------------- src/routes/reactions.js | 2 +- 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/db.js b/src/db.js index ddc7bf3..27d32b4 100644 --- a/src/db.js +++ b/src/db.js @@ -26,49 +26,28 @@ const clientOptions = { }; // Connection management -async function initializeClient() { - try { - client = new MongoClient(uri, clientOptions); - - // Minimal essential monitoring - client.on('serverHeartbeatFailed', (error) => { - console.error('MongoDB server heartbeat failed:', error); - }); - - client.on('connectionPoolCleared', () => { - console.warn('MongoDB connection pool cleared'); - }); - - await client.connect(); - console.log('MongoDB connection established'); - return client; - } catch (error) { - console.error('Failed to initialize MongoDB client:', error); - throw error; - } -} - if (process.env.NODE_ENV === 'development') { + // In development mode, use a global variable so that the value + // is preserved across module reloads caused by HMR (Hot Module Replacement). if (!global._mongoClientPromise) { - global._mongoClientPromise = initializeClient(); + client = new MongoClient(uri, clientOptions); + global._mongoClientPromise = client.connect(); + console.log('MongoDB connection initialized in development'); } clientPromise = global._mongoClientPromise; } else { - clientPromise = initializeClient(); + // In production mode, it's best to not use a global variable. + client = new MongoClient(uri, clientOptions); + clientPromise = client.connect(); + console.log('MongoDB connection initialized in production'); } + + async function getDb() { try { - const _client = await clientPromise; - const db = _client.db(); - - // Basic connection validation - if (!_client.topology || !_client.topology.isConnected()) { - console.warn('MongoDB connection lost, reconnecting...'); - await _client.connect(); - } - - return db; + const mongoClient = await clientPromise; + return mongoClient.db(); } catch (error) { console.error('Error getting MongoDB database:', error); throw error; diff --git a/src/routes/reactions.js b/src/routes/reactions.js index d72da35..9d07aed 100644 --- a/src/routes/reactions.js +++ b/src/routes/reactions.js @@ -1,7 +1,7 @@ const { Router } = require('express'); const { getDb } = require('../db'); const authRequired = require('../middleware/auth'); -console.log('typeof authRequired:', typeof authRequired, authRequired); + const fetch = global.fetch || require('node-fetch'); const router = Router();