Skip to content

Commit

Permalink
connects dbConnection and db_build.js to build the database
Browse files Browse the repository at this point in the history
Relates #1
  • Loading branch information
SleepySheepy172 committed Aug 22, 2018
1 parent 83c634c commit 0c9ddd6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
1 change: 1 addition & 0 deletions config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
EMO_DB_URL = postgres://emoadmin:adminemo@localhost:5432/emotions
5 changes: 3 additions & 2 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 @@ -39,4 +40,4 @@
"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;
8 changes: 4 additions & 4 deletions src/db/db_build.js
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/db/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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', '[email protected]', 'placeholder'),
Expand Down

0 comments on commit 0c9ddd6

Please sign in to comment.