server/services/database/CONNECTOR.js
Artem Anufrij 9d0272351d
All checks were successful
continuous-integration/drone/push Build is passing
wip
2023-09-28 13:00:46 +02:00

28 lines
838 B
JavaScript

const MongoClient = require('mongodb').MongoClient;
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)
const client = await MongoClient.connect(url, (err, db)=> {
console.log("DB Connected")
}
);
dbo = client.db(database);
return dbo;
} catch (error) {
console.error(`MongoDB connection failed with > ${error}`);
throw error;
}
}
}