Skip to content

Commit

Permalink
Merge pull request #14 from GSG-G7/database
Browse files Browse the repository at this point in the history
setup Database
  • Loading branch information
AlaaTaima authored Aug 27, 2019
2 parents 42f7b61 + 4c61b6f commit 5b55ee6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions server/database/config/buil_db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { join } = require('path');
const { readFileSync } = require('fs');
const connection = require('./connection');

let sql = readFileSync(join(__dirname, 'build_db.sql')).toString();

connection
.query(sql)
.then(console.log('Build Successfully', new Date()))
.catch((err) => console.log(err));

module.exports = { connection };

32 changes: 32 additions & 0 deletions server/database/config/build_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
BEGIN;
DROP table if exists users, tweets, comments CASCADE;
create table users (
id serial Primary key,
name varchar(20),
email varchar UNIQUE,
password varchar(20),
imgUrl varchar,
gender varchar(10),
birthday varchar,
country varchar(30),
bio text
);

create table tweets(
id serial primary key,
content text NOT NULL,
publishTime timestamp,
user_id integer references users(id)
);

create table comments(
id serial primary key,
content text,
publishTime timestamp,
user_id integer references users(id),
tweet_id integer references tweets(id)
);

insert into users (name,email,password,imgUrl,gender,birthday, country, bio) values ('Fares','fares@gmail.com','fares1998','hgjjjkhghghjjhj','male','29/5/1998','Palestine','Web fullstack Developer');
insert into tweets (user_id,content,publishTime) values (1,'Be optimistic ^_^','2019-08-27T13:08:15.369Z');
COMMIT;
12 changes: 12 additions & 0 deletions server/database/config/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Pool } = require('pg');
require('env2')('./config.env')

let dbUrl = process.env.DB_URL;

const options = {
connectionString: dbUrl,
ssl: true,
};

if (!dbUrl) throw new Error('DB_URL not found');
module.exports = new Pool(options);

0 comments on commit 5b55ee6

Please sign in to comment.