mirror of
https://gitlab.com/foxixus/neomovies-api.git
synced 2025-10-27 17:38:51 +05:00
small fixes and repair shit code
This commit is contained in:
83
src/db.js
83
src/db.js
@@ -12,51 +12,90 @@ let clientPromise;
|
|||||||
const clientOptions = {
|
const clientOptions = {
|
||||||
maxPoolSize: 10,
|
maxPoolSize: 10,
|
||||||
minPoolSize: 0,
|
minPoolSize: 0,
|
||||||
serverSelectionTimeoutMS: 5000,
|
serverSelectionTimeoutMS: 30000,
|
||||||
socketTimeoutMS: 45000,
|
socketTimeoutMS: 45000,
|
||||||
connectTimeoutMS: 30000,
|
connectTimeoutMS: 30000,
|
||||||
retryWrites: true,
|
retryWrites: true,
|
||||||
w: 'majority',
|
w: 'majority',
|
||||||
tlsAllowInvalidCertificates: true,
|
|
||||||
heartbeatFrequencyMS: 10000,
|
serverApi: {
|
||||||
minHeartbeatFrequencyMS: 500,
|
version: '1',
|
||||||
maxIdleTimeMS: 30000,
|
strict: true,
|
||||||
waitQueueTimeoutMS: 30000
|
deprecationErrors: true
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 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') {
|
||||||
if (!global._mongoClientPromise) {
|
if (!global._mongoClientPromise) {
|
||||||
client = new MongoClient(uri, clientOptions);
|
global._mongoClientPromise = initializeClient();
|
||||||
global._mongoClientPromise = client.connect();
|
|
||||||
}
|
}
|
||||||
clientPromise = global._mongoClientPromise;
|
clientPromise = global._mongoClientPromise;
|
||||||
} else {
|
} else {
|
||||||
client = new MongoClient(uri, clientOptions);
|
clientPromise = initializeClient();
|
||||||
clientPromise = client.connect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getDb() {
|
async function getDb() {
|
||||||
const _client = await clientPromise;
|
try {
|
||||||
return _client.db();
|
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;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting MongoDB database:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function closeConnection() {
|
async function closeConnection() {
|
||||||
if (client) {
|
if (client) {
|
||||||
await client.close();
|
try {
|
||||||
|
await client.close(true);
|
||||||
|
client = null;
|
||||||
|
global._mongoClientPromise = null;
|
||||||
|
console.log('MongoDB connection closed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error closing MongoDB connection:', error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { getDb, closeConnection };
|
// Clean up handlers
|
||||||
|
const cleanup = async () => {
|
||||||
process.on('SIGINT', async () => {
|
|
||||||
await closeConnection();
|
await closeConnection();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
});
|
};
|
||||||
|
|
||||||
process.on('SIGTERM', async () => {
|
process.on('SIGTERM', cleanup);
|
||||||
await closeConnection();
|
process.on('SIGINT', cleanup);
|
||||||
process.exit(0);
|
|
||||||
});
|
|
||||||
|
|
||||||
process.on('uncaughtException', async (err) => {
|
process.on('uncaughtException', async (err) => {
|
||||||
console.error('Uncaught Exception:', err);
|
console.error('Uncaught Exception:', err);
|
||||||
@@ -69,3 +108,5 @@ process.on('unhandledRejection', async (reason) => {
|
|||||||
await closeConnection();
|
await closeConnection();
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module.exports = { getDb, closeConnection };
|
||||||
|
|||||||
0
src/models/user.js
Normal file
0
src/models/user.js
Normal file
0
src/routes/auth.routes.js
Normal file
0
src/routes/auth.routes.js
Normal file
0
src/routes/session.routes.js
Normal file
0
src/routes/session.routes.js
Normal file
0
src/services/auth.service.js
Normal file
0
src/services/auth.service.js
Normal file
Reference in New Issue
Block a user