Skip to content

Commit

Permalink
Improved startup messages
Browse files Browse the repository at this point in the history
  • Loading branch information
malee31 committed Oct 21, 2024
1 parent dd634f4 commit aed6973
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import app, { activateApiRouter } from "./src/app.js";
import * as database from "./src/database/database.js";

app.listen(API_PORT, () => {
console.log("Server Active");
console.log("===== Web Server Active =====");
});

// Start up the database and swap out the router once it is ready
console.log("Starting Database");
console.log("===== Starting Database =====");
database.start()
.then(() => {
activateApiRouter();
console.log("API Router Connected!");
console.log("===== API Active =====");
console.log("===== Ready For Requests =====");
})
.catch(err => {
console.warn("Unable to start API:");
console.warn("!!!!! Unable To Start API (Reason below) !!!!! ");
console.error(err);
process.exit(1);
});
});
20 changes: 12 additions & 8 deletions src/database/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ let sequelize = PRODUCTION
) : (
new Sequelize({
dialect: "sqlite",
storage: "dev-database.sqlite"
storage: "dev-database.sqlite",
logging: false
})
);

Expand Down Expand Up @@ -127,7 +128,10 @@ User.init({
// Generates tables if they do not exist
// WARNING: Will not modify tables with an updated schema if they already exist
async function createTables() {
await sequelize.sync({ alter: true });
await sequelize.sync({
alter: true,
logging: false // Change to `console.log` if debugging or making alterations in development
});
}

// Purges all tables and data
Expand All @@ -150,27 +154,27 @@ async function dropTables() {

// Sets up and starts up the database
async function start(skipTableCreation = false) {
if(!TESTING) console.log("Creating And Testing A Connection");
if(!TESTING) console.log("===== Testing SQL Connection =====");

// Test connection
await db.query("SELECT 1 + 1 AS solution")
.then(() => {
if(!TESTING) console.log("Successfully Connection Confirmed");
if(!TESTING) console.log("===== SQL Connection Confirmed =====");
})
.catch(err => {
console.warn("Failed To Confirm Connection:");
console.warn("!!!!! Failed To Confirm Connection (Reason below) !!!!!");
console.error(err);
});

if(!skipTableCreation) {
// Create tables if they do not already exist
if(!TESTING) console.log("Setting Up Tables");
if(!TESTING) console.log("===== Synchronizing Table Structures =====");
await createTables()
.then(() => {
if(!TESTING) console.log("Table Existence Confirmed");
if(!TESTING) console.log("===== Confirmed Tables Exist =====");
})
.catch(err => {
console.warn("Failed To Create Tables:");
console.warn("!!!!! Failed To Create Tables (Reason below) !!!!!");
console.error(err);
});
}
Expand Down

0 comments on commit aed6973

Please sign in to comment.