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..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", @@ -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" } -} +} \ 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 e69de29..89d1c60 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('./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)); \ No newline at end of file diff --git a/src/db/db_build.sql b/src/db/db_build.sql index e69de29..832b9f7 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