Skip to content

Commit

Permalink
create db_build.js & fix db_build.sql
Browse files Browse the repository at this point in the history
Relates #8
  • Loading branch information
iPhatty committed Apr 11, 2018
1 parent afedcbe commit 25bd383
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Src/Database/db_build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require('fs');
const dbConnection = require('./db_connection');

const sql = fs.readFileSync(`${__dirname}/db_build.sql`).toString();

dbConnection.query(sql, (err, res) => {
if (err) throw err;
console.log('Database created');
dbConnection.end();
})
8 changes: 3 additions & 5 deletions Src/Database/db_build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(100) NOT NULL,
password CHAR(64) NOT NULL,
member_since TIMESTAMPTZ NOT NULL NOW()
member_since TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE destinations (
Expand All @@ -17,10 +17,8 @@ CREATE TABLE destinations (

CREATE TABLE comments (
comment TEXT NOT NULL,
userid INT,
destid INT,
FOREIGN KEY destid REFERENCES destinations(id),
FOREIGN KEY userid REFERENCES users(id),
userid INTEGER REFERENCES users(id),
destid INTEGER REFERENCES destinations(id),
PRIMARY KEY (userid, destid)
);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A web app where you can write a comment on a place you have visited",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build-db": "node Src/Database/db_build.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 25bd383

Please sign in to comment.