You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
// This file establishes conenction to the mongodb. Connection is used by the applicaiton and initialized
|
|
// when it loads during starup . This is registered in the root level index.js
|
|
|
|
// require mongoose module
|
|
const fp = require("fastify-plugin");
|
|
const db = require("mongoose");
|
|
db.set("useFindAndModify", false);
|
|
// require clak module to give colors to console text.
|
|
var chalk = require("chalk");
|
|
|
|
const boom = require("boom");
|
|
|
|
|
|
// Define colors for db messages on cosole.
|
|
|
|
var connected = chalk.bold.cyan;
|
|
var error = chalk.bold.yellow;
|
|
var disconnected = chalk.bold.red;
|
|
var termination = chalk.bold.magenta;
|
|
|
|
// TODO
|
|
// Need to read database url from a environment variable.
|
|
// const databaseURL = "mongodb://armintatankdbuser:armintatank1@35.207.198.4:27017/arminta-tank-db;
|
|
//const databaseURLNew = "mongodb://35.207.198.4:27017/arminta-tank-db";
|
|
//const databaseURLNew = "mongodb://35.200.129.165:27017/health-care-db";
|
|
|
|
const databaseURLNew = "mongodb://127.0.0.1:27017/arminta-tank-db";
|
|
// Next line not used , or no need to pass default db in the mongo connection url.
|
|
const defaultDatabase = "arminta-tank-db";
|
|
|
|
//export this function and imported by server.js
|
|
|
|
|
|
async function dbConnection() {
|
|
try {
|
|
db.connect(databaseURLNew, { user: "armintatankdbuser", pass: "armintatank1" , useUnifiedTopology: true, });
|
|
//db.connect(databaseURLNew, { user: "healthcaredbuser01", pass: "healthcare01" , useUnifiedTopology: true, });
|
|
|
|
// db.connect(databaseURLNew)
|
|
db.connection.on("connected", function () {
|
|
console.log(
|
|
connected("Mongoose default connection is open to ", databaseURLNew)
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
db.connection.on("error", function (err) {
|
|
console.log(
|
|
error("Mongoose default connection has occured " + err + " error")
|
|
);
|
|
});
|
|
|
|
db.connection.on("disconnected", function () {
|
|
console.log(disconnected("Mongoose default connection is disconnected"));
|
|
});
|
|
|
|
process.on("SIGINT", function () {
|
|
db.connection.close(function () {
|
|
console.log(
|
|
termination(
|
|
"Mongoose default connection is disconnected due to application termination"
|
|
)
|
|
);
|
|
process.exit(0);
|
|
});
|
|
});
|
|
} catch (err) {
|
|
throw boom.boomify(err);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = fp(dbConnection, { fastify: ">=1.0.0" });
|