Skip to content

Commit

Permalink
Merge pull request #22 from GSG-G10/4-setup-database
Browse files Browse the repository at this point in the history
setup database
  • Loading branch information
Mu7ammadAbed authored Oct 25, 2021
2 parents 0b3fdac + dc4e28a commit 57da083
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node server",
"dev": "nodemon server",
"test": "jest"
"start": "cross-env NODE_ENV=production node server",
"dev": "cross-env NODE_ENV=development nodemon server",
"test": "cross-env NODE_ENV=test jest"
},
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions server/database/config/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { readFileSync } = require('fs');
const { join } = require('path');

const connection = require('../connection');
const dbBuild = () => {
let sql = readFileSync(join(__dirname, 'build.sql')).toString();
sql+= readFileSync(join(__dirname, 'fakeData.sql')).toString();
return connection.query(sql);
};
module.exports = dbBuild;
43 changes: 43 additions & 0 deletions server/database/config/bulid.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
BEGIN;
DROP TABLE IF EXISTS admins,estates,agents,images;
CREATE TABLE admins(
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE agents(
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL UNIQUE,
avater TEXT

);
CREATE TABLE estates(
id SERIAL PRIMARY KEY,
agent_id INTEGER REFERENCES agents(id) ON DELETE CASCADE,
title VARCHAR(255) NOT NULL,
price DECIMAL NOT NULL,
description TEXT NOT NULL,
type VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL,
street VARCHAR(255) NOT NULL,
city VARCHAR(255) NOT NULL,
region VARCHAR(255) NOT NULL,
bathrooms INTEGER DEFAULT 0,
bedrooms INTEGER DEFAULT 0,
rooms INTEGER DEFAULT 0,
space DECIMAL NOT NULL ,
approved BOOLEAN DEFAULT FALSE,
rate INTEGER DEFAULT 0,
available BOOLEAN DEFAULT TRUE
);

CREATE TABLE images(
id SERIAL PRIMARY KEY,
estate_id INTEGER REFERENCES estates(id) ON DELETE CASCADE,
image TEXT
);
COMMIT;
25 changes: 25 additions & 0 deletions server/database/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require('env2')('.env');
const {Pool} = require('pg');

const {
NODE_ENV, DATABASE_URL_DEV, DATABASE_URL, DATABASE_URL_TEST,
} = process.env;
let dbUrl = '';
switch (NODE_ENV) {
case 'development':
dbUrl = DATABASE_URL_DEV;
break;
case 'production':
dbUrl = DATABASE_URL;
break;
case 'test':
dbUrl = DATABASE_URL_TEST;
break;
default:
throw new Error('There\'s no environment');
}
const options = {
connectionString: dbUrl,
ssl: { rejectUnauthorized: false },
};
module.exports = new Pool(options);

0 comments on commit 57da083

Please sign in to comment.