Add database initialization
parent
2557a83e37
commit
9554d54496
|
@ -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();
|
||||
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();
|
|
@ -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;
|
|
@ -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) {
|
||||
|
|
Reference in New Issue