Database settings moved to own file in /config

This commit is contained in:
Lucas Patenaude
2024-05-03 02:47:55 -06:00
parent c3c2662b25
commit 4886632263
2 changed files with 30 additions and 28 deletions

24
config/database.js Normal file
View File

@@ -0,0 +1,24 @@
const pgp = require("pg-promise")();
// Database configuration
const dbConfig = {
host: "db",
port: 5432,
database: "users_db",
user: "postgres",
password: "pwd",
};
const db = pgp(dbConfig);
// Test database connection
db.connect()
.then((obj) => {
console.log("Database connection successful");
obj.done();
})
.catch((error) => {
console.log("ERROR:", error.message || error);
});
module.exports = db;

View File

@@ -14,6 +14,12 @@ const bcrypt = require("bcryptjs"); // To hash passwords
const axios = require("axios"); // To make HTTP requests from our server. We'll learn more about it in Part C.
const moment = require("moment"); // To extract current time data
// Start the Database
const db = require("../config/database"); // Import the db module
// Export the app object to index.js
module.exports = app;
// *****************************************************
// <!-- Section 2 : Connect to DB -->
// *****************************************************
@@ -25,28 +31,6 @@ const hbs = handlebars.create({
partialsDir: __dirname + "/../public/views/partials",
});
// database configuration
// database configuration
const dbConfig = {
host: "db", // the database server
port: 5432, // the database port
database: "users_db", // the database name
user: "postgres", // the user account to connect with
password: "pwd", // the password of the user account
};
const db = pgp(dbConfig);
// test your database
db.connect()
.then((obj) => {
console.log("Database connection successful"); // you can view this message in the docker compose logs
obj.done(); // success, release the connection;
})
.catch((error) => {
console.log("ERROR:", error.message || error);
});
// *****************************************************
// <!-- Section 3 : App Settings -->
// *****************************************************
@@ -397,9 +381,3 @@ async function getFavoriteTeamsForUser(userId) {
throw error; // Rethrow the error for handling at a higher level
}
}
// *****************************************************
// <!-- Section 6 : Export Object -->
// *****************************************************
module.exports = app; // Export the app object to index.js