This repository has been archived on 2024-04-19. You can view files and clone it, but cannot push or open issues or pull requests.
brief-04-back/services/MongodbService.js
2024-04-17 16:55:04 +02:00

28 lines
842 B
JavaScript

const {MongoClient} = require('mongodb')
const { Logger } = require('tslog')
/**
* Establishes a connection to a MongoDB server using the MongoClient.
*
* @returns {MongoClient} A Promise that resolves with a MongoDB client object connected to the server.
* The client object can be used to perform database operations.
* @throws {Error} If an error occurs while attempting to connect to the MongoDB server.
*/
async function connect() {
try {
return await MongoClient.connect("mongodb://127.0.0.1:27017/")
} catch (err) {
throw new Error(err)
}
}
async function getDatabase(name) {
const {connect} = require("./MongodbService")
const client = await connect()
//console.log(client)
const db = client.db(name);
return db;
}
module.exports = {connect, getDatabase}