diff --git a/database/database.js b/database/database.js index e653b9a..766168d 100644 --- a/database/database.js +++ b/database/database.js @@ -1,9 +1,41 @@ const app = require('../app'); const { Client } = require('pg'); +const fs = require('fs'); if (process.env.NODE_ENV !== 'production' || process.env.NODE_ENV !== 'testing') { require('dotenv').config(); } const client = new Client(); -client.connect(); \ No newline at end of file +client.connect(); + +async function executeQuery(query, values = []) { + const result = await client.query({ + rowMode: 'array', + text: query, + values: values + }); + return result.rows; +} + +async function InitializeDatabase() { + console.log("Initializing database...") + const sql = fs.readFileSync('database/init_database.sql').toString(); + await executeQuery(sql); + console.log("Database initialized.") +} + + + + +async function checkForDatabaseInitialization() { + const query = `SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'scores'`; + let result = await executeQuery(query); + + const scoresSchemaExists = result.length !== 0; + + if(!scoresSchemaExists) { + InitializeDatabase(); + } +} +checkForDatabaseInitialization(); \ No newline at end of file diff --git a/database/init_database.sql b/database/init_database.sql new file mode 100644 index 0000000..5df20f8 --- /dev/null +++ b/database/init_database.sql @@ -0,0 +1,105 @@ +/* SCORE TRACKER DATABASE LAYOUT + +scores: + + sports: + *sport_id* | name | currently_active + + divisions: + *division_id* | name | gender | *sport_id* | currently_active + + teams: + *team_id* | name | ~sport_id~ | currently_active + + seasons: + *season_id* | school_year + + games: + *game_id* | ~division_id~ | ~season_id~ | date | ~team1_id~ | ~team2_id~ | team1_score | team2_score | ~submitter_id~ | updated_timestamp + + + +accounts: + + users: + *user_id* | email | salt | password_hash | role | approved + + sessions: + *session_id* | ~user_id~ | expiration_date + +*/ + + +BEGIN; + + +CREATE SCHEMA IF NOT EXISTS scores; + + +CREATE TABLE IF NOT EXISTS scores.sports( + sport_id BIGINT GENERATED ALWAYS AS IDENTITY, + sport_name TEXT UNIQUE NOT NULL, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.divisions( + division_id BIGINT GENERATED ALWAYS AS IDENTITY, + division_name TEXT NOT NULL, + gender VARCHAR(1) CHECK (gender IN ( 'F', 'M' ) ), + sport_id BIGINT, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(division_id), + CONSTRAINT fk_sport + FOREIGN KEY(sport_id) + REFERENCES scores.sports(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.teams( + team_id BIGINT GENERATED ALWAYS AS IDENTITY, + team_name TEXT NOT NULL, + sport_id BIGINT, + currently_active BOOLEAN DEFAULT TRUE, + PRIMARY KEY(team_id), + CONSTRAINT fk_sport + FOREIGN KEY(sport_id) + REFERENCES scores.sports(sport_id) + ); + +CREATE TABLE IF NOT EXISTS scores.seasons( + season_id BIGINT GENERATED ALWAYS AS IDENTITY, + school_year INTEGER NOT NULL, + PRIMARY KEY(season_id) + ); + +CREATE TABLE IF NOT EXISTS scores.games( + game_id BIGINT GENERATED ALWAYS AS IDENTITY, + division_id BIGINT, + season_id BIGINT, + game_date DATE, + team1_id BIGINT, + team2_id BIGINT, + team1_score INTEGER, + team2_score INTEGER, +/* submitter_id BIGINT,*/ + updated_timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(), + PRIMARY KEY(game_id), + CONSTRAINT fk_division + FOREIGN KEY(division_id) + REFERENCES scores.divisions(division_id), + CONSTRAINT fk_season + FOREIGN KEY(season_id) + REFERENCES scores.seasons(season_id), + CONSTRAINT fk_team1 + FOREIGN KEY(team1_id) + REFERENCES scores.teams(team_id), + CONSTRAINT fk_team2 + FOREIGN KEY(team2_id) + REFERENCES scores.teams(team_id) +/* CONSTRAINT fk_submitter + FOREIGN KEY(submitter_id) + REFERENCES accounts.users(user_id)*/ + ); + + +COMMIT; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js index 9399195..c806b1c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); +var database = require('../database/database'); /* GET home page. */ router.get('/', function(req, res, next) {