-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 dbconnection = require('./connection.js'); | ||
const sql = fs.readFileSync(`${__dirname}/build.sql`).toString(); | ||
dbconnection.query(sql, (err, result) => { | ||
if (err) { | ||
console.log(err, "error"); | ||
} else { | ||
console.log("database created"); | ||
dbconnection.end(() => { | ||
console.log('connection closed') | ||
}) | ||
} | ||
}); | ||
}; | ||
|
||
buildDatabase(); | ||
|
||
|
||
module.exports = { dbBuild }; | ||
module.exports = buildDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' ,'[email protected]','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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |