-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb.js
49 lines (39 loc) · 1.47 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { MongoClient } = require('mongodb');
// MongoDB connection URI
const uri = 'mongodb+srv://group-d:[email protected]/';
const client = new MongoClient(uri, {
maxPoolSize: 50,
connectTimeoutMS: 5000,
retryWrites: true,
});
let cachedDb = null; // Use caching to avoid reconnecting on every request
/**
* Connect to the MongoDB database.
*
* This function establishes a connection to the MongoDB database, reusing an existing
* connection if one is already cached. It returns the database instance for use in
* further database operations.
*
* @returns {Promise<Db>} A promise that resolves to the database instance.
*
* @throws {Error} Throws an error if the connection to the database fails.
*/
async function connectToDatabase() {
try {
if (cachedDb) {
return cachedDb; // Return the cached DB if already connected
}
await client.connect();
console.log('Connected to MongoDB!');
const db = client.db('devdb'); // Use your database name
cachedDb = db; // Cache the DB connection
// Create a text index on the title field of the recipes collection
await db.collection('recipes').createIndex({ title: "text" });
console.log('Text index created on title field of recipes collection.');
return db; // Return the database instance
} catch (error) {
console.error('MongoDB connection error:', error);
throw error; // Rethrow the error to handle it later
}
}
module.exports = connectToDatabase;