-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
35 lines (26 loc) · 797 Bytes
/
db.js
File metadata and controls
35 lines (26 loc) · 797 Bytes
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
import { MongoClient,ServerApiVersion } from 'mongodb'
import dotenv from "dotenv"
dotenv.config()
const url = process.env.DATABASE_PASSWORD
const client = new MongoClient(url, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
export async function connectToMongoDB(name) {
try {
await client.connect();
console.log('Connected to MongoDB');
let db = client.db(name);//database reference
console.log(`Connected to database ${name}`)
return db;
} catch (errors) {
console.error('Error connecting to MongoDB', errors)
}
}
export async function closeConnection() {
await client.close();
console.log('Disconnected from MongoDB');
}