-
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.
Merge pull request #5 from fac-14/make-db
Make db
- Loading branch information
Showing
5 changed files
with
109 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const pgp = require('pg-promise')(); | ||
// require('env2')('./config.env'); | ||
|
||
const herokuDB = { | ||
host: process.env.HEROKU_HOST, | ||
user: process.env.HEROKU_USER, | ||
password: process.env.HEROKU_PW, | ||
database: process.env.HEROKU_DB, | ||
ssl: true, | ||
}; | ||
|
||
const localDB = { | ||
host: 'localhost', | ||
port: 5432, | ||
database: 'emotions', | ||
user: 'emoadmin', | ||
password: 'adminemo' | ||
}; | ||
|
||
// const testDB = { | ||
// host: 'localhost', | ||
// port: 5432, | ||
// database: 'emotions', | ||
// user: 'emoadmin', | ||
// password: 'adminemo' | ||
// }; | ||
|
||
// let DB_URL = process.env.EMO_DB_URL; | ||
|
||
// if (!DB_URL) { | ||
// throw new Error('Environmental variable DB_URL needs to be set'); | ||
// } | ||
|
||
const connection = process.env.NODE_ENV === 'production' ? herokuDB : localDB; | ||
|
||
const db = pgp(connection); | ||
module.exports = db; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { QueryFile } = require('pg-promise'); | ||
const path = require('path'); | ||
const db = require('./dbConnection'); | ||
|
||
const sql = file => QueryFile(path.join(__dirname, file)/*, { minify: true }*/); | ||
|
||
const build = sql('./db_build.sql'); | ||
|
||
db.query(build) | ||
.then(res => console.log('db built, res = ', res)) | ||
.catch(err => console.log('db build error = ', err)); |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
BEGIN; | ||
|
||
DROP TABLE IF EXISTS users, scores CASCADE; | ||
|
||
CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(50) NOT NULL, | ||
email VARCHAR(80) UNIQUE NOT NULL, | ||
pw_hash VARCHAR(100) NOT NULL, | ||
admin BOOLEAN DEFAULT FALSE | ||
); | ||
|
||
CREATE TABLE scores ( | ||
id SERIAL PRIMARY KEY, | ||
user_id INTEGER REFERENCES users(id) NOT NULL, | ||
score FLOAT(4) NOT NULL, -- should give about 7 decimal places (4-bytes worth) | ||
emotion VARCHAR(20), | ||
img VARCHAR(100) | ||
); | ||
|
||
INSERT INTO users (name,email,pw_hash) VALUES | ||
('Nicolas Cage', '[email protected]', 'placeholder'), | ||
('Harry', '[email protected]', 'placeholder'); | ||
|
||
INSERT INTO scores (user_id, score, emotion, img) VALUES | ||
(1, 1000.97657, 'Anger', 'nicolascage-anger-1' ), -- last number should be score id | ||
(2, 1.4736, 'Sadness', 'harry-sadness-1'), -- may need to include file extension at end of img | ||
(2, 20.4736, 'Disgust', 'harry-disgust-1'); | ||
|
||
COMMIT; |