Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make db #5

Merged
merged 5 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"test": "nyc tape ./test/*.js | tap-spec",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
"start": "nodemon server.js"
"start": "nodemon server.js",
"build-db": "node src/db/db_build.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -36,6 +37,7 @@
"express-handlebars": "^3.0.0",
"express-sessions": "^1.0.6",
"passport": "^0.4.0",
"pg": "^7.4.3"
"pg": "^7.4.3",
"pg-promise": "^8.4.6"
}
}
}
37 changes: 37 additions & 0 deletions src/db/dbConnection.js
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;
11 changes: 11 additions & 0 deletions src/db/db_build.js
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));
30 changes: 30 additions & 0 deletions src/db/db_build.sql
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;