server/services/database/CONNECTOR.js

28 lines
838 B
JavaScript
Raw Normal View History

2023-09-28 13:00:46 +02:00
const MongoClient = require('mongodb').MongoClient;
2023-02-08 12:30:56 +01:00
const server = require("../../server");
const config = server.config;
var dbo;
const url = "mongodb://" + config.database.host + ":" + config.database.port + "/";
const database = config.database.name;
exports.connect = async function () {
if (dbo) {
console.log("DB CONNECTED")
return dbo;
}
else {
try {
console.log("DB CONNECTING:" + config.database.host + ":" + config.database.port)
2023-09-28 13:00:46 +02:00
const client = await MongoClient.connect(url, (err, db)=> {
console.log("DB Connected")
}
);
2023-02-08 12:30:56 +01:00
dbo = client.db(database);
return dbo;
} catch (error) {
console.error(`MongoDB connection failed with > ${error}`);
throw error;
}
}
}