mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-28 01:48:51 +05:00
impove db code
This commit is contained in:
47
src/db.js
47
src/db.js
@@ -26,49 +26,28 @@ const clientOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Connection management
|
// 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') {
|
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) {
|
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;
|
clientPromise = global._mongoClientPromise;
|
||||||
} else {
|
} 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() {
|
async function getDb() {
|
||||||
try {
|
try {
|
||||||
const _client = await clientPromise;
|
const mongoClient = await clientPromise;
|
||||||
const db = _client.db();
|
return mongoClient.db();
|
||||||
|
|
||||||
// Basic connection validation
|
|
||||||
if (!_client.topology || !_client.topology.isConnected()) {
|
|
||||||
console.warn('MongoDB connection lost, reconnecting...');
|
|
||||||
await _client.connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
return db;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error getting MongoDB database:', error);
|
console.error('Error getting MongoDB database:', error);
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const { Router } = require('express');
|
const { Router } = require('express');
|
||||||
const { getDb } = require('../db');
|
const { getDb } = require('../db');
|
||||||
const authRequired = require('../middleware/auth');
|
const authRequired = require('../middleware/auth');
|
||||||
console.log('typeof authRequired:', typeof authRequired, authRequired);
|
|
||||||
const fetch = global.fetch || require('node-fetch');
|
const fetch = global.fetch || require('node-fetch');
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|||||||
Reference in New Issue
Block a user