From c366ab26dac40e0da8d1615e04bf683b0fb47ae6 Mon Sep 17 00:00:00 2001 From: kholoudfann <141060@ppu.edu.ps> Date: Wed, 26 Feb 2020 10:07:28 +0200 Subject: [PATCH] connection the schema sql with build.js #22 --- database/config/build.js | 31 +++++------ database/config/build.sql | 98 +++++++++++++++++------------------ database/config/connection.js | 31 ++++++----- 3 files changed, 84 insertions(+), 76 deletions(-) diff --git a/database/config/build.js b/database/config/build.js index c24a4f8..440784b 100644 --- a/database/config/build.js +++ b/database/config/build.js @@ -1,18 +1,19 @@ -const fs = require("fs"); -const path = require("path"); - -const dbConnection = require("./connection.js"); - - -const dbBuild = () => { - const sqlPath = readFileSync (join(__dirname, "build.sql")).toString(); -return dbConnection.query(sql,( err,res)=>{ - if(err) throw new Error('error build.js'); - else console.log('build sucsess'); - -}); +const fs = require('fs'); +const buildDatabase = () => { + const connection = require('./connection.js'); + const sql = fs.readFileSync(`${__dirname}/build.sql`).toString(); + connection.query(sql, (err, result) => { + if (err) { + console.log(err, "error"); + } else { + console.log("database created"); + connection.end(() => { + console.log('connection closed') + }) + } + }); }; +buildDatabase(); - -module.exports = {dbBuild}; \ No newline at end of file +module.exports = buildDatabase; \ No newline at end of file diff --git a/database/config/build.sql b/database/config/build.sql index d047bbe..f1b1723 100644 --- a/database/config/build.sql +++ b/database/config/build.sql @@ -1,10 +1,10 @@ BEGIN; -DROP TABLE IF EXISTS students, teachers, grades, courses; +DROP TABLE IF EXISTS relations ,students, teachers, grades, courses CASCADE; -CREATE TABLE IF NOT EXISTS courses( - course_id SERIAL PRIMARY KEY, +CREATE TABLE courses( + course_id SERIAL PRIMARY KEY, course_title TEXT NOT NULL ); @@ -17,14 +17,14 @@ INSERT INTO courses (course_id, course_title) VALUES(5,'Biology'); INSERT INTO courses (course_id, course_title) VALUES(6,'coding'); INSERT INTO courses (course_id, course_title) VALUES(7,'English'); -CREATE TABLE IF NOT EXISTS students( +CREATE TABLE students( student_id SERIAL PRIMARY KEY, student_name TEXT NOT NULL , - student_birth INTEGER NOT NULL, + student_birth INTEGER , student_sex TEXT NOT NULL, - student_address TEXT NOT NULL , + student_address TEXT , student_EMAIL VARCHAR(320), - student_img VARCHAR(250) NOT NULL + student_img VARCHAR(1000) NoT NULL ); @@ -34,52 +34,52 @@ INSERT INTO students (student_id, student_name, student_birth, student_sex, stud INSERT INTO students (student_id, student_name, student_birth, student_sex, student_address, student_EMAIL,student_img) VALUES (4,'Harry' , 17/11/1996, 'Male', 'Bethlehem' ,'Harry1996@gmail.com','https://www.google.com/url?sa=i&url=https%3A%2F%2Fp-upload.facebook.com%2Fnasdaily%2Fphotos%2F%3Fref%3Dpage_internal&psig=AOvVaw0aqN38lJLcC_x4z05J8o3B&ust=1582725738852000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCPjl3f3u7OcCFQAAAAAdAAAAABAO'); -CREATE TABLE IF NOT EXISTS relation( +CREATE TABLE relations( relation_id SERIAL PRIMARY KEY, -course_id INT , -FOREIGN KEY course_id REFERENCES courses (course_id) -student_id INT, -FOREIGN KEY student_id REFERENCES students (student_id), +course_id INTEGER , +FOREIGN KEY (course_id) REFERENCES courses (course_id) , +student_id INTEGER, +FOREIGN KEY (student_id) REFERENCES students (student_id) ); -INSERT INTO relation( course_id , student_id) VALUES (1,1); -INSERT INTO relation( course_id , student_id) VALUES (2,1); -INSERT INTO relation( course_id , student_id) VALUES (3,1); -INSERT INTO relation( course_id , student_id) VALUES (4,1); -INSERT INTO relation( course_id , student_id) VALUES (5,1); -INSERT INTO relation( course_id , student_id) VALUES (6,1); -INSERT INTO relation( course_id , student_id) VALUES (7,1); -INSERT INTO relation( course_id , student_id) VALUES (1,2); -INSERT INTO relation( course_id , student_id) VALUES (2,2); -INSERT INTO relation( course_id , student_id) VALUES (3,2); -INSERT INTO relation( course_id , student_id) VALUES (4,2); -INSERT INTO relation( course_id , student_id) VALUES (5,2); -INSERT INTO relation( course_id , student_id) VALUES (6,2); -INSERT INTO relation( course_id , student_id) VALUES (7,2); -INSERT INTO relation( course_id , student_id) VALUES (1,3); -INSERT INTO relation( course_id , student_id) VALUES (2,3); -INSERT INTO relation( course_id , student_id) VALUES (3,3); -INSERT INTO relation( course_id , student_id) VALUES (4,3); -INSERT INTO relation( course_id , student_id) VALUES (5,3); -INSERT INTO relation( course_id , student_id) VALUES (6,3); -INSERT INTO relation( course_id , student_id) VALUES (7,3); -INSERT INTO relation( course_id , student_id) VALUES (1,4); -INSERT INTO relation( course_id , student_id) VALUES (2,4); -INSERT INTO relation( course_id , student_id) VALUES (3,4); -INSERT INTO relation( course_id , student_id) VALUES (4,4); -INSERT INTO relation( course_id , student_id) VALUES (5,4); -INSERT INTO relation( course_id , student_id) VALUES (6,4); -INSERT INTO relation( course_id , student_id) VALUES (7,4); - - -CREATE TABLE IF NOT EXISTS grades( +INSERT INTO relations( course_id , student_id) VALUES (1,1); +INSERT INTO relations( course_id , student_id) VALUES (2,1); +INSERT INTO relations( course_id , student_id) VALUES (3,1); +INSERT INTO relations( course_id , student_id) VALUES (4,1); +INSERT INTO relations( course_id , student_id) VALUES (5,1); +INSERT INTO relations( course_id , student_id) VALUES (6,1); +INSERT INTO relations( course_id , student_id) VALUES (7,1); +INSERT INTO relations( course_id , student_id) VALUES (1,2); +INSERT INTO relations( course_id , student_id) VALUES (2,2); +INSERT INTO relations( course_id , student_id) VALUES (3,2); +INSERT INTO relations( course_id , student_id) VALUES (4,2); +INSERT INTO relations( course_id , student_id) VALUES (5,2); +INSERT INTO relations( course_id , student_id) VALUES (6,2); +INSERT INTO relations( course_id , student_id) VALUES (7,2); +INSERT INTO relations( course_id , student_id) VALUES (1,3); +INSERT INTO relations( course_id , student_id) VALUES (2,3); +INSERT INTO relations( course_id , student_id) VALUES (3,3); +INSERT INTO relations( course_id , student_id) VALUES (4,3); +INSERT INTO relations( course_id , student_id) VALUES (5,3); +INSERT INTO relations( course_id , student_id) VALUES (6,3); +INSERT INTO relations( course_id , student_id) VALUES (7,3); +INSERT INTO relations( course_id , student_id) VALUES (1,4); +INSERT INTO relations( course_id , student_id) VALUES (2,4); +INSERT INTO relations( course_id , student_id) VALUES (3,4); +INSERT INTO relations( course_id , student_id) VALUES (4,4); +INSERT INTO relations( course_id , student_id) VALUES (5,4); +INSERT INTO relations( course_id , student_id) VALUES (6,4); +INSERT INTO relations( course_id , student_id) VALUES (7,4); + + +CREATE TABLE grades( grade_id SERIAL PRIMARY KEY, grade_mark VARCHAR(50) , - course_id INT , - FOREIGN KEY course_id REFERENCES courses (course_id) , - student_id INT, - FOREIGN KEY student_id REFERENCES students (student_id) + course_id INTEGER , + FOREIGN KEY (course_id) REFERENCES courses (course_id) , + student_id INTEGER, + FOREIGN KEY (student_id) REFERENCES students (student_id) ); INSERT INTO grades( grade_mark , course_id , student_id) VALUES(84,1 ,1 ); @@ -115,11 +115,11 @@ INSERT INTO grades( grade_mark , course_id , student_id) VALUES(88,7,4); -CREATE TABLE IF NOT EXISTS teachers( +CREATE TABLE teachers( teacher_id SERIAL PRIMARY KEY, teacher_address TEXT , teacher_phone INTEGER ); -COMMIT; +COMMIT; \ No newline at end of file diff --git a/database/config/connection.js b/database/config/connection.js index 159dbb2..c268c91 100644 --- a/database/config/connection.js +++ b/database/config/connection.js @@ -1,19 +1,26 @@ -require("env2")("./config.env"); -const { Pool } = require("pg"); -const url = ''; +const {Pool} = require('pg'); +const url = require('url'); +const env = require('env2'); +env('./config.env'); -let connectionString = process.env.DB_URL; - -if (process.env.NODE_ENV === "test") { - connectionString = process.env.TEST_DB_URL; +if (!process.env.DB_URL) { + throw new Error('Environment variable DATABASE_URL must be set'); } -if (!connectionString) { - throw new Error("please set a DB_URL env variable"); -} +const params = url.parse(process.env.DB_URL); + +const [username, password] = params.auth.split(':'); + const options = { - connectionString: url, - sll: true + host: params.hostname, + port: params.port, + database: params.pathname.split('/')[1], + max: process.env.DB_MAX_CONNECTIONS || 2 }; +if (username) { options.user = username; } +if (password) { options.password = password; } + +options.ssl = (options.host !== 'localhost'); + module.exports = new Pool(options); \ No newline at end of file