diff --git a/config/database.js b/config/database.js new file mode 100644 index 0000000..e7f104b --- /dev/null +++ b/config/database.js @@ -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; diff --git a/src/app.js b/src/app.js index fd59d02..0722842 100644 --- a/src/app.js +++ b/src/app.js @@ -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; + // ***************************************************** // // ***************************************************** @@ -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); - }); - // ***************************************************** // // ***************************************************** @@ -397,9 +381,3 @@ async function getFavoriteTeamsForUser(userId) { throw error; // Rethrow the error for handling at a higher level } } - -// ***************************************************** -// -// ***************************************************** - -module.exports = app; // Export the app object to index.js