From 4289fa2d3c2ef044b94b458443228ddee85515b6 Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Wed, 22 Aug 2018 14:58:38 +0100 Subject: [PATCH 1/5] created database schema and added dummy data Relates #1 --- src/db/db_build.sql | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/db/db_build.sql b/src/db/db_build.sql index e69de29..46eb0e5 100644 --- a/src/db/db_build.sql +++ b/src/db/db_build.sql @@ -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', 'nicolas@cage.com', 'placeholder'), +('Harry', 'harry@potter.com', '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; \ No newline at end of file From 419c9e72dc7837abf8cd6c245ac2f21621b8ce74 Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Wed, 22 Aug 2018 15:05:57 +0100 Subject: [PATCH 2/5] installed pg-promise --- package-lock.json | 26 ++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index f0c4fd3..f072abc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2815,6 +2815,11 @@ "pify": "^3.0.0" } }, + "manakin": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/manakin/-/manakin-0.5.1.tgz", + "integrity": "sha1-xKcRb2sA3z1fGjetPKUV0iBlplg=" + }, "map-cache": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/map-cache/-/map-cache-0.2.2.tgz", @@ -5448,11 +5453,27 @@ "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" }, + "pg-minify": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-0.5.4.tgz", + "integrity": "sha512-GHB2v4OiMHDgwiHH86ZWNfvgEPVijrnfuWLQocseX6Zlf30k+x0imA65zBy4skIpEwfBBEplIEEKP4n3q9KkVA==" + }, "pg-pool": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.3.tgz", "integrity": "sha1-wCIDLIlJ8xKk+R+2QJzgQHa+Mlc=" }, + "pg-promise": { + "version": "8.4.6", + "resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-8.4.6.tgz", + "integrity": "sha512-H0QaHNp6K5EBr6xJaqGTDOt/lWZtbL7o0461FfEzUCj0Zpd2JPW/r54VmDASjAVew59jQmHBpLmvZ7p5pkvFgQ==", + "requires": { + "manakin": "0.5.1", + "pg": "7.4.3", + "pg-minify": "0.5.4", + "spex": "2.0.2" + } + }, "pg-types": { "version": "1.12.1", "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz", @@ -6086,6 +6107,11 @@ "integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=", "dev": true }, + "spex": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/spex/-/spex-2.0.2.tgz", + "integrity": "sha512-LU6TS3qTEpRth+FnNs/fIWEmridYN7JmaN2k1Jk31XVC4ex7+wYxiHMnKguRxS7oKjbOFl4H6seeWNDFFgkVRg==" + }, "split": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/split/-/split-0.3.3.tgz", diff --git a/package.json b/package.json index 80ffaba..5339b1c 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,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" } } From 83c634cb6f044fc18e3265529ffd8060d3b1d27b Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Wed, 22 Aug 2018 15:16:11 +0100 Subject: [PATCH 3/5] adds queryfile to db_build --- src/db/db_build.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/db/db_build.js b/src/db/db_build.js index e69de29..73fa0c0 100644 --- a/src/db/db_build.js +++ b/src/db/db_build.js @@ -0,0 +1,11 @@ +const { queryfile } = require('pg-promise'); +const path = require('path'); +const db = require('./db_connection'); + +const sql = file => queryfile(path.join(__dirname, file) /*, {minify: true}*/); + +const build = sql('./build.sql'); + +db.query(build) + .then(res => console.log('db built, res = ', res)) + .catch(err => console.log('db build error = ', err)); \ No newline at end of file From 0c9ddd6b852c2465d5c64cae9429e48fcd061f0c Mon Sep 17 00:00:00 2001 From: Emma Ogden Date: Wed, 22 Aug 2018 16:13:41 +0100 Subject: [PATCH 4/5] connects dbConnection and db_build.js to build the database Relates #1 --- config.env | 1 + package.json | 5 +++-- src/db/dbConnection.js | 37 +++++++++++++++++++++++++++++++++++++ src/db/db_build.js | 8 ++++---- src/db/db_build.sql | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 config.env create mode 100644 src/db/dbConnection.js diff --git a/config.env b/config.env new file mode 100644 index 0000000..1a900f9 --- /dev/null +++ b/config.env @@ -0,0 +1 @@ +EMO_DB_URL = postgres://emoadmin:adminemo@localhost:5432/emotions \ No newline at end of file diff --git a/package.json b/package.json index 5339b1c..2f03a40 100644 --- a/package.json +++ b/package.json @@ -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", @@ -39,4 +40,4 @@ "pg": "^7.4.3", "pg-promise": "^8.4.6" } -} +} \ No newline at end of file diff --git a/src/db/dbConnection.js b/src/db/dbConnection.js new file mode 100644 index 0000000..31f0e18 --- /dev/null +++ b/src/db/dbConnection.js @@ -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; \ No newline at end of file diff --git a/src/db/db_build.js b/src/db/db_build.js index 73fa0c0..89d1c60 100644 --- a/src/db/db_build.js +++ b/src/db/db_build.js @@ -1,10 +1,10 @@ -const { queryfile } = require('pg-promise'); +const { QueryFile } = require('pg-promise'); const path = require('path'); -const db = require('./db_connection'); +const db = require('./dbConnection'); -const sql = file => queryfile(path.join(__dirname, file) /*, {minify: true}*/); +const sql = file => QueryFile(path.join(__dirname, file)/*, { minify: true }*/); -const build = sql('./build.sql'); +const build = sql('./db_build.sql'); db.query(build) .then(res => console.log('db built, res = ', res)) diff --git a/src/db/db_build.sql b/src/db/db_build.sql index 46eb0e5..832b9f7 100644 --- a/src/db/db_build.sql +++ b/src/db/db_build.sql @@ -16,7 +16,7 @@ CREATE TABLE scores ( 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', 'nicolas@cage.com', 'placeholder'), From fd8925a145c2a4c7e07a021186b30292cde5e684 Mon Sep 17 00:00:00 2001 From: Kate Beard Date: Wed, 22 Aug 2018 16:21:57 +0100 Subject: [PATCH 5/5] Delete config.env --- config.env | 1 - 1 file changed, 1 deletion(-) delete mode 100644 config.env diff --git a/config.env b/config.env deleted file mode 100644 index 1a900f9..0000000 --- a/config.env +++ /dev/null @@ -1 +0,0 @@ -EMO_DB_URL = postgres://emoadmin:adminemo@localhost:5432/emotions \ No newline at end of file